L6 Tutorial - KNN - Jupyter Notebook
L6 Tutorial - KNN - Jupyter Notebook
In [2]: 1 iris.feature_names
In [3]: 1 iris.target_names
In [4]: 1 df = pd.DataFrame(iris.data,columns=iris.feature_names)
2 df.head()
Out[4]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
Out[5]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
Out[6]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
In [7]: 1 df[df.target==2].head()
Out[7]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
Out[8]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target flower_name
Out[9]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target flower_name
In [16]: , X_test,
1 y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
In [17]: 1 len(X_train)
Out[17]: 120
In [18]: 1 len(X_test)
Out[18]: 30
Out[19]: KNeighborsClassifier(n_neighbors=10)
Out[20]: 0.9666666666666667
In [21]: 1 knn.predict([[4.8,3.0,1.5,0.3]])
Out[21]: array([0])
Print classification report for precesion, recall and f1-score for each
classes
accuracy 0.97 30
macro avg 0.95 0.97 0.96 30
weighted avg 0.97 0.97 0.97 30