You are on page 1of 7

9/8/2020 KNN_example_1

KKN without Lib for Classification [Sweet and Sour]


In [1]: #Data set

In [2]: import numpy as np


import pandas as pd

In [6]: X_feature=np.array([[1,9],[2,8],[3,7],[2,9],[6,4],[9,1],[8,2],[9,1],[1,1],[5,5]])
Y_fruit_type=np.array(["Sour","Sour","Sour","Sour","Sweet","Sweet","Sweet","Sweet","None","Sour"])

In [5]: X_feature.shape
Out[5]: (10, 2)

In [7]: Y_fruit_type.shape

Out[7]: (10,)

In [9]: training_data=pd.DataFrame(X_feature,columns=["Sweetness","Sourness"])

In [11]: training_data["Fruit_type"]=Y_fruit_type

localhost:8888/nbconvert/html/Desktop/KNN_example_1.ipynb?download=false 1/
In [12]: training_data

Out[12]:
Sweetness Sourness Fruit_type

0 1 9 Sour

1 2 8 Sour

2 3 7 Sour

3 2 9 Sour

4 6 4 Sweet

5 9 1 Sweet

6 8 2 Sweet

7 9 1 Sweet

8 1 1 None

9 5 5 Sour

In [13]: from matplotlib import pyplot as plt

In [15]: un_sample=training_data.Fruit_type.value_counts()

In [30]: sample_value=list(un_sample.values) sample_name=list(un_sample.index)


In [33]: plt.bar(sample_name,sample_value) plt.xlabel("Fruit type")
plt.ylabel("Count") plt.show()

In [34]: import seaborn as sb


In [35]: sb.countplot("Fruit_type",data=training_data) plt.show()

In [36]: # Calclulate the distance from each sample

In [165]: sw=int(input("Please enter sweetness value")) so=int(input("Please enter sourness value"))


k=int(input("please select value of k for Classification"))

Please enter sweetness value1


Please enter sourness value1
please select value of k for Classification5

In [166]: test_sample=np.array([[sw,so]])

In [167]: test_sample

Out[167]: array([[1, 1]])

In [168]: #l2_norms
In [169]: SwFeature=training_data.iloc[:,0]
SourFeature=training_data.iloc[:,1]

In [170]: l2_norms=np.sqrt(np.square(SwFeature-sw)+np.square(SourFeature-so))

In [171]: training_data["Nearsest_Nei"]=l2_norms

In [172]: training_data
Out[172]:
Sweetness Sourness Fruit_type Nearsest_Nei

0 1 9 Sour 8.000000

1 2 8 Sour 7.071068

2 3 7 Sour 6.324555

3 2 9 Sour 8.062258

4 6 4 Sweet 5.830952

5 9 1 Sweet 8.000000

6 8 2 Sweet 7.071068

7 9 1 Sweet 8.000000

8 1 1 None 0.000000

9 5 5 Sour 5.656854

In [173]: #2step is arrange sample by NN

In [174]: arrange_table=training_data.sort_values(by="Nearsest_Nei",ascending=True)

In [175]: arrange_table=arrange_table.reset_index()
arrange_table=arrange_table.drop("index",axis=1)
In [176]: arrange_table

Out[176]:
Sweetness Sourness Fruit_type Nearsest_Nei

0 1 1 None 0.000000

1 5 5 Sour 5.656854

2 6 4 Sweet 5.830952

3 3 7 Sour 6.324555

4 2 8 Sour 7.071068

5 8 2 Sweet 7.071068

6 1 9 Sour 8.000000

7 9 1 Sweet 8.000000

8 9 1 Sweet 8.000000

9 2 9 Sour 8.062258

In [177]: clssification_result=arrange_table.iloc[:k,:]["Fruit_type"]

In [178]: clssification_result

Out[178]: 0 None
1 Sour
2 Sweet
3 Sour
4 Sour
Name: Fruit_type, dtype: object

In [180]: clssification_result.mode()

Out[180]: 0 Sour
dtype: object
by Sklearn Lib
In [181]: from sklearn.neighbors import KNeighborsClassifier

In [185]: trainer=KNeighborsClassifier(n_neighbors=1)

In [186]: learner=trainer.fit(X_feature,Y_fruit_type)

In [187]: learner.predict([[1,1]])

Out[187]: array(['None'], dtype='<U5')

In [ ]:

You might also like