My First Machine Learning Mini-Adventure with Python
Tags: python, machinelearning, computerscience, tutorial
Published: 2020-06-29 09:56:34

<p><strong>Being a Python Developer I have always heard about its application in all the latest technological advancements going on around the world.</strong> This always fascinated me to delve into the details of how it is done.</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/70gba5vzss1o74jb60jp.gif)

<p class="graf graf--p">And coincidentally, I heard a news about one of my relatives being suffering from Parkinson&rsquo;s Disease and how they the doctors were unable to diagnose it in the earlier stages.&nbsp;</p>

<p><a href="https://bit.ly/3esKGHc"><em><strong>Some of the Most Challenging Python Projects</strong></em></a></p>

<p><strong>This is where I got the idea for my next adventure with Python. I decided to build a system that detects Parkinson's Disease quite easily.</strong>Parkinson&rsquo;s Disease is a disorder of the nervous system that affects the movement of few parts of the body.&nbsp;</p>
<h2><strong>MY MACHINE LEARNING ADVENTURE WITH PYTHON</strong></h2>
<h3><strong>Insights of the Project</strong></h3>
<p class="graf graf--p">For this Machine Learning Project I used different Python libraries such as <strong class="markup--strong markup--p-strong">scikit-learn</strong>, <strong class="markup--strong markup--p-strong">numpy</strong>, <strong class="markup--strong markup--p-strong">pandas</strong>, and <strong class="markup--strong markup--p-strong">xgboost</strong> to build a model by using <strong class="markup--strong markup--p-strong">XGBClassifier</strong>. The flow of project goes like this &mdash; loading the data, getting the features and labels, scaling the features, then splitting the dataset, building an XGBClassifier, and then calculating the accuracy of the model.</p>

<p><a href="https://t.me/pythonpundits"><em><strong>The Power of Python at your fingertips</strong></em></a></p>

<h3><strong>My Dataset</strong></h3>
<p class="graf graf--p">I used the <strong class="markup--strong markup--p-strong">UCI ML Parkinsons dataset </strong>for my project. It has 24 columns and 195 records and is sized only 39.7 KB</p>

<p><a href="https://bit.ly/2YyZL4G"><em><strong>A huge collection of Datasets</strong></em></a></p>

<h3><strong>Prerequisites</strong></h3>
<p>I installed the following libraries through pip:</p>
<pre class="graf graf--pre"><strong class="markup--strong markup--pre-strong">pip install numpy pandas sklearn xgboost<br /><br /></strong></pre>
<p class="graf graf--p">I also installed JupyterLab and then run through the following command:</p>
<pre class="graf graf--pre"><strong class="markup--strong markup--pre-strong">C:\Users\Anuj&gt;jupyter lab</strong></pre>
<p class="graf graf--p">This prompted a new JupyterLab window into the browser. Then I created a new console and typed in my code, then I pressed Shift+Enter to execute the lines.</p>

<p><a href="https://docs.google.com/spreadsheets/d/1eNBLcKqCVN9zZQvfGUmm5bAzsETqB_ugVOlUtmvJGYU/edit#gid=1276262674"><em><strong>'Coz this Cheat Sheet is a treat for all the Tech freaks</strong></em></a></p>

<h2><strong>BUILDING THE PROJECT</strong></h2>
<h3><strong>STEP №1</strong></h3>
<p>The first step of any project is to make all the necessary imports for the project. I used the following commands for the same.</p>

<pre><strong>import numpy as np
import pandas as pd
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</strong></pre>

<h3><strong>STEP №2</strong></h3>
<p>Now the next step was to read the data into a DataFrame so that I get the first 5 records.</p>
<pre><strong>#Anuj&mdash; Read the data
df=pd.read_csv(&lsquo;D:\\Rinu\\parkinsons.data&rsquo;)
df.head()</strong></pre>
<h4 class="graf graf--h4">Screenshot:</h4>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k9iyhkzuqtndhvlg227g.png)

<h3><strong>STEP №3</strong></h3>
<p>Then I got the features and labels from the DataFrame (dataset). The features are all the columns except, and the labels are those in the column.</p>

<pre>#Anuj &mdash; Get the features and labels
features=df.loc[:,df.columns!=&rsquo;status&rsquo;].values[:,1:]
labels=df.loc[:,&rsquo;status&rsquo;].values</strong></pre>

<h3><strong>STEP №4</strong></h3>
<p class="graf graf--p">The &lsquo;status&rsquo; column has values 0 and 1 as labels; then I counted the labels for both 0 and 1.</p>

<pre><strong>#Anuj&mdash; Get the count of each label (0 and 1) in labels
print(labels[labels==1].shape[0], labels[labels==0].shape[0])</strong></pre>

<p class="graf graf--p"><strong>There are 147 ones and 48 zeros in the status column in the dataset.</strong></p>

<h3><strong>STEP №5</strong></h3>
<p class="graf graf--p">So, now I initialized a MinMaxScaler and scaled the features to between -1 and 1 to normalize them. The MinMaxScaler is meant to transform the features by scaling them to a given range. The fit_transform() method does two things; fits to the data and then transforms it. There's no need to scale the labels.</p>
<pre><strong>#Anuj &mdash; Scale the features to between -1 and 1
scaler=MinMaxScaler((-1,1))
x=scaler.fit_transform(features)
y=labels</strong></pre>
<h3><strong>STEP №6</strong></h3>
<p class="graf graf--p">Time to split the dataset into training and testing sets keeping only 20% of the data for testing.</p>
<pre><strong>#Anuj &mdash; Split the dataset
x_train,x_test,y_train,y_test=train_test_split(x, y, test_size=0.2, random_state=7)</strong></pre>

<h3><strong>STEP №7</strong></h3>
<p class="graf graf--p">I then initialized an XGBClassifier and trained the model. The classification is done using eXtreme Gradient Boosting, i.e. using gradient boosting algorithms for modern Data Science problems. This comes under the category of Ensemble Learning in ML, where we train and predict using many models to produce one superior output.</p>
<pre><strong>#Anuj &mdash; Train the model
model=XGBClassifier()
model.fit(x_train,y_train)</strong></pre>
<h4><strong>Screenshot:</strong></h4>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k9iyhkzuqtndhvlg227g.png)

<h3><strong>STEP №8</strong></h3>
<p class="graf graf--p">The final step is to generate y_pred (predicted values for x_test) and to calculate the accuracy for the model. Finally I printed it.</p>
<pre><strong>#Anuj &mdash; Calculate the accuracy
y_pred=model.predict(x_test)
print(accuracy_score(y_test, y_pred)*100)</strong></pre>
<h4><strong>Screenshot:</strong></h4>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ylvqikphhsnxy1cpcvj8.png)

<h2><strong>THE FINAL NOTE...</strong></h2>
<p class="graf graf--p">In this Machine Learning project of mine, I detected the presence of Parkinson&rsquo;s Disease in people using various factors. <strong>For this, I used an XGBClassifier and made use of the sklearn library to prepare the dataset.</strong> Through this I achieved an <strong>accuracy of 94.87%</strong>, which is more than decent considering the number of lines of code in this Python project.</p>

<p><a href="https://dev.to/anujgupta/stay-ahead-of-your-friends-with-all-the-latest-technology-trends-43k3"><em><strong>Have all the Latest Technology Trends before your friends</strong></em></a></p>