-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn.py
More file actions
57 lines (41 loc) · 1.55 KB
/
Copy pathknn.py
File metadata and controls
57 lines (41 loc) · 1.55 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pandas as pd
import numpy as np
import pickle
from src.preprocessing import featurize, prepare_data
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, \
f1_score
from src.feature_engineering import feature_engineering
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def main():
X_train, X_test, y_train, y_test, scaler = prepare_data()
run_model_knn(X_train, X_test, y_train, y_test)
# with open('scaler.pkl', 'wb') as f:
# pickle.dump(scaler, f, protocol=4)
def run_model_knn(X_train, X_test, y_train, y_test):
y_train = np.array(y_train).ravel()
y_test = np.array(y_test).ravel()
print('Running KNN')
model = knn(X_train, X_test, y_train, y_test)
print()
knn_save_pickle(model)
def knn(X_train, X_test, y_train, y_test):
# K Nearest Neighbors
model = KNeighborsClassifier(n_neighbors=4, weights='distance')
model.fit(X_train, y_train)
predicted = model.predict(X_test)
print('Accuracy: ', accuracy_score(y_test, predicted))
print('Precision: ', precision_score(y_test, predicted))
print('Recall: ', recall_score(y_test, predicted))
print('F1 score: ', f1_score(y_test, predicted))
return model
def knn_save_pickle(model):
# Save pickle file
output = open('knn_model.pkl', 'wb')
print('Pickle dump model')
pickle.dump(model, output, protocol=4)
output.close()
return
if __name__ == '__main__':
main()