# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="ug7Q7DElsGfU" colab_type="text"
# **PARKINSON'S DISEASE PREDICTION USING MACHINE LEARNING TECHNIQUES**

# + [markdown] id="QgvQmFAisPpl" colab_type="text"
# IMPORTING REQUIRED LIBRARIES

# + id="vN4EmIGim_uF" colab_type="code" colab={}
import numpy as np
import pandas as pd
import warnings
import os, sys
from sklearn.preprocessing import MinMaxScaler
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import seaborn as sns
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
from imblearn.over_sampling import SMOTE
from imblearn.combine import SMOTETomek
sm=SMOTE(random_state=2)
smk = SMOTETomek(random_state= 2)
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report,confusion_matrix

# + [markdown] id="wZocAleNsdr6" colab_type="text"
# LAUNCHING

# + id="IAMbx0pqnFdh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="1f3d46a2-0b0c-4ccc-e565-7b8d1171b2ae"
df=pd.read_csv('/content/parkinsons.data')
df.head()

# + [markdown] id="lj6NZa86tFcA" colab_type="text"
# REMOVING NON CATEGORICAL FEATURE BEFORE MOVING AHEAD

# + id="1v1UUuZ7tJRT" colab_type="code" colab={}
df = df.drop(['name'],axis = 1)

# + [markdown] id="dt3OCR9XshLp" colab_type="text"
# DATA TREATMENT

# + id="0rKsFlQ8nPcl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 371} outputId="b45dab01-5ce4-4fbc-d56d-f59ce6821d20"
#CHECKING FOR MISSING VALUES AND REPRESENTING IT USING HEAT MAPS
sns.heatmap(df.isnull(),cmap='coolwarm',xticklabels = True,yticklabels = False,cbar=False)

# + [markdown] id="-Yt22NFLthfE" colab_type="text"
# PREPARING x AND y

# + id="bP0sZPsrnfTh" colab_type="code" colab={}
y = df['status']
X = df.drop(['status'],axis =1 )

# + id="rp5UA_qyoID_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="e222a06f-f4bc-4cd3-e7aa-0a0fb6dd5eb6"
X.head()

# + id="TGsNznBaoL_z" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="2c17eb98-3020-44f1-85f7-24d5e5a6ce86"
y.head()

# + [markdown] id="JkvW3YH1ttTU" colab_type="text"
# SPLITTING DATA INTO TEST AND TRAIN

# + id="qBgsLZwRoNSO" colab_type="code" colab={}
X_train,X_test,y_train, y_test = train_test_split( X, y ,test_size = 0.3 , random_state = 123)


# + [markdown] id="sFrkYGm-t9Eb" colab_type="text"
# BALANCING DATA FOR IMPROVING CLASSIFICATION PERFORMANCE

# + id="yh1gNZ73oRIk" colab_type="code" colab={}
def balancing(X_train,y_train) :
    #Smote
    if( x ==1):
        #X_train, y_train = sm.fit_sample(X_train, y_train.values.ravel())
        X_train, y_train = sm.fit_sample(X_train, y_train)
        
    #smotetomek
    elif(x ==2) :
        X_train, y_train = smk.fit_sample(X_train, y_train.values.ravel())
    else :
        print('wrong input')
    y_train = pd.DataFrame(y_train)
    X_train = pd.DataFrame(X_train)

    X_train = X_train.round(2)
    #print(X_train.head())
    return X_train,y_train


# + id="KKEO_QaAod6Q" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 71} outputId="7c569a71-eef5-4a20-acb6-0b63cd3c0b4b"
x = 1
X_train,y_train = balancing(X_train,y_train)

# + [markdown] id="QkGxRtcmuC_I" colab_type="text"
# RENAMING THE COLUMNS FOR BETTER UNDERSTANDING AND READIBILITY

# + id="HSNCR-nwogkr" colab_type="code" colab={}
    X_train = X_train.rename(columns={0:'id',1:'MDVP:Fo(Hz)',2:'MDVP:Fhi(Hz)',3:'MDVP:Flo(Hz)',4:'MDVP:Jitter(%)',5
                       :'MDVP:Jitter(Abs)',6:'MDVP:RAP',7:'MDVP:PPQ',8:'Jitter:DDP',9:'MDVP:Shimmer',
                                      10:'MDVP:Shimmer(dB)',11:'Shimmer:APQ3',12:'Shimmer:APQ5',
                                      13:'MDVP:APQ',14:'Shimmer:DDA',15:'NHR',16:'HNR',
                                      17:'RPDE',18:'DFA',19:'spread1',20:'spread2',21:
                                     'D2', 22:'PPE'})
    y_train = y_train.rename(columns={0:'status'})

# + id="1ECC5GDtovww" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="f7e6abc7-1802-440e-9475-78ab2084f487"
y_train['status'].value_counts()

# + [markdown] id="aEUr0DHqugwV" colab_type="text"
# STANDARDIZING THE MULTIVARIATE DATA 

# + id="9iWSaVTkoy9j" colab_type="code" colab={}
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)

# + [markdown] id="lZcRNPzRurz4" colab_type="text"
# APPLYING MACHINE LEARNING ALGORITHMS 

# + id="JUQ4h7z9o46C" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 139} outputId="3b124da4-d69e-45d9-b7dd-e79fa804d52a"
#support vector classifier
svc_model = SVC(class_weight ='balanced',probability = True)
svc_model.fit(X_train,y_train)

# + id="LgSjg_6EqvjU" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="42e4f1ef-4c4e-4fde-b2f2-86f69282e087"
svc_predictions = svc_model.predict(X_test)
print(accuracy_score(y_test,svc_predictions))

# + id="yU1-t4hCq40D" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="606b28a5-79d6-4fb3-e8d6-f1bce6d71702"
print(classification_report(y_test,svc_predictions))
print(confusion_matrix(y_test,svc_predictions))

# + id="sf_a7J1gq73p" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="7f2ca52f-ce72-4e97-9931-b3bc68d081aa"
svc_score = cross_val_score(estimator = svc_model , X = X_train , y = y_train,cv = 5)
print(svc_score.mean())

# + id="TIlD3t8frCiX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 88} outputId="ce8faca4-e206-45d6-e60d-61c0dc5fedb3"
#NAIVE BAYES
nb = GaussianNB()
nb.fit(X_train,y_train)

# + id="ktAr51GtrGhu" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="2e05663d-8a34-4bf2-c4b1-3e3bd21016d6"
nb_predictions = nb.predict(X_test)
print(accuracy_score(y_test,nb_predictions))

# + id="olmZPIkWrPDp" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="94b95826-a324-49fd-b4a5-7d0e7ed6ca9b"
print(classification_report(y_test,nb_predictions))
print(confusion_matrix(y_test,nb_predictions))

# + id="ZR6E4I2wrTzm" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 156} outputId="7016e338-4dbb-4e63-89fe-1d808f8581f0"
#LOGISTIC REGRESSION
logreg = LogisticRegression()
logreg.fit(X_train,y_train)

# + id="h1jyT4g2rYDR" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="f3baae9e-5e83-4b14-dfa8-d0b65f2b7e5c"
logreg_predictions = logreg.predict(X_test)
print(accuracy_score(y_test,logreg_predictions))

# + id="CdnHDE3src5F" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="b942247e-e634-4515-b812-e0ce63205ac4"
print(classification_report(y_test,logreg_predictions))
print(confusion_matrix(y_test,logreg_predictions))

# + id="DK5ztzDDrgCh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 207} outputId="6456310f-7d3e-46b8-e0d2-c8207483d7b6"
#RANDOM FOREST
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators =10 )
rfc.fit(X_train,y_train)

# + id="a1bT7k4froEr" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="718bcb17-b2a4-4baf-d77d-c60d60719b29"
rfc_predictions = rfc.predict(X_test)
print(accuracy_score(y_test,rfc_predictions))

# + id="XeDySf8brreZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="045dfa14-3d99-4d97-c5e5-de60998b7bf6"
print(classification_report(y_test,rfc_predictions))
print(confusion_matrix(y_test,rfc_predictions))

# + id="gyQK0-Gort7S" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="cff37704-3359-4b8d-9349-9f28fab0fd8b"
rfc_score = cross_val_score(estimator = rfc , X = X_train , y = y_train,cv =5)
print(rfc_score.mean())

# + id="i-EkLPblryBT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="ba9bdc60-c24b-4f1f-9b8e-642dbe53c900"
import xgboost as xgb
model = xgb.XGBClassifier()
model.fit(X_train,y_train)

# + id="peF8UeA7r5U3" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0c419a83-0cc1-4094-f241-ba95835f65a2"
predictions = model.predict(X_test)
print(accuracy_score(y_test,predictions))

# + id="9ZEEyANZr_Ex" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="c3c795c5-c89e-4a79-d5e2-7c12657407a8"
print(classification_report(y_test,predictions))
print(confusion_matrix(y_test,predictions))

# + id="tA2NitsJsA05" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 122} outputId="17adc3f1-6554-46d6-a9ee-55158b0ed205"
score = cross_val_score(estimator = model , X = X_train , y = y_train,cv =5)
print(score.mean())

# + [markdown] id="HWJ9QajwyesZ" colab_type="text"
# **INFERENCE: Considering the accuracy scores and cross validation scores of various predictions over the same pre processed data, our proposed algorithm , XGBoost comes out as the winner**

# + [markdown] id="lKCUqJWdy0Z0" colab_type="text"
# **CONCLUSION:XGBOOST has given promising results in terms of early prediction on Parkinson's disease and can be explored furthur in order to making a feasible model for medical application**
#
# ---
#
#
#
# ---
#
#
# **As tested in this project,
# XGBoost with its ability to predict the disease at an early
# stage could change lives of many, paving the way for a
# better life for one and all**

StarcoderdataJupyterScriptsDedupFiltered