|
a |
|
b/BE-PROJECT-1MODEL/knn.py |
|
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
"""KNN.ipynb |
|
|
3 |
|
|
|
4 |
Automatically generated by Colaboratory. |
|
|
5 |
|
|
|
6 |
Original file is located at |
|
|
7 |
https://colab.research.google.com/drive/1p90EngR7skAHqdMPNlTz7xUU3Gan4k1S |
|
|
8 |
""" |
|
|
9 |
|
|
|
10 |
import pandas as pd |
|
|
11 |
import pickle |
|
|
12 |
|
|
|
13 |
df = pd.read_csv('upscale.csv') |
|
|
14 |
|
|
|
15 |
df.head(10) |
|
|
16 |
|
|
|
17 |
df = df.drop(columns="Hybridization REF") |
|
|
18 |
from sklearn.model_selection import train_test_split |
|
|
19 |
training_set, test_set = train_test_split(df, test_size = 0.2, random_state = 1) |
|
|
20 |
|
|
|
21 |
X_train = training_set.iloc[:,0:21].values |
|
|
22 |
Y_train = training_set.iloc[:,21].values |
|
|
23 |
|
|
|
24 |
X_test = test_set.iloc[:,0:21].values |
|
|
25 |
Y_test = test_set.iloc[:,21].values |
|
|
26 |
|
|
|
27 |
from sklearn.neighbors import KNeighborsClassifier |
|
|
28 |
knn = KNeighborsClassifier(n_neighbors=15) |
|
|
29 |
knn.fit(X_train,Y_train) |
|
|
30 |
|
|
|
31 |
y_pred = knn.predict(X_test) |
|
|
32 |
|
|
|
33 |
from sklearn.metrics import accuracy_score |
|
|
34 |
accuracy_score(Y_test,y_pred) |
|
|
35 |
|
|
|
36 |
from sklearn.metrics import precision_score, recall_score, f1_score |
|
|
37 |
precision = precision_score(Y_test,y_pred) |
|
|
38 |
print(precision) |
|
|
39 |
|
|
|
40 |
recall_score(Y_test, y_pred) |
|
|
41 |
|
|
|
42 |
f1_score(Y_test, y_pred) |
|
|
43 |
|
|
|
44 |
#serialization and de-serialization |
|
|
45 |
pickle.dump(knn, open('model1.pkl','wb')) |
|
|
46 |
|
|
|
47 |
model = pickle.load(open('model1.pkl', 'rb')) |