In [ ]:
#exercise KNN Algorithm using Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
In [ ]:
df= pd.read_csv('o_m.csv')
df.head()
Out[ ]:
| weight | donut | result | |
|---|---|---|---|
| 0 | 20.409116 | 3 | malnourished |
| 1 | 22.551343 | 9 | malnourished |
| 2 | 63.816049 | 18 | obese |
| 3 | 57.820181 | 11 | obese |
| 4 | 49.767124 | 10 | obese |
In [ ]:
df['result'].value_counts()
Out[ ]:
| count | |
|---|---|
| result | |
| obese | 15 |
| malnourished | 8 |
In [ ]:
#exploratory analysis
sns.jointplot(data=df, x= 'donut', y='weight', kind= 'reg')
plt.show()
In [ ]:
plt.figure(figsize=(5,4))
sns.scatterplot(data=df, x='donut', y='weight', hue='result',
s=50, palette='hls')
plt.show()
In [ ]:
#model building
In [ ]:
train_x= df.iloc[:, 0:2].values
train_x
train_y= df.iloc[:, -1].values
train_y
Out[ ]:
array(['malnourished', 'malnourished', 'obese', 'obese', 'obese', 'obese',
'obese', 'malnourished', 'malnourished', 'malnourished', 'obese',
'obese', 'obese', 'obese', 'obese', 'obese', 'obese',
'malnourished', 'malnourished', 'obese', 'obese', 'malnourished',
'obese'], dtype=object)
In [ ]:
#model
model= KNeighborsClassifier(n_neighbors=5)
In [ ]:
#train the model
model.fit(train_x, train_y)
Out[ ]:
KNeighborsClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
KNeighborsClassifier()
In [ ]:
#new samples
mickey1 = [[40, 20]]
mickey2= [[4,2]]
In [ ]:
prediction1 = model.predict(mickey1)
prediction2= model.predict(mickey2)
print(prediction1, prediction2)
['obese'] ['malnourished']
In [ ]:
!jupyter nbconvert --to html bana410_092525.ipynb
[NbConvertApp] Converting notebook bana410_092525.ipynb to html [NbConvertApp] WARNING | Alternative text is missing on 2 image(s). [NbConvertApp] Writing 379847 bytes to bana410_092525.html
In [ ]: