How to get started with Machine Learning in about 10 minutes
By: Tirmidzi Faizal Aflahi
Tags: Machine Learning, Data Science, Python, Technology, Artificial Intelligence

Python Machine Learning Projects

Of course, reading and studying alone will not bring you where you need to go. You need actual practice. As I said on my blog, learning the tools is pointless if you do not jump into the data. And so, I introduce you to a place where you can find Python Machine Learning Projects easily.

Courtesy of Kaggle.com

Kaggle is a platform where you can dive directly into the data. You’ll solve projects and get really good at Machine Learning. Something that might make you more interested in it: the Machine Learning competitions it holds may give a prize as big as $100,000. And you might want to try your luck. Haha.

But, the most important thing is not the money — it is really a place where you can find Machine Learning with Python Projects. There are lots of projects you can try. But if you are a newbie, and I assume you are, you will want to join this competition.

Here’s an example project that we’ll use in the below tutorial:

Yes, the infamous Titanic. A tragic disaster in 1912, that took the lives of 1502 people from 2224 passengers and crew. This Kaggle competition (or I can say tutorial) gives you the real data about the disaster. And your task is to explain the data so that you can predict whether a personal survived or not during the incident.

Machine Learning with Python Tutorial

Before going deep into the data of the Titanic, let’s install some tools you need.

Of course, Python. You need to install it first from the Python offfical website. You need to install version 3.6+ to keep up to date with the libraries.

After that, you need to install all the libraries via Python pip. Pip should be installed automatically with the distribution of Python you just downloaded.

Then install the things you need via pip. Open your terminal, command line, or Powershell, and write the following:

pip install numpy

pip install pandas

pip install matplotlib

pip install seaborn

pip install scikit-learn

pip install jupyter

Well, everything looks good. But wait, what is jupyter? Jupyter stands for Julia, Python, and R, hence Jupytr. But it’s an odd combo of words, so they changed it into just Jupyter. It is a famous notebook where you can write Python code interactively.

Just type jupyter notebook in your terminal and you will open a browser page like this:

Jupyter Notebook

Write the code inside the green rectangle and you can write and evaluate Python code interactively.

Now you have installed all the tools. Let’s get going!

Data Exploration

The first step is to explore the data. You need to download the data from the Titanic page in Kaggle. Then put the extracted data inside a folder where you start your Jupyter notebook.

Then import the necessary libraries:

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

import warnings

warnings.filterwarnings('ignore')

%matplotlib inline

Then load the data:

train_df=pd.read_csv("train.csv")

train_df.head()

You will see something like this:

That is our data. It has the following columns:

PassengerId, the identifier of the passenger Survived, whether he/she survived or not Pclass, the class of the service, maybe 1 is economy, 2 is business, and 3 is first class Name, the name of the passenger Sex Age Sibsp, or siblings and spouses, number of siblings and spouses on board Parch, or parents and children, number of them on board Ticket, ticket detail Cabin, their cabin. NaN means unknown Embarked, the origin of embarkation, S for Southampton, Q for Queenstown, C for Cherbourg

While exploring data, we often find missing data. Let’s see them:

def missingdata(data):

total = data.isnull().sum().sort_values(ascending = False)

percent = (data.isnull().sum()/data.isnull().count()*100).sort_values(ascending = False)

ms=pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])

ms= ms[ms["Percent"] > 0]

f,ax =plt.subplots(figsize=(8,6))

plt.xticks(rotation='90')

fig=sns.barplot(ms.index, ms["Percent"],color="green",alpha=0.8)

plt.xlabel('Features', fontsize=15)

plt.ylabel('Percent of missing values', fontsize=15)

plt.title('Percent missing data by feature', fontsize=15)

return ms missingdata(train_df)

We will see a result like this:

The cabin, age, and embarked data has some missing values. And cabin information is largely missing. We need to do something about them. This is what we call Data Cleaning.

Data Cleaning

This is what we use 90% of the time. We will do Data Cleaning a lot for every single Machine Learning project. When the data is clean, we can easily jump ahead to the next step without worrying about anything.

The most common technique in Data Cleaning is filling missing data. You can fill the data missing with Mode, Mean, or Median. There is no absolute rule on these choices — you can try to choose one after another and see the performance. But, for a rule of thumb, you can only use mode for categorized data, and you can use median or mean for continuous data.

So let’s fill the embarkation data with Mode and the Age data with median.

train_df['Embarked'].fillna(train_df['Embarked'].mode()[0], inplace = True)

train_df['Age'].fillna(train_df['Age'].median(), inplace = True)

The next important technique is to just remove the data, especially for largely missing data. Let’s do it for the cabin data.

drop_column = ['Cabin']

train_df.drop(drop_column, axis=1, inplace = True)

Now we can check the data we have cleaned.

print('check the nan value in train data')

print(train_df.isnull().sum())

Perfect! No missing data found. Means the data has been cleaned.