0% found this document useful (0 votes)
15 views11 pages

ML3,4

The document is a lab manual for a Machine Learning course, detailing practical exercises involving the implementation of SVM classifiers and various classification techniques using datasets like nursery and Fashion MNIST. It includes code snippets for data preparation, model training, evaluation metrics, and results display. The manual emphasizes the use of libraries such as pandas, scikit-learn, and Keras for machine learning tasks.

Uploaded by

sanjanaghinaiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

ML3,4

The document is a lab manual for a Machine Learning course, detailing practical exercises involving the implementation of SVM classifiers and various classification techniques using datasets like nursery and Fashion MNIST. It includes code snippets for data preparation, model training, evaluation metrics, and results display. The manual emphasizes the use of libraries such as pandas, scikit-learn, and Keras for machine learning tasks.

Uploaded by

sanjanaghinaiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

FACULTY OF TECHNOLOGY

Department Of Computer Engineering


01CE0614-Machine Learning -Lab Manual

Practical 3: Using a dataset implement SVM classifier

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

# Check the imported libraries


print("Libraries Imported Successfully!")

# Load the dataset


df = pd.read_csv('/content/nursery.csv')
df

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

# Prepare the data


# Separate features (X) and target variable (y)
X = df.drop('final evaluation', axis=1)
y = df['final evaluation']

# Convert categorical features to numerical using Label Encoding


label_encoders = {}
for column in X.columns:
label_encoders[column] = LabelEncoder()
X[column] = label_encoders[column].fit_transform(X[column])

# Encode the target variable


label_encoder_y = LabelEncoder()
y = label_encoder_y.fit_transform(y)

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42) # 80% train, 20% test

# Initialize and train the SVM model


model = SVC(kernel='linear') # You can experiment with different kernels
model.fit(X_train, y_train)

# Make predictions on the test set


y_pred = model.predict(X_test)
92200103150 12
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual

# Evaluate the model


print("Results:")
print("X_train:", X_train.shape)
print("X_test:", X_test.shape)
print("y_train:", y_train.shape)
print("y_test:", y_test.shape)
print("y_pred:", y_pred.shape)
print("\nAccuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))

92200103150 13
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual

Practical 4 :Implement classification techniques evaluation parameters


using sample dataset.
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import BernoulliNB, GaussianNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix, roc_auc_score
from sklearn.preprocessing import LabelEncoder, StandardScaler
from keras.datasets import fashion_mnist

# Load the Fashion MNIST dataset


(trainX, trainy), (testX, testy) = fashion_mnist.load_data()

# Flatten the images


trainX = trainX.reshape(trainX.shape[0], -1)
testX = testX.reshape(testX.shape[0], -1)

# Normalize the pixel values


trainX = trainX / 255.0
testX = testX / 255.0

# 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
}

# Train and evaluate each classifier


for name, clf in classifiers.items():
92200103150 14
FACULTY OF TECHNOLOGY
Department Of Computer Engineering
01CE0614-Machine Learning -Lab Manual

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

You might also like