0% found this document useful (0 votes)
11 views4 pages

Classification Demo

Uploaded by

rxn114392
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)
11 views4 pages

Classification Demo

Uploaded by

rxn114392
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/ 4

classification-demo

April 22, 2024

[ ]: import pandas as pd
url = "https://raw.githubusercontent.com/LahiruTjay/
↪Machine-Learning-With-Python/master/datasets/diabetes.csv"

data = pd.read_csv(url, sep = ',')

[ ]: data.head()

[ ]: Pregnancies Glucose BloodPressure … DiabetesPedigreeFunction Age


Outcome
0 6 148 72 … 0.627 50
1
1 1 85 66 … 0.351 31
0
2 8 183 64 … 0.672 32
1
3 1 89 66 … 0.167 21
0
4 0 137 40 … 2.288 33
1

[5 rows x 9 columns]

[ ]: import numpy as np
data_array = np.asarray(data)

[ ]: data_array.shape

[ ]: (768, 9)

[ ]: feature = data_array[:,0:8 ]

[ ]: label = data_array[:,-1]

[ ]: from sklearn.model_selection import train_test_split


x_train, x_test, y_train, y_test = train_test_split(feature, label, test_size =␣
↪0.20, random_state= 42)

1
[ ]: print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

(614, 8) (154, 8) (614,) (154,)

[ ]: from sklearn.metrics import confusion_matrix, plot_confusion_matrix,␣


↪accuracy_score, classification_report

from sklearn.naive_bayes import GaussianNB

naive = GaussianNB()
naive.fit(x_train,y_train)
y_pred_naive = naive.predict(x_test)
print("Naive Bayes")
print("Accuracy score =", accuracy_score(y_test, y_pred_naive))
print(classification_report(y_test, y_pred_naive ))
plot_confusion_matrix(naive, x_test, y_test, normalize='true')

Naive Bayes
Accuracy score = 0.7662337662337663
precision recall f1-score support

0.0 0.83 0.80 0.81 99


1.0 0.66 0.71 0.68 55

accuracy 0.77 154


macro avg 0.75 0.75 0.75 154
weighted avg 0.77 0.77 0.77 154

/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87:
FutureWarning: Function plot_confusion_matrix is deprecated; Function
`plot_confusion_matrix` is deprecated in 1.0 and will be removed in 1.2. Use one
of the class methods: ConfusionMatrixDisplay.from_predictions or
ConfusionMatrixDisplay.from_estimator.
warnings.warn(msg, category=FutureWarning)

[ ]: <sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at
0x7f0983686c10>

2
[ ]: from sklearn.tree import DecisionTreeClassifier
decision = DecisionTreeClassifier()
decision.fit(x_train,y_train)
y_pred_decision = decision.predict(x_test)
print("Decision Tree")
print("Accuracy score =", accuracy_score(y_test, y_pred_decision))
print(classification_report(y_test, y_pred_decision ))
plot_confusion_matrix(decision, x_test, y_test, normalize='true')

Decision Tree
Accuracy score = 0.7467532467532467
precision recall f1-score support

0.0 0.83 0.77 0.80 99


1.0 0.63 0.71 0.67 55

accuracy 0.75 154


macro avg 0.73 0.74 0.73 154
weighted avg 0.76 0.75 0.75 154

/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87:
FutureWarning: Function plot_confusion_matrix is deprecated; Function
`plot_confusion_matrix` is deprecated in 1.0 and will be removed in 1.2. Use one
of the class methods: ConfusionMatrixDisplay.from_predictions or

3
ConfusionMatrixDisplay.from_estimator.
warnings.warn(msg, category=FutureWarning)

[ ]: <sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at
0x7f09836acbd0>

[ ]:

You might also like