-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
29 lines (23 loc) · 898 Bytes
/
model.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
29
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
import pickle
# Load dataset
df = pd.read_csv('./Assets/diabetes.csv')
# Splitting the dataset into features and target variable
X = df.drop('Outcome', axis=1)
y = df['Outcome']
# Splitting data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardizing the data
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
# Training a KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_scaled, y_train)
# Saving the model and scaler to disk
with open('knn_model.pkl', 'wb') as model_file:
pickle.dump(knn, model_file)
with open('scaler.pkl', 'wb') as scaler_file:
pickle.dump(scaler, scaler_file)