Machine Learning:Logistic Regression Tutorial

Day 2: Getting the Python Code


Open a new Python project and paste the contents into the the first frame.

Run the code in the first frame.

This code imports libraries from Python


Paste the contents into the the second frame.

Run the code in the first two frames.

These lines read in the Excel spreasheet and assign the contents to the variable dataset.

Then the first lines of the dataset are displayed.

Your file location will differ from mine. It needs to be where you saved your spreadsheet.

"grill2.xlsx" is the file name.


Create a third frame and paste the contents above into it.

The first line of code assigns the independent variables to the letter X.

The second line of code assigns the values in the dependent variable to y


Create a forth frame and paste the contents above into it.

This first line imports the library train_test_split

The next line trains the algorithum, creates a X_test set that contains 25% of the dataset.

The random_state statement assures us that the random seed will not be regenerated each time we run the program.


Create a fifth frame and paste the contents above into it.

These logistic regression lines of the model do the training and make the predictions.

Create a new frame and key in: print(X_test).

Save and run each frame starting with frame 1.

Let's examine the results of the last frame.


The above 30 items, which is 25%, represent the test set that will be trained on the remaining 75% of the dataset.

Let's look at the first entry in the X_test data. It is the eighth record in the original spreadsheet Id 6849. The 2 indicates that the record is a female. The 3 indicates the age group is 41 to 50. The 7 means that the customer is 71- 80 years. The 3 indicates that she completed one year of college.

Now let's get what prediction label determined for our X_test data. Key in this line in a new frame:

print(y_pred)

Here are the result after running all frames.

[0 0 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0]