<jupyter_start><jupyter_text>Detecting Parkinson’s Disease with XGBoost  IntroductionThe goal of the project is to Predict who is likely having the Parkinson's Disease based on the features of a user.   Steps involved in this Notebook1. Getting to know about the Data2. Extract New features3. Check distribution of target variable4. Understand Relationship between variables5. Identifying Potential Outliers6. Data Cleaning7. Model Building       > XGBoost classifier8. Feature Importances  1. Make necessary imports:<jupyter_code>import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
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 warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>2. Now, let’s read the data into a DataFrame and get the first 5 records.<jupyter_code># Read the data
df=pd.read_csv('parkinsons.data')
df.head(20)
df.info()
df.isna().sum()
#Inspecting the dataframe
# This function displays the datatype,levels 0f variables and number of levels in each column of the given train dataframe.here lambda is used to pass arguments
def inspect_data(data):
    return pd.DataFrame({"Data Type":data.dtypes,"No of Levels":data.apply(lambda x: x.nunique(),axis=0), "Levels":data.apply(lambda x: str(x.unique()),axis=0)})
inspect_data(df)<jupyter_output><empty_output><jupyter_text>Statistical Summary<jupyter_code>df.describe(include='all')<jupyter_output><empty_output><jupyter_text>Checking for Duplicate values<jupyter_code>df.duplicated().sum() # displays duplicate records
#checking for duplicated rows
df[df.duplicated()]<jupyter_output><empty_output><jupyter_text>There are no duplications in the dataset<jupyter_code>#checking for null values in train
df.isnull().sum(axis=0)
#Visualising missing values for a sample of 250
import missingno as msno

msno.matrix(df.sample(100))

#Heatmap
#The missingno correlation heatmap measures nullity correlation: how strongly the presence or absence of one variable affects the presence of another:

#msno.heatmap(mobp)

#there are no missing values therefore nothing is displayed
msno.bar(df.sample(10))
#no missing values

#Dendrogram
#The dendrogram allows you to more fully correlate variable completion, revealing trends deeper than the pairwise ones visible in the correlation heatmap:

#msno.dendrogram(mobp)<jupyter_output><empty_output><jupyter_text>Estimating Skewness and Kurtosis<jupyter_code>df.skew()
df.kurt()
sns.countplot(df['status'])
#checking for target variable
y = df['status']
plt.figure(1); plt.title('<NAME>')
sns.distplot(y, kde=False)
plt.figure(2); plt.title('Normal')
sns.distplot(y, kde=False)
plt.figure(3); plt.title('Log Normal')
sns.distplot(y, kde=False)
fig = plt.figure(figsize = (12,10))
sns.heatmap(df.corr(), cmap='Greens', annot = True) 
#checking the distribution of highly correlated features with label variable
cols = ['NHR', 'PPE',
       'D2', 'HNR']
plt.figure(figsize=(20,4), dpi=100)
i = 1
for col in cols:
    plt.subplot(1,11,i)
    #sns.distplot(car['price'])
    sns.distplot(df[col])
    i = i+1
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Get the features and labels from the DataFrame (dataset). The features are all the columns except ‘status’, and the labels are those in the ‘status’ column.<jupyter_code>features=df.loc[:,df.columns!='status'].values[:,1:]
labels=df.loc[:,'status'].values<jupyter_output><empty_output><jupyter_text>The ‘status’ column has values 0 and 1 as labels; let’s get the counts of these labels for both- 0 and 1.<jupyter_code>print(labels[labels==1].shape[0], labels[labels==0].shape[0]) #We have 147 ones and 48 zeros in the status column in our dataset.<jupyter_output>147 48<jupyter_text>Initialize a MinMaxScaler and scale the features to between -1 and 1 to normalize them. The MinMaxScaler transforms features by scaling them to a given range. The fit_transform() method fits to the data and then transforms it. We don’t need to scale the labels.<jupyter_code>scaler=MinMaxScaler((-1,1))
x=scaler.fit_transform(features)
y=labels<jupyter_output><empty_output><jupyter_text>Now, split the dataset into training and testing sets keeping 20% of the data for testing.<jupyter_code>x_train,x_test,y_train,y_test=train_test_split(x, y, test_size=0.2, random_state=7)<jupyter_output><empty_output><jupyter_text>Now the data is ready to deal with ML Algorithms Here we are done with the EDA and data cleaning part   Initialize an XGBClassifier and train the model. This classifies using eXtreme Gradient Boosting- using gradient boosting algorithms for modern data science problems. It falls under the category of Ensemble Learning in ML, where we train and predict using many models to produce one superior output.<jupyter_code>model=XGBClassifier()
model.fit(x_train,y_train)<jupyter_output>[21:10:16] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.4.0/src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.<jupyter_text>8. Finally, generate y_pred (predicted values for x_test) and calculate the accuracy for the model. Print it out.<jupyter_code>y_pred=model.predict(x_test)
print(accuracy_score(y_test, y_pred)*100)<jupyter_output>94.87179487179486<jupyter_text>Export the tested model to a pickle file<jupyter_code>import pickle
with open('parkinson_model.pkl','wb') as f:
    pickle.dump(model,f)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup