Loading…
DS · Classification · Program 22 · In the record
Compare classification algorithms on the same dataset.
1from sklearn.datasets import load_wine2from sklearn.model_selection import train_test_split3from sklearn.preprocessing import StandardScaler4from sklearn.metrics import accuracy_score5from sklearn.linear_model import LogisticRegression6from sklearn.neighbors import KNeighborsClassifier7from sklearn.tree import DecisionTreeClassifier8from sklearn.svm import SVC9from sklearn.ensemble import RandomForestClassifier10from sklearn.naive_bayes import GaussianNB11 12X, y = load_wine(return_X_y=True)13X = StandardScaler().fit_transform(X)14X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)15 16models = {"Logistic Regression": LogisticRegression(), "kNN": KNeighborsClassifier(),17 "Decision Tree": DecisionTreeClassifier(random_state=42), "SVM": SVC(),18 "Random Forest": RandomForestClassifier(random_state=42), "Naive Bayes": GaussianNB()}19for name, model in models.items():20 model.fit(X_train, y_train)21 print(f"{name:20s} Accuracy: {accuracy_score(y_test, model.predict(X_test)):.4f}")Logistic Regression Accuracy: 0.9815 kNN Accuracy: 0.9630 Decision Tree Accuracy: 0.9630 SVM Accuracy: 0.9815 Random Forest Accuracy: 1.0000 Naive Bayes Accuracy: 1.0000

Run program 22 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.