add binary crossentropy to gather the metrics, from sklearn.featureextraction.text…
- User
add binary crossentropy to gather the metrics, from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics import classification_report from sklearn.linear_model import LogisticRegression from sklearn.naive_bayes import MultinomialNB from sklearn.naive_bayes import GaussianNB from sklearn.ensemble import RandomForestClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay import matplotlib.pyplot as plt
Adjusting the function to accept pre-split data
def compare_classifiers(X_train, X_test, y_train, y_test):
code# List of classifiers to compare classifiers = [ ('Logistic Regression', LogisticRegression(random_state=42)), ('Naive Bayes', MultinomialNB()), ('Random Forest', RandomForestClassifier(n_estimators=100, random_state=42)), ('KNN', KNeighborsClassifier(n_neighbors=5)) ] # Iterate over classifiers, train, predict, and display metrics for name, clf in classifiers: print(f'----- {name} -----') clf.fit(X_train, y_train) y_pred = clf.predict(X_test) # Classification Report print('Classification Report:') print(classification_report(y_test, y_pred)) # Confusion Matrix print('Confusion Matrix:') cm = confusion_matrix(y_test, y_pred) disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=clf.classes_) disp.plot() plt.show() print('\n')The function now expects four parameters:
X_train: The training data (text)
X_test: The test data (text)
y_train: The training labels
y_test: The test labels
Example usage (assuming pre-split data):
compare_classifiers(X_train, X_test, y_train, y_test)
- Assistant