ML3,4
ML3,4
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler, OneHotEncoder,
OrdinalEncoder
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
92200103150 11
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
print(df.isnull().sum())
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix
from sklearn.preprocessing import LabelEncoder
92200103150 13
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
# Initialize classifiers
classifiers = {
"BernoulliNB": BernoulliNB(),
"GaussianNB": GaussianNB(),
"MultinomialNB": MultinomialNB(),
"KNeighborsClassifier": KNeighborsClassifier(),
"DecisionTreeClassifier": DecisionTreeClassifier(),
"RandomForestClassifier": RandomForestClassifier(),
"SVC": SVC(probability=True) #probability=True is needed for
roc_auc_score
}
clf.fit(trainX, trainy)
predy = clf.predict(testX)
# Evaluate the model
print(f"Results for {name}:")
print("X_train:", trainX.shape)
print("X_test:", testX.shape)
print("y_train:", trainy.shape)
print("y_test:", testy.shape)
print("y_pred:", predy.shape)
print("\nAccuracy:", accuracy_score(testy, predy))
print("\nConfusion Matrix:\n", confusion_matrix(testy, predy))
try:
print("\nROC AUC Score:", roc_auc_score(testy, clf.predict_proba(testX),
multi_class='ovr'))
except:
print("\nROC AUC Score: Not applicable for this classifier")
print("-" * 50)
print("\nClassification Report:\n", classification_report(testy, predy))
92200103150 14
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
92200103150 15
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
92200103150 16
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
92200103150 17
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
92200103150 18
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
92200103150 19
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual
92200103150 20