|
|
import numpy as np |
|
|
import joblib |
|
|
import os |
|
|
from sklearn.linear_model import LogisticRegression |
|
|
|
|
|
|
|
|
MODEL_PATH = os.path.join(os.path.dirname(__file__), "..", "models", "final_classifier.pkl") |
|
|
os.makedirs(os.path.dirname(MODEL_PATH), exist_ok=True) |
|
|
|
|
|
print(">>> Generating Synthetic Logic-Aligned Training Data...") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_sample(label_idx): |
|
|
|
|
|
feats = np.random.uniform(0.0, 0.3, 13) |
|
|
|
|
|
|
|
|
if label_idx == 0: |
|
|
feats[0] = np.random.uniform(0.6, 1.0) |
|
|
feats[7] = np.random.uniform(0.5, 1.0) |
|
|
feats[5] = np.random.uniform(0.0, 0.2) |
|
|
feats[8] = np.random.uniform(0.0, 0.2) |
|
|
|
|
|
feats[12] = np.random.uniform(0.7, 1.0) |
|
|
feats[9] = np.random.uniform(0.0, 0.2) |
|
|
|
|
|
|
|
|
elif label_idx == 1: |
|
|
feats[1] = np.random.uniform(0.6, 1.0) |
|
|
feats[5] = np.random.uniform(0.5, 1.0) |
|
|
feats[7] = np.random.uniform(0.0, 0.2) |
|
|
feats[8] = np.random.uniform(0.0, 0.2) |
|
|
|
|
|
feats[10] = np.random.uniform(0.7, 1.0) |
|
|
feats[9] = np.random.uniform(0.0, 0.3) |
|
|
|
|
|
|
|
|
elif label_idx == 2: |
|
|
feats[2] = np.random.uniform(0.6, 1.0) |
|
|
feats[7] = np.random.uniform(0.5, 1.0) |
|
|
feats[5] = np.random.uniform(0.0, 0.2) |
|
|
feats[8] = np.random.uniform(0.0, 0.2) |
|
|
|
|
|
feats[11] = np.random.uniform(0.7, 1.0) |
|
|
feats[10] = np.random.uniform(0.0, 0.2) |
|
|
|
|
|
|
|
|
elif label_idx == 3: |
|
|
feats[3] = np.random.uniform(0.6, 1.0) |
|
|
feats[5] = np.random.uniform(0.5, 1.0) |
|
|
feats[7] = np.random.uniform(0.0, 0.2) |
|
|
feats[8] = np.random.uniform(0.0, 0.2) |
|
|
|
|
|
feats[9] = np.random.uniform(0.7, 1.0) |
|
|
feats[10] = np.random.uniform(0.0, 0.4) |
|
|
|
|
|
|
|
|
elif label_idx == 4: |
|
|
feats[4] = np.random.uniform(0.5, 1.0) |
|
|
feats[6] = np.random.uniform(0.5, 1.0) |
|
|
feats[5] = np.random.uniform(0.0, 0.2) |
|
|
feats[7] = np.random.uniform(0.0, 0.2) |
|
|
feats[8] = np.random.uniform(0.0, 0.1) |
|
|
|
|
|
feats[9] = np.random.uniform(0.0, 0.3) |
|
|
feats[10] = np.random.uniform(0.0, 0.3) |
|
|
|
|
|
return feats |
|
|
|
|
|
|
|
|
X = [] |
|
|
y = [] |
|
|
SAMPLES_PER_CLASS = 500 |
|
|
|
|
|
for label in range(5): |
|
|
for _ in range(SAMPLES_PER_CLASS): |
|
|
X.append(generate_sample(label)) |
|
|
y.append(label) |
|
|
|
|
|
X = np.array(X) |
|
|
y = np.array(y) |
|
|
|
|
|
print(f"Training Logistic Regression on {len(X)} synthetic samples (13 features)...") |
|
|
clf = LogisticRegression(max_iter=1000, multi_class='multinomial', solver='lbfgs') |
|
|
clf.fit(X, y) |
|
|
|
|
|
print(f"Accuracy on Training Set: {clf.score(X, y):.4f}") |
|
|
|
|
|
print(f"Saving model to {MODEL_PATH}...") |
|
|
joblib.dump(clf, MODEL_PATH) |
|
|
print("DONE.") |
|
|
|
|
|
|