Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
# ... (previous code)

# Training Linear Regression model
from sklearn.linear_model import LinearRegression

# Instantiate Linear Regression model
lr = LinearRegression()

# Train the Linear Regression model
lr.fit(Xtr, ytr)

# Predict using Linear Regression
y_pred_lr = lr.predict(Xtst)

# Training LinearSVM
from sklearn.svm import LinearSVC

# Instantiate LinearSVM model
svm = LinearSVC(dual=False)

# Train the LinearSVM model
svm.fit(Xtr, ytr)

# Predict using LinearSVM
y_pred_svm = svm.predict(Xtst)

# Helper function to transform continuous labels to binary ones
def get_classification_labels_from_regression_predictions(unique_labels, y_pred):
    assert len(unique_labels) == 2
    mean_val = np.mean(unique_labels)
    transformed_predictions = np.zeros(len(y_pred))
    transformed_predictions[y_pred < mean_val] = np.min(unique_labels)
    transformed_predictions[y_pred >= mean_val] = np.max(unique_labels)
    return transformed_predictions

# Get unique labels from ytr (training labels)
unique_labels = np.unique(ytr)

# Transform predictions for both models
y_pred_lr_transformed = get_classification_labels_from_regression_predictions(unique_labels, y_pred_lr)
y_pred_svm_transformed = get_classification_labels_from_regression_predictions(unique_labels, y_pred_svm)

# Calculate accuracies
accuracy_lr = np.sum(y_pred_lr_transformed == ytst) / len(ytst)
accuracy_svm = np.sum(y_pred_svm_transformed == ytst) / len(ytst)

# Calculate the difference in test accuracies
difference_in_accuracies = accuracy_svm - accuracy_lr

print("Test Accuracy Linear Regression:", accuracy_lr)
print("Test Accuracy LinearSVM:", accuracy_svm)
print("Difference in Test Accuracies:", difference_in_accuracies)