Question: # Evaluate on the test set y _ pred = grid _ svm . predict ( X _ test ) print ( SVM Test

# Evaluate on the test set
y_pred = grid_svm.predict(X_test)
print("SVM Test Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:
", classification_report(y_test, y_pred))
print("Confusion Matrix:
", confusion_matrix(y_test, y_pred))
# # K-Nearest Neighbors (KNN)
# Define hyperparameters for KNN
param_grid_knn ={
'classifier__n_neighbors': [3,5,7,10],
'classifier__weights': ['uniform', 'distance'],
'classifier__p': [1,2]
}
# Set up GridSearchCV
grid_knn = GridSearchCV(Pipeline([
('preprocessor', preprocessor),
('classifier', KNeighborsClassifier())
]), param_grid_knn, cv=5, scoring='accuracy')
# Fit GridSearchCV
grid_knn.fit(X_train, y_train)
# Best parameters and score
print("Best Parameters for KNN:", grid_knn.best_params_)
print("Best Score for KNN:", grid_knn.best_score_)
# Evaluate on the test set
y_pred = grid_knn.predict(X_test)
print("KNN Test Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:
", classification_report(y_test, y_pred))
print("Confusion Matrix:
", confusion_matrix(y_test, y_pred))
# # Train and Evaluate the Models
# Store results in a dictionary
results ={}
# Logistic Regression
y_pred = grid_log_reg.predict(X_test)
results['Logistic Regression']={
'Accuracy': accuracy_score(y_test, y_pred),
'Classification Report': classification_report(y_test, y_pred, output_dict=True),
'Confusion Matrix': confusion_matrix(y_test, y_pred)
}
# Decision Tree
y_pred = grid_dec_tree.predict(X_test)
results['Decision Tree']={
'Accuracy': accuracy_score(y_test, y_pred),
'Classification Report': classification_report(y_test, y_pred, output_dict=True),
'Confusion Matrix': confusion_matrix(y_test, y_pred)
}
# Random Forest
y_pred = grid_rf.predict(X_test)
results['Random Forest']={
'Accuracy': accuracy_score(y_test, y_pred),
'Classification Report': classification_report(y_test, y_pred, output_dict=True),
'Confusion Matrix': confusion_matrix(y_test, y_pred)
}
# SVM
y_pred = grid_svm.predict(X_test)
results['SVM']={
'Accuracy': accuracy_score(y_test, y_pred),
'Classification Report': classification_report(y_test, y_pred, output_dict=True),
'Confusion Matrix': confusion_matrix(y_test, y_pred)
}
# KNN
y_pred = grid_knn.predict(X_test)
results['KNN']={
'Accuracy': accuracy_score(y_test, y_pred),
'Classification Report': classification_report(y_test, y_pred, output_dict=True),
'Confusion Matrix': confusion_matrix(y_test, y_pred)
}
# # Compare Accuracy
import matplotlib.pyplot as plt
# Extract accuracies
models = list(results.keys())
accuracies =[results[model]['Accuracy'] for model in models]
# Plot
plt.figure(figsize=(10,6))
plt.bar(models, accuracies, color=['blue', 'green', 'red', 'purple', 'orange'])
plt.title('Model Accuracy Comparison')
plt.xlabel('Model')
plt.ylabel('Accuracy')
plt.ylim([0,1])
plt.show()
# # Visualize Confusion Matrices
import seaborn as sns
def plot_confusion_matrix(conf_matrix, title):
plt.figure(figsize=(8,6))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)
plt.title(title)
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()
# Plot confusion matrices for each model
for model, result in results.items():
plot_confusion_matrix(result['Confusion Matrix'], f'{model} Confusion Matrix')
# # Text-Based Application
import joblib
# Save the model
joblib.dump(grid_rf.best_estimator_, 'random_forest_model.pkl')
Explain the code ?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!