Create new file
Browse files- diabetes-model-pl.py +36 -0
diabetes-model-pl.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importing essential libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle
|
| 5 |
+
|
| 6 |
+
# Loading the dataset
|
| 7 |
+
df = pd.read_csv('plashkar/diabetes-db')
|
| 8 |
+
|
| 9 |
+
# Renaming DiabetesPedigreeFunction as DPF
|
| 10 |
+
df = df.rename(columns={'DiabetesPedigreeFunction':'DPF'})
|
| 11 |
+
|
| 12 |
+
# Replacing the 0 values from ['Glucose','BloodPressure','SkinThickness','Insulin','BMI'] by NaN
|
| 13 |
+
df_copy = df.copy(deep=True)
|
| 14 |
+
df_copy[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']] = df_copy[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']].replace(0,np.NaN)
|
| 15 |
+
|
| 16 |
+
# Replacing NaN value by mean, median depending upon distribution
|
| 17 |
+
df_copy['Glucose'].fillna(df_copy['Glucose'].mean(), inplace=True)
|
| 18 |
+
df_copy['BloodPressure'].fillna(df_copy['BloodPressure'].mean(), inplace=True)
|
| 19 |
+
df_copy['SkinThickness'].fillna(df_copy['SkinThickness'].median(), inplace=True)
|
| 20 |
+
df_copy['Insulin'].fillna(df_copy['Insulin'].median(), inplace=True)
|
| 21 |
+
df_copy['BMI'].fillna(df_copy['BMI'].median(), inplace=True)
|
| 22 |
+
|
| 23 |
+
# Model Building
|
| 24 |
+
from sklearn.model_selection import train_test_split
|
| 25 |
+
X = df.drop(columns='Outcome')
|
| 26 |
+
y = df['Outcome']
|
| 27 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0)
|
| 28 |
+
|
| 29 |
+
# Creating Random Forest Model
|
| 30 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 31 |
+
classifier = RandomForestClassifier(n_estimators=20)
|
| 32 |
+
classifier.fit(X_train, y_train)
|
| 33 |
+
|
| 34 |
+
# Creating a pickle file for the classifier
|
| 35 |
+
filename = 'diabetes-prediction-rfc-model.pkl'
|
| 36 |
+
pickle.dump(classifier, open(filename, 'wb'))
|