KNN ALGORITHM - Ipynb - Colab
KNN ALGORITHM - Ipynb - Colab
ipynb -
AM Colab
Mounted at /content/drive
import numpy as
np import pandas
as pd
dataset.shape
(150, 6)
dataset.describe()
# Let’s now take a look at the number of instances (rows) that belong to each class. We can view this as an absolute count.
dataset.groupby('Species').size()
Species
Iris-setosa 50
Iris-versicolor 50
Iris-virginica 50
dtype: int64
X = dataset.iloc[:,
1:5].values y =
dataset.iloc[:, 5].values
plt.figure()
sns.pairplot(dataset.drop("Id", axis=1), hue = "Species", size=3, markers=["o", "s", "D"])
plt.show()
https://colab.research.google.com/drive/ 1/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4
1/29/25, 9:49 KNN ALGORITHM.ipynb -
AM Colab
/usr/local/lib/python3.11/dist-packages/seaborn/axisgrid.py:2100: UserWarning: The `size` parameter has been renamed to
`height`; warnings.warn(msg, UserWarning)
<Figure size 640x480 with 0 Axes>
plt.figure()
dataset.drop("Id", axis=1).boxplot(by="Species", figsize=(15,
10)) plt.show()
https://colab.research.google.com/drive/ 2/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4
1/29/25, 9:49 KNN ALGORITHM.ipynb -
AM Colab
<Figure size 640x480 with 0 Axes>
▾ i ?
KNeighborsClassifier(n_neighbors=
50)
https://colab.research.google.com/drive/ 3/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4
1/29/25, 9:49 KNN ALGORITHM.ipynb -
AM # Make predictions on the test Colab
set y_pred = knn.predict(X_test)
knn = KNeighborsClassifier(n_neighbors=3)
▾ KNeighborsClassifier i ?
KNeighborsClassifier(n_neighbors=
3)
https://colab.research.google.com/drive/ 4/
1JqVsKd0IjWMC0yvZBCs7HcDrzaBVSqFx#scrollTo=TzFVc2ocSDuj&printMode=true 4