0% found this document useful (0 votes)
36 views7 pages

Comsats University Islamabad Sub Campus Vehari: Assignment No 2 Submitted To: Dr. Rab Nawaz

This document contains code to test the accuracy of various machine learning classifiers on iris and titanic datasets. It first tests a Gaussian Naive Bayes classifier on iris data, achieving an accuracy of 97.777777%. It then tests a KNN classifier with 2 neighbors on iris data, achieving 100% accuracy. Finally, it uses an SVM classifier to predict titanic survival data, achieving 64.7% accuracy. When the SVM model predicts on a sample input, it predicts a survival of 0.

Uploaded by

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

Comsats University Islamabad Sub Campus Vehari: Assignment No 2 Submitted To: Dr. Rab Nawaz

This document contains code to test the accuracy of various machine learning classifiers on iris and titanic datasets. It first tests a Gaussian Naive Bayes classifier on iris data, achieving an accuracy of 97.777777%. It then tests a KNN classifier with 2 neighbors on iris data, achieving 100% accuracy. Finally, it uses an SVM classifier to predict titanic survival data, achieving 64.7% accuracy. When the SVM model predicts on a sample input, it predicts a survival of 0.

Uploaded by

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

Comsats university Islamabad Sub campus Vehari

Assignment No 2

Submitted to: Dr. Rab Nawaz


Submitted by: Sania Iram

Registration #: FA22-RCS-002

Class: MSCS

Batch: 03

Course code: CSC-668


GNB Accuracy Test

from sklearn.naive_bayes import GaussianNB

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

import numpy as np

from sklearn import datasets

iris = datasets.load_iris()

X = iris.data

y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42)

gnb = GaussianNB()

gnb.fit(X_train, y_train)

y_pred = gnb.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

Accuracy: 0.9777777777777777
KNN Classifier Accuracy Testing

from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

import numpy as np
from sklearn import datasets

iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
knn = KNeighborsClassifier(n_neighbors=2)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Output:
Accuracy: 1.0
Titanic Survival Prediction with SVM Model

import pandas as pd
from sklearn import svm

df=pd.read_csv(r'C:\Users\Mian\Documents\Excel/titanic_survival.csv')
df

Gender Age Ticket Survived


Pclass

0 3 male 34 78292 0

1 3 female 47 7 1

2 2 male 62 96875 0

3 3 male 27 86625 0

4 3 female 22 122875 1

... ... ... ... ... ...

412 3 male 55 805 0

413 1 female 39 1089 1

414 3 male 385 725 0

415 3 male 55 805 0

416 3 male 55 223583 0

417 rows × 5 columns

Survival=df["Survived"]
Survival
0 0
1 1
2 0
3 0
4 1
..
412 0
413 1
414 0
415 0
416 0
Name: Survived, Length: 417, dtype: int64

input=df["Pclass"]
input=df.drop("Survived",axis='columns')
input

Pclas
Gender Age Ticket
s

0 3 male 34 78292

femal
1 3 47 7
e

2 2 male 62 96875

3 3 male 27 86625

femal
4 3 22 122875
e

... ... ... ... ...

412 3 male 55 805

femal
413 1 39 1089
e

414 3 male 385 725

415 3 male 55 805

416 3 male 55 223583


417 rows × 4 columns

from sklearn.preprocessing import LabelEncoder


l1=LabelEncoder()
input["Gender_n"]=l1.fit_transform(input["Gender"])
input

Pclas Ticke
Gender Age Gender_n
s t

0 3 male 34 78292 1

1 3 female 47 7 0

2 2 male 62 96875 1

3 3 male 27 86625 1

4 3 female 22 122875 0

... ... ... ... ... ...

412 3 male 55 805 1

413 1 female 39 1089 0

414 3 male 385 725 1

415 3 male 55 805 1

416 3 male 55 223583 1

417 rows × 5 columns

input_n=input.drop(["Gender"],axis='columns')
input_n
Pclass Age Ticket Gender_n

0 3 34 78292 1

1 3 47 7 0

2 2 62 96875 1

3 3 27 86625 1

4 3 22 122875 0

... ... ... ... ...

412 3 55 805 1

413 1 39 1089 0

414 3 385 725 1

415 3 55 805 1

416 3 55 223583 1

417 rows × 4 columns

from sklearn import tree


from sklearn import svm
model=svm.SVC()
model.fit(input_n,Survival)
model.score(input_n,Survival)

OUTPUT: 0.6474820143884892

model.predict([[3,55,805,1]])
OUTPUT: array([0], dtype=int64)

You might also like