Multiple disease prediction system - diabetes
Part 1 එකේදී හදපු diabetes model එක, ඇත්තටම පාවිච්චි කරන්න පුළුවන් interface එකකට package කරනවා මේකෙන් — course project එකක්, ඇත්තටම වැඩ කරන tool එකක් වෙන්න පුළුවන් කියන සාක්ෂියක්.
This packages the diabetes model you built back in Part 1 into a real, usable interface — proof that a course project can become an actual working tool.
Lesson 54 of 64 · Part 5 · Shipping Models to the World · Multiple Disease Prediction System · bilingual notes (English + සිංහල)
Why this matters. This packages the diabetes model you built back in Part 1 into a real, usable interface — proof that a course project can become an actual working tool.
සිංහලෙන්: Part 1 එකේදී හදපු diabetes model එක, ඇත්තටම පාවිච්චි කරන්න පුළුවන් interface එකකට package කරනවා මේකෙන් — course project එකක්, ඇත්තටම වැඩ කරන tool එකක් වෙන්න පුළුවන් කියන සාක්ෂියක්.
What you'll use this for. This exact building block later plugs into a single app covering three diseases at once — the pattern here is what makes that combination possible.
සිංහලෙන්: රෝග තුනක් එකපාරටම cover කරන app එකකට, මේ එකම building block එක පස්සේ ins ගැහෙනවා — ඒ combination එක කරන්න පුළුවන් වෙන්නෙ මෙතන තියෙන pattern එකෙන්.
Key principles
- The app must collect input in exactly the same order the model was trained on. If "glucose" and "blood pressure" are swapped in the input form compared to training, the model will silently make wrong predictions — no error message, just quietly bad answers.
Model එක train කරාපු ම order එකෙන්ම app එකෙන් input එක ගන්න ඕන
Walkthrough
Importing the Dependencies
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_scoreData Collection and Analysis
PIMA Diabetes Dataset
# loading the diabetes dataset to a pandas DataFrame
diabetes_dataset = pd.read_csv('/content/diabetes.csv')# printing the first 5 rows of the dataset
diabetes_dataset.head()| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | … | |
|---|---|---|---|---|---|---|---|---|
| 0 | 6 | 148 | 72 | 35 | 0 | 33.6 | 0.627 | … |
| 1 | 1 | 85 | 66 | 29 | 0 | 26.6 | 0.351 | … |
| 2 | 8 | 183 | 64 | 0 | 0 | 23.3 | 0.672 | … |
| 3 | 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | … |
| 4 | 0 | 137 | 40 | 35 | 168 | 43.1 | 2.288 | … |
# number of rows and Columns in this dataset
diabetes_dataset.shapeOutput
(768, 9)# getting the statistical measures of the data
diabetes_dataset.describe()| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | … | |
|---|---|---|---|---|---|---|---|---|
| count | 768.000000 | 768.000000 | 768.000000 | 768.000000 | 768.000000 | 768.000000 | 768.000000 | … |
| mean | 3.845052 | 120.894531 | 69.105469 | 20.536458 | 79.799479 | 31.992578 | 0.471876 | … |
| std | 3.369578 | 31.972618 | 19.355807 | 15.952218 | 115.244002 | 7.884160 | 0.331329 | … |
| min | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.078000 | … |
| 25% | 1.000000 | 99.000000 | 62.000000 | 0.000000 | 0.000000 | 27.300000 | 0.243750 | … |
| 50% | 3.000000 | 117.000000 | 72.000000 | 23.000000 | 30.500000 | 32.000000 | 0.372500 | … |
… 2 more rows
diabetes_dataset['Outcome'].value_counts()Output
0 500
1 268
Name: Outcome, dtype: int640 --> Non-Diabetic
1 --> Diabetic
diabetes_dataset.groupby('Outcome').mean()| Pregnancies | Glucose | BloodPressure | SkinThickness | Insulin | BMI | DiabetesPedigreeFunction | Age | |
|---|---|---|---|---|---|---|---|---|
| Outcome | ||||||||
| 0 | 3.298000 | 109.980000 | 68.184000 | 19.664000 | 68.792000 | 30.304200 | 0.429734 | 31.190000 |
| 1 | 4.865672 | 141.257463 | 70.824627 | 22.164179 | 100.335821 | 35.142537 | 0.550500 | 37.067164 |
# separating the data and labels
X = diabetes_dataset.drop(columns = 'Outcome', axis=1)
Y = diabetes_dataset['Outcome']print(X)Output
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \
0 6 148 72 35 0 33.6
1 1 85 66 29 0 26.6
2 8 183 64 0 0 23.3
3 1 89 66 23 94 28.1
4 0 137 40 35 168 43.1
.. ... ... ... ... ... ...
763 10 101 76 48 180 32.9
764 2 122 70 27 0 36.8
765 5 121 72 23 112 26.2
766 1 126 60 0 0 30.1
767 1 93 70 31 0 30.4
DiabetesPedigreeFunction Age
0 0.627 50
1 0.351 31
2 0.672 32
3 0.167 21
… (9 more lines)print(Y)Output
0 1
1 0
2 1
3 0
4 1
..
763 0
764 0
765 0
766 1
767 0
Name: Outcome, Length: 768, dtype: int64Train Test Split
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.2, stratify=Y, random_state=2)print(X.shape, X_train.shape, X_test.shape)Output
(768, 8) (614, 8) (154, 8)Training the Model
classifier = svm.SVC(kernel='linear')#training the support vector Machine Classifier
classifier.fit(X_train, Y_train)Output
SVC(kernel='linear')Model Evaluation
Accuracy Score
# accuracy score on the training data
X_train_prediction = classifier.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)print('Accuracy score of the training data : ', training_data_accuracy)Output
Accuracy score of the training data : 0.7833876221498371# accuracy score on the test data
X_test_prediction = classifier.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)print('Accuracy score of the test data : ', test_data_accuracy)Output
Accuracy score of the test data : 0.7727272727272727Making a Predictive System
input_data = (5,166,72,19,175,25.8,0.587,51)
# changing the input_data to numpy array
input_data_as_numpy_array = np.asarray(input_data)
# reshape the array as we are predicting for one instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)
prediction = classifier.predict(input_data_reshaped)
print(prediction)
if (prediction[0] == 0):
print('The person is not diabetic')
else:
print('The person is diabetic')Output
[1]
The person is diabeticOutput
/usr/local/lib/python3.7/dist-packages/sklearn/base.py:451: UserWarning: X does not have valid feature names, but SVC was fitted with feature names
"X does not have valid feature names, but"Saving the trained model
import picklefilename = 'diabetes_model.sav'
pickle.dump(classifier, open(filename, 'wb'))# loading the saved model
loaded_model = pickle.load(open('diabetes_model.sav', 'rb'))input_data = (5,166,72,19,175,25.8,0.587,51)
# changing the input_data to numpy array
input_data_as_numpy_array = np.asarray(input_data)
# reshape the array as we are predicting for one instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)
prediction = loaded_model.predict(input_data_reshaped)
print(prediction)
if (prediction[0] == 0):
print('The person is not diabetic')
else:
print('The person is diabetic')Output
[1]
The person is diabeticOutput
/usr/local/lib/python3.7/dist-packages/sklearn/base.py:451: UserWarning: X does not have valid feature names, but SVC was fitted with feature names
"X does not have valid feature names, but"for column in X.columns:
print(column)Output
Pregnancies
Glucose
BloodPressure
SkinThickness
Insulin
BMI
DiabetesPedigreeFunction
AgeKeep going: ← Lesson 53 · Lesson 55 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 17: ML 5 — Shipping Models to the World. Progress is saved in this browser.



