"""
# Parkinsons disease
A disorder of the central nervous system that affects movement, often including tremors.
Nerve cell damage in the brain causes dopamine levels to drop, leading to the symptoms of Parkinson's.
Parkinson's often starts with a tremor in one hand. Other symptoms are slow movement, stiffness and loss of balance.
Medication can help control the symptoms of Parkinson's.


# What are the risk factors for Parkinson's disease?

Age,Sex,Genetic factors
Head trauma,Exposure to chemicals,Medications and other drugs,Impact of smoking.


"""
# import important libraries
import pandas as pd
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
%matplotlib inline

# load dataset
df=pd.read_csv('/kaggle/input/parkinsons-data-set/parkinsons.data')
# top 5 rows
df.head()
# check rows and features in Parkinsons Data
df.shape
# some important info about data like null values ,datatype
df.info()

# in this dataset there is no null values 
# Data Analysis With Matplotlib and Seaborn
numeric_columns=df.select_dtypes(include=['float','int']).columns
string_columns=df.select_dtypes(include='object').columns
# Total Numeric Columns
len(numeric_columns)
# univariant analysis 
# histogram
plt.figure(figsize=(30,35))
for i,v in enumerate(numeric_columns,1):
    plt.subplot(6,5,i)
    plt.title(v)
    sns.distplot(df[v],bins=10)
plt.show();
# target variable analysis
sns.countplot(df['status']);

# target variable check percentage using pie chart
df['status'].value_counts().plot(kind='pie',autopct='%.2f');

# check outliers
plt.figure(figsize=(25,30))
for i,col in enumerate(numeric_columns,1):
    plt.subplot(6,4,i)
    sns.set_theme(style="whitegrid")
    plt.title(col)
    sns.boxplot(df[col])
        
plt.show()

# now fixed outlier using capping technique
# Computing 10th, 90th percentiles and replacing the outliers
import numpy as np
tenth_per=[]
ninety_per=[]
for i,col in enumerate(numeric_columns,1):
    tenth_percentile = np.percentile(df[col], 10)
    tenth_per.append(tenth_percentile)
    ninetieth_percentile = np.percentile(df[col], 90)
    ninety_per.append(ninetieth_percentile)
#     print(f"{i}  tenth percentile {tenth_percentile} ninetieth percentile {ninetieth_percentile}")
    
    
# capping
for index,i in enumerate(numeric_columns):
    df[i]=df[i].apply(lambda x : tenth_per[index] if (x<tenth_per[index]) else x)
    
    df[i]=df[i].apply(lambda x : ninety_per[index] if (x>ninety_per[index]) else x)
# after capping all values
# check outliers once again
plt.figure(figsize=(25,30))
for i,col in enumerate(numeric_columns,1):
    plt.subplot(6,4,i)
    sns.set_theme(style="whitegrid")
    plt.title(col)
    sns.boxplot(df[col])
        
plt.show()
# check correlation 
plt.figure(figsize=(19,7))
sns.heatmap(df.corr(),annot=True);
# top 5 rows
df.head()
# independent and dependent variable
x=df.drop(columns=['name','status'],axis=1)
y=df['status']
# Data Standardization
# Al the values of the dataset of all the columns varies. So we need to convert all the values in a common range.
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
x=scaler.fit_transform(x)
# divide in test and train data
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=2)
# model selection
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score, recall_score
def best_model(model,X,y):
    xtrain,xtest,ytrain,ytest=train_test_split(X,y,test_size=0.2)
    print(xtrain.shape,xtest.shape,ytrain.shape,ytest.shape)
    model.fit(xtrain,ytrain)
    print('Train Score',model.score(xtrain,ytrain))
    pred=model.predict(xtest)
    print('Prediction Score',accuracy_score(pred,ytest))
    print('Test Score',model.score(xtest,ytest))
    print("***********************************************")
    print(confusion_matrix(ytest,pred))
    print("***********************************************")
    print("Precision Score",precision_score(ytest,pred))
    print("Recall Score",recall_score(ytest,pred))
    
    
# logistic Regression
from sklearn.linear_model import LogisticRegression
lr=LogisticRegression()
best_model(lr,x,y)
# svc
from sklearn.svm import SVC
svc=SVC(kernel='linear')
best_model(svc,x,y)
# k nearest neighbour
from sklearn.neighbors import KNeighborsClassifier  
classifier= KNeighborsClassifier(n_neighbors=5 )  
best_model(classifier,x,y)
# random forest
from sklearn.ensemble import RandomForestClassifier
rf=RandomForestClassifier()
best_model(rf,x,y)
"""
svc has highest accuracy score because

Effective in high dimensional spaces.

Still effective in cases where number of dimensions is greater than the number of samples.


"""
x=[119.992,157.302,75.6146,0.007840,0.00007,40,0.00370,55,455,76,0.005540,0.011090,0.043740,0.06545,0.022110,21.033,0.414783,0.789799,-4.813031,0.266482,2.301442,0.284654]
result=svc.predict(scaler.fit_transform([x]))
if result[0]==1:
    print("Parkinsons Disease")
else:
    print('Healthy')