Anda di halaman 1dari 1

In [1]: import numpy as np

import matplotlib.pyplot as plt


import seaborn as sns
import pandas as pd
%matplotlib inline

In [8]: df = pd.read_csv('iris.data')

In [9]: df.head()

Out[9]:
5.1 3.5 1.4 0.2 Iris-setosa

0 4.9 3.0 1.4 0.2 Iris-setosa

1 4.7 3.2 1.3 0.2 Iris-setosa

2 4.6 3.1 1.5 0.2 Iris-setosa

3 5.0 3.6 1.4 0.2 Iris-setosa

4 5.4 3.9 1.7 0.4 Iris-setosa

In [10]: col = ['sepal length','sepal width', 'petal length', 'petal width', 'class']

In [11]: df.columns=col

In [12]: df.head()

Out[12]:
sepal length sepal width petal length petal width class

0 4.9 3.0 1.4 0.2 Iris-setosa

1 4.7 3.2 1.3 0.2 Iris-setosa

2 4.6 3.1 1.5 0.2 Iris-setosa

3 5.0 3.6 1.4 0.2 Iris-setosa

4 5.4 3.9 1.7 0.4 Iris-setosa

In [13]: x = df.iloc[:,0:4]
x.head()

Out[13]:
sepal length sepal width petal length petal width

0 4.9 3.0 1.4 0.2

1 4.7 3.2 1.3 0.2

2 4.6 3.1 1.5 0.2

3 5.0 3.6 1.4 0.2

4 5.4 3.9 1.7 0.4

In [14]: y = df.iloc[:,4:]
y.head()

Out[14]:
class

0 Iris-setosa

1 Iris-setosa

2 Iris-setosa

3 Iris-setosa

4 Iris-setosa

In [15]: from sklearn.model_selection import train_test_split

In [16]: x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.4, rand


om_state= 5)

In [17]: from sklearn.neighbors import KNeighborsClassifier

For: n_neighbors = 5

metric : 'manhattan'

In [20]: model = KNeighborsClassifier(n_neighbors = 5, metric='manhattan')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[20]: array([[19, 0, 0],


[ 0, 21, 2],
[ 0, 0, 18]], dtype=int64)

In [21]: accuracy_score(y_test,pred)

Out[21]: 0.9666666666666667

metric : 'hamming'

In [22]: model = KNeighborsClassifier(n_neighbors = 5, metric='hamming')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[22]: array([[19, 0, 0],


[ 1, 21, 1],
[ 4, 8, 6]], dtype=int64)

In [23]: accuracy_score(y_test,pred)

Out[23]: 0.7666666666666667

metric : 'euclidean'

In [29]: model = KNeighborsClassifier(n_neighbors = 5, metric='euclidean')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[29]: array([[19, 0, 0],


[ 0, 21, 2],
[ 0, 0, 18]], dtype=int64)

In [30]: accuracy_score(y_test,pred)

Out[30]: 0.9666666666666667

metric : 'minkowski' , power = 3

In [32]: model = KNeighborsClassifier(n_neighbors = 5, metric='minkowski', p=3)


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[32]: array([[19, 0, 0],


[ 0, 21, 2],
[ 0, 0, 18]], dtype=int64)

In [33]: accuracy_score(y_test,pred)

Out[33]: 0.9666666666666667

For: n_neighbors = 7

metric : 'manhattan'

In [35]: model = KNeighborsClassifier(n_neighbors = 7, metric='manhattan')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[35]: array([[19, 0, 0],


[ 0, 22, 1],
[ 0, 0, 18]], dtype=int64)

In [36]: accuracy_score(y_test,pred)

Out[36]: 0.9833333333333333

metric : 'hamming'

In [37]: model = KNeighborsClassifier(n_neighbors = 7, metric='hamming')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[37]: array([[19, 0, 0],


[ 1, 21, 1],
[ 5, 5, 8]], dtype=int64)

In [38]: accuracy_score(y_test,pred)

Out[38]: 0.8

metric : 'euclidean'

In [39]: model = KNeighborsClassifier(n_neighbors = 7, metric='euclidean')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[39]: array([[19, 0, 0],


[ 0, 22, 1],
[ 0, 0, 18]], dtype=int64)

In [40]: accuracy_score(y_test,pred)

Out[40]: 0.9833333333333333

metric : 'minkowski' , power = 3

In [41]: model = KNeighborsClassifier(n_neighbors = 7, metric='minkowski',p=3)


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[41]: array([[19, 0, 0],


[ 0, 22, 1],
[ 0, 0, 18]], dtype=int64)

In [42]: accuracy_score(y_test,pred)

Out[42]: 0.9833333333333333

For: n_neighbors = 3

metric : 'manhattan'

In [43]: model = KNeighborsClassifier(n_neighbors = 3, metric='manhattan')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[43]: array([[19, 0, 0],


[ 0, 20, 3],
[ 0, 0, 18]], dtype=int64)

In [44]: accuracy_score(y_test,pred)

Out[44]: 0.95

metric : 'hamming'

In [46]: model = KNeighborsClassifier(n_neighbors = 3, metric='hamming')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[46]: array([[18, 1, 0],


[ 2, 20, 1],
[ 6, 7, 5]], dtype=int64)

In [47]: accuracy_score(y_test,pred)

Out[47]: 0.7166666666666667

metric : 'euclidean'

In [49]: model = KNeighborsClassifier(n_neighbors = 3, metric='euclidean')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[49]: array([[19, 0, 0],


[ 0, 20, 3],
[ 0, 0, 18]], dtype=int64)

In [50]: accuracy_score(y_test,pred)

Out[50]: 0.95

metric : 'minkowski' , power = 3

In [51]: model = KNeighborsClassifier(n_neighbors = 3, metric='minkowski',p=3)


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[51]: array([[19, 0, 0],


[ 0, 20, 3],
[ 0, 0, 18]], dtype=int64)

In [52]: accuracy_score(y_test,pred)

Out[52]: 0.95

For: n_neighbors = 1

metric : 'manhattan'

In [56]: model = KNeighborsClassifier(n_neighbors = 1, metric='manhattan')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[56]: array([[19, 0, 0],


[ 0, 20, 3],
[ 0, 0, 18]], dtype=int64)

In [57]: accuracy_score(y_test,pred)

Out[57]: 0.95

metric : 'hamming'

In [58]: model = KNeighborsClassifier(n_neighbors = 1, metric='hamming')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[58]: array([[18, 1, 0],


[ 0, 20, 3],
[ 4, 2, 12]], dtype=int64)

In [59]: accuracy_score(y_test,pred)

Out[59]: 0.8333333333333334

metric : 'euclidean'

In [60]: model = KNeighborsClassifier(n_neighbors = 1, metric='euclidean')


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[60]: array([[19, 0, 0],


[ 0, 20, 3],
[ 0, 0, 18]], dtype=int64)

In [61]: accuracy_score(y_test,pred)

Out[61]: 0.95

metric : 'minkowski' , power = 3

In [62]: model = KNeighborsClassifier(n_neighbors = 1, metric='minkowski',p=3)


model.fit(x_train, y_train)
pred = model.predict(x_test)

from sklearn.metrics import confusion_matrix, accuracy_score


confusion_matrix(y_test,pred)

C:\Users\Saili\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DataCon
versionWarning: A column-vector y was passed when a 1d array was expected.
Please change the shape of y to (n_samples, ), for example using ravel().

Out[62]: array([[19, 0, 0],


[ 0, 20, 3],
[ 0, 0, 18]], dtype=int64)

In [63]: accuracy_score(y_test,pred)

Out[63]: 0.95

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

In [ ]:

Anda mungkin juga menyukai