-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.knn_iris.py
28 lines (22 loc) · 1023 Bytes
/
1.knn_iris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 12 19:48:47 2019
@author: Salam Saudagar
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris_data = pd.read_csv("file:///E:/py_practice/Iris.csv")
sns.pairplot(iris_data.drop(labels=['Id'], axis = 1), hue='Species')
x_train, x_test, y_train, y_test = train_test_split(iris_data[['SepalLengthCm', 'SepalWidthCm',
'PetalLengthCm', 'PetalWidthCm']],
iris_data['Species'], random_state= 0)
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(x_train, y_train)
y_pred = knn.predict(x_test)
pd.concat([x_test, y_test, pd.Series(y_pred, name='Predicted', index=x_test.index)],
ignore_index=False, axis=1)
print("Test set score: {:.2f}".format(knn.score(x_test, y_test)))