Galle, LK--:--5/6 sites live

Multiple disease prediction system - Parkinsons

Multi-disease system එක සම්පූර්ණ කරන, තෙවෙනි disease module එක — දැන් වෙනකොට deployment pattern එක, classifier ඕනම එකකට run කරන්න පුළුවන් checklist එකක් වගේ දැනිය යුතුයි.

The third disease module, completing the multi-disease system — by now the deployment pattern should feel like a checklist you can run on almost any classifier.

19 May 20265 min readSelf Guide · Stage 17#Shipping Models to the World#Multiple Disease Prediction System#Machine learning

Lesson 56 of 64 · Part 5 · Shipping Models to the World · Multiple Disease Prediction System · bilingual notes (English + සිංහල)

Why this matters. The third disease module, completing the multi-disease system — by now the deployment pattern should feel like a checklist you can run on almost any classifier.

සිංහලෙන්: Multi-disease system එක සම්පූර්ණ කරන, තෙවෙනි disease module එක — දැන් වෙනකොට deployment pattern එක, classifier ඕනම එකකට run කරන්න පුළුවන් checklist එකක් වගේ දැනිය යුතුයි.

What you'll use this for. Voice and movement-pattern features (used for Parkinson's detection) show that this exact same pipeline works regardless of what the input data actually represents.

සිංහලෙන්: Parkinson's detection එකට පාවිච්චි කරන voice සහ movement-pattern features වලින් පේනවා, input data එකෙන් ඇත්තටම නියෝජනය කරන්නෙ මොකක් උනත් මේ එකම pipeline එක වැඩ කරනවා කියලා.

Key principles

  • A generalizable pipeline is judged by how little changes between projects, not how much. If deploying this model needed a completely different app structure from the diabetes one, that would be a sign your first design wasn't actually reusable.
    Generalize වෙන pipeline එකක්, projects අතර වෙනස් වෙන්නෙ අඩුවෙන් තරමින් judge වෙනවා, වැඩියෙන් නෙවෙයි

Walkthrough

Importing the Dependencies

ExamplePython
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_score

Data Collection & Analysis

ExamplePython
# loading the data from csv file to a Pandas DataFrame
parkinsons_data = pd.read_csv('/content/parkinsons.csv')
ExamplePython
# printing the first 5 rows of the dataframe
parkinsons_data.head()
nameMDVP:Fo(Hz)MDVP:Fhi(Hz)MDVP:Flo(Hz)MDVP:Jitter(%)MDVP:Jitter(Abs)MDVP:RAP…
0phon_R01_S01_1119.992157.30274.9970.007840.000070.00370…
1phon_R01_S01_2122.400148.650113.8190.009680.000080.00465…
2phon_R01_S01_3116.682131.111111.5550.010500.000090.00544…
3phon_R01_S01_4116.676137.871111.3660.009970.000090.00502…
4phon_R01_S01_5116.014141.781110.6550.012840.000110.00655…
ExamplePython
# number of rows and columns in the dataframe
parkinsons_data.shape

Output

Output
(195, 24)
ExamplePython
# getting more information about the dataset
parkinsons_data.info()

Output

Output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 195 entries, 0 to 194
Data columns (total 24 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   name              195 non-null    object 
 1   MDVP:Fo(Hz)       195 non-null    float64
 2   MDVP:Fhi(Hz)      195 non-null    float64
 3   MDVP:Flo(Hz)      195 non-null    float64
 4   MDVP:Jitter(%)    195 non-null    float64
 5   MDVP:Jitter(Abs)  195 non-null    float64
 6   MDVP:RAP          195 non-null    float64
 7   MDVP:PPQ          195 non-null    float64
 8   Jitter:DDP        195 non-null    float64
 9   MDVP:Shimmer      195 non-null    float64
 10  MDVP:Shimmer(dB)  195 non-null    float64
 11  Shimmer:APQ3      195 non-null    float64
 12  Shimmer:APQ5      195 non-null    float64
… (13 more lines)
ExamplePython
# checking for missing values in each column
parkinsons_data.isnull().sum()

Output

Output
name                0
MDVP:Fo(Hz)         0
MDVP:Fhi(Hz)        0
MDVP:Flo(Hz)        0
MDVP:Jitter(%)      0
MDVP:Jitter(Abs)    0
MDVP:RAP            0
MDVP:PPQ            0
Jitter:DDP          0
MDVP:Shimmer        0
MDVP:Shimmer(dB)    0
Shimmer:APQ3        0
Shimmer:APQ5        0
MDVP:APQ            0
Shimmer:DDA         0
NHR                 0
HNR                 0
status              0
… (7 more lines)
ExamplePython
# getting some statistical measures about the data
parkinsons_data.describe()
MDVP:Fo(Hz)MDVP:Fhi(Hz)MDVP:Flo(Hz)MDVP:Jitter(%)MDVP:Jitter(Abs)MDVP:RAPMDVP:PPQ…
count195.000000195.000000195.000000195.000000195.000000195.000000195.000000…
mean154.228641197.104918116.3246310.0062200.0000440.0033060.003446…
std41.39006591.49154843.5214130.0048480.0000350.0029680.002759…
min88.333000102.14500065.4760000.0016800.0000070.0006800.000920…
25%117.572000134.86250084.2910000.0034600.0000200.0016600.001860…
50%148.790000175.829000104.3150000.0049400.0000300.0025000.002690…

… 2 more rows

ExamplePython
# distribution of target Variable
parkinsons_data['status'].value_counts()

Output

Output
1    147
0     48
Name: status, dtype: int64

1 --> Parkinson's Positive

0 --> Healthy

ExamplePython
# grouping the data bas3ed on the target variable
parkinsons_data.groupby('status').mean()
MDVP:Fo(Hz)MDVP:Fhi(Hz)MDVP:Flo(Hz)MDVP:Jitter(%)MDVP:Jitter(Abs)MDVP:RAPMDVP:PPQ…
status…
0181.937771223.636750145.2072920.0038660.0000230.0019250.002056…
1145.180762188.441463106.8935580.0069890.0000510.0037570.003900…

Data Pre-Processing

Separating the features & Target

ExamplePython
X = parkinsons_data.drop(columns=['name','status'], axis=1)
Y = parkinsons_data['status']
ExamplePython
print(X)

Output

Output
     MDVP:Fo(Hz)  MDVP:Fhi(Hz)  MDVP:Flo(Hz)  MDVP:Jitter(%)  \
0        119.992       157.302        74.997         0.00784   
1        122.400       148.650       113.819         0.00968   
2        116.682       131.111       111.555         0.01050   
3        116.676       137.871       111.366         0.00997   
4        116.014       141.781       110.655         0.01284   
..           ...           ...           ...             ...   
190      174.188       230.978        94.261         0.00459   
191      209.516       253.017        89.488         0.00564   
192      174.688       240.005        74.287         0.01360   
193      198.764       396.961        74.904         0.00740   
194      214.289       260.277        77.973         0.00567   

     MDVP:Jitter(Abs)  MDVP:RAP  MDVP:PPQ  Jitter:DDP  MDVP:Shimmer  \
0             0.00007   0.00370   0.00554     0.01109       0.04374   
1             0.00008   0.00465   0.00696     0.01394       0.06134   
2             0.00009   0.00544   0.00781     0.01633       0.05233   
3             0.00009   0.00502   0.00698     0.01505       0.05492   
… (35 more lines)
ExamplePython
print(Y)

Output

Output
0      1
1      1
2      1
3      1
4      1
      ..
190    0
191    0
192    0
193    0
194    0
Name: status, Length: 195, dtype: int64

Splitting the data to training data & Test data

ExamplePython
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)
ExamplePython
print(X.shape, X_train.shape, X_test.shape)

Output

Output
(195, 22) (156, 22) (39, 22)

Model Training

Support Vector Machine Model

ExamplePython
model = svm.SVC(kernel='linear')
ExamplePython
# training the SVM model with training data
model.fit(X_train, Y_train)

Output

Output
SVC(kernel='linear')

Model Evaluation

Accuracy Score

ExamplePython
# accuracy score on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(Y_train, X_train_prediction)
ExamplePython
print('Accuracy score of training data : ', training_data_accuracy)

Output

Output
Accuracy score of training data :  0.8717948717948718
ExamplePython
# accuracy score on training data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(Y_test, X_test_prediction)
ExamplePython
print('Accuracy score of test data : ', test_data_accuracy)

Output

Output
Accuracy score of test data :  0.8717948717948718

Building a Predictive System

ExamplePython
input_data = (197.07600,206.89600,192.05500,0.00289,0.00001,0.00166,0.00168,0.00498,0.01098,0.09700,0.00563,0.00680,0.00802,0.01689,0.00339,26.77500,0.422229,0.741367,-7.348300,0.177551,1.743867,0.085569)

# changing input data to a numpy array
input_data_as_numpy_array = np.asarray(input_data)

# reshape the numpy array
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)

prediction = model.predict(input_data_reshaped)
print(prediction)

if (prediction[0] == 0):
  print("The Person does not have Parkinsons Disease")

else:
  print("The Person has Parkinsons")

Output

Output
[0]
The Person does not have Parkinsons Disease

Output

Output
/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

ExamplePython
import pickle
ExamplePython
filename = 'parkinsons_model.sav'
pickle.dump(model, open(filename, 'wb'))
ExamplePython
# loading the saved model
loaded_model = pickle.load(open('parkinsons_model.sav', 'rb'))
ExamplePython
for column in X.columns:
  print(column)

Output

Output
MDVP:Fo(Hz)
MDVP:Fhi(Hz)
MDVP:Flo(Hz)
MDVP:Jitter(%)
MDVP:Jitter(Abs)
MDVP:RAP
MDVP:PPQ
Jitter:DDP
MDVP:Shimmer
MDVP:Shimmer(dB)
Shimmer:APQ3
Shimmer:APQ5
MDVP:APQ
Shimmer:DDA
NHR
HNR
RPDE
DFA
… (4 more lines)

Keep going: ← Lesson 55 · Lesson 57 → · Track it in the Self Guide