Python Machine Learning Example – Linear Regression

In my previous post, I went over the basic concepts in machine learning and I used a very small amount of data. I got great feedbacks but also notes to make more complex example with bigger dataset. In this post I will use a bigger dataset and use pandas , seaborn and scikit-learn to illustrate the process.

Linear regression

Linear regression is a very simple supervised machine learning algorithm – we have data (X , Y) with linear relationship. we want to predict unknown Y vales for given X. X can be one or more parameters.

Before we start we need to import some libraries:

In this example we will use a dataset from seaborn library (seaborn provides statistics graphs as an extension to matplotlib)

There are other datasets builtin seaborn library – see documentation

Lets look at the data:

The dataset contains tips data from different customers females and males smokers and non smokers from days Thursday to Sunday, dinner or lunch and from different tables size

We want to predict how much tip the waiter will earn based on other parameters

First lets look into the data using some dataframe methods :

Data analysis with Pandas

Now lets answer some questions:

What is the hardest day to work ? (based on number of tables been served)

Lets find out what is the best day to work – maximum tips (sum and percents)

we can see that the tips are around 15% of the bill

who eats more (and tips more)? smokers or non smokers?

Lets group by day and table size:

(smaller tables are better to serve)

Visualization with Seaborn

lets draw some seaborn graphs:

Tables per day

Tables per day per size:

Smokers or not ?

Transform and clean the data

Before we start building our model, we need to convert all the text values into numbers. We can do it in many ways:

  • Using update statements
  • Using replace method
  • Iterate over the rows
  • Use dummy variables

Using replace: 

convert sex and smoker columns to values

Using dummy variables:

The values in day column are:  Thu, Fri, Sat, Sun we can convert it to 1,2,3,4 but to get a good model, it is better to use boolean variables. We can achieve it by converting the column into 4 columns – one for each day with 0 or 1 as values. In pandas library it can be done using get_dummies:

Actually we can drop one of the columns without loosing data – for example if we drop column ‘Thur’ we know that index 135 is Thur because all other days are 0. It is also supported by the same function:

Do the same with time column and concat all data frames, Also we don’t need the day and size columns anymore so we drop them

Building our Machine Learning model

Now we are ready to build the linear regression model:

We create a list of features as X and predicted as Y

Now lets split the data into test and train so we can test our model before we use it – we decide to split 70% – 30%:

Now lets train the model with X_train and y_train:

And predict the X_test values:

We can now look at the predictions and compare it with y_test

We can draw a graph to see the difference distribution:

 

We can see from the graph that most of the times the predictions were correct (difference = 0). We can continue working on the model , adding data and play with the parameters

If we want to predict new value for example :

We have a 3 size table smoker male on friday lunch:

we expect to get 3.12$ tip

And the same table on dinner:

We expect to get 3.73$

 

 

 

 

 

Tagged ,

2 thoughts on “Python Machine Learning Example – Linear Regression

  1. […] For example with a bigger dataset and pandas see this post […]

  2. Hi,
    Can you help me in making me understand your conclusion about the distribution plot.
    i am new to python and its concepts and trying to learn more about the language.
    i am sorry to have asked such a silly question.

    thank you

Leave a Reply

Your email address will not be published. Required fields are marked *