Hello everyone and welcome to the logistic regression with Python lecture part 3 in part 1 of this lecture
series.
We went ahead and did some exploratory data analysis with some visualizations.
In part two we went ahead and cleaner data by dealing of categorical variables and missing data.
Now in part 3 we're actually going to train and use the model to predict the classes of whether or not
a passenger survived on the Titanic.
Let's go ahead and jump back to the notebook where we left off last time.
All right.
Here I am at the notebook.
And here we have our data set.
Something to note here is that if you go back to your folder you'll notice that you had a titanic underscore
test that not see as the file in a titanic underscore train CACP file.
I'm going to go ahead and treat this training set right here or train as if it was the entirety of our
data.
So I'm going to go ahead and walk through the process of a trained test pilot.
In reality what you can actually do is just do the same cleaning process on the test.
See as we have here and use that as your test set.
Since she actually have two separate cc files that in order to go through the process of using Cyke
Hitler's train to split I'm going to go ahead and treat this train data frame as if it was all our data
.
All right let's go ahead and walk through how you would do that.
The first thing we need to do is separate our data into an x and y.
Remember that why is the column that you're trying to predict the actual label.
In this case that's going to be the survive column.
So I want to predict a zero or one on that survive column.
Everything else are going to be the features.
So I'm going to go ahead and say x is equal to train drop survived along axis is equal to 1.
Now I can go ahead and say from S-K learn the cross underscore validation import train test split and
then.
I'm just using tab to autocomplete there and what I like to do is call train to split.
You shift to Jupiter notebook's I can see that documentation stream and scroll down until I get to the
example.
That way I can just go ahead and copy and paste this and save myself some typing.
So I go in and copy that.
And then I will paste it into replace that and put it now all on one line.
OK so here you have my X train x test Y train Y test.
I'm doing train test split I'm going to go ahead and tests set my test sized to 30 percent and I'll
set my random state to 101.
You don't have to set a random state but this is just so if you want your results to match mine.
Exactly.
Now let's go ahead and run this.
Great.
Now we have our X test Y train Y test and X train data.
Now the next step is to actually train and predicts by creating a model for religious or regression
.
We'll go ahead and grab this from the linear model family so I'll say from S-K learn the linear underscore
model import logistic regression.
We'll go ahead and run that.
And the next thing I want to do is create an instance of a logistic regression model.
So I go ahead and create logistic regression.
We'll go ahead and call our model log model and the next step is actually just training the model.
Now if you do shift tap here we'll give you some more information about the various parameters you can
add but we'll go ahead and just passen fit x train and the y underscore train or run that.
And it shows you the default parameters.
Those should be fine for us and we'll go ahead and call some predictions off of this using our X test
dataset.
That means we'll go ahead and say predictions equals log model predicts and then we just pass x tests
because we want to predict values off of that X underscore a test.
Great.
And that's it.
We've already we're able to create a model model and predict the model in essentially just three lines
of code.
If you don't count the train tests split this is actually very reflective of a real world analysis and
machine learning building process.
A lot of times most of your work as a data scientist is actually going to be in cleaning the data and
getting the data into an acceptable form.
We're a machine learning algorithm the machine learning algorithm libraries that you'll be using are
actually sophisticated enough and abstract enough that the process of building them will actually be
quite simple.
So most of your work will be reflected on the cleaning data.
OK.
Now let's go ahead and evaluate our model for classification tasks sikat learn has a really nice classification
report you can use.
You can say from Eskdale learn that metrics import classification report and the classification report
will basically tell you your precision your recall value is accuracy a foreign score etc..
And that way you don't have to read it yourself often confusion matrix.
We're going to go ahead and say print's you call classification report and then you pass your true values
y test and then your predictions run that then and actually report back the precision and recall for
each class as well as the average total and the supporting cases for it as well as an EF 1 score.
Now if you don't want the classification of poor and you want the pure confusion matrix as Kaylor metrics
has that too you can say from S-K learned that metrics in poor confusion matrix understudying tab autocomplete
.
And you can say confusion matrix and pasand the same thing.
Why test predictions and you'll get the actual confusion matrix to evaluate your model that way.
That way in case you want to actually calculate a very specific value you can do that directly from
the confusion matrix.
OK these results are actually not so bad considering we're dealing a smaller set of the data by splitting
our training data instead of using the train.
And Tessie espies but you may want to explore other ways you can increase your precision or recall her
accuracy what she can do are a couple of things maybe just try using the entirety of the training set
versus comparing it to the test CXXVI after you repeat the cleaning process for the test cxxviii file
other things you can do are more feature engineering such as trying to grab the title of the name.
Treat that as a feature such as Dr Mr Mrs cetera and maybe the cabin letter could be a feature maybe
that has to do if location on the ship and that has more information and also the ticket may have information
for you.
Go ahead and continue to explore this project as you see fit.
There's also if you go back to the Kaggle link that's actually in the note book to the machine learning
section.
There is some more information on Kaldor column and how to get started with doing this sort of analysis
with Python so you can check this out.
Cregan more ideas.
And they actually use a random force.
But since we haven't learned about treatment that's quite yet.
You may want to hold off on that but go ahead and treat this as kind of an open ended ending for you
to go ahead and explore the rest of this project and this day to see if you can improve the model further
.
There are some more feature engineering.
OK.
Hope you enjoyed this project.
Coming up next is going to be your actual portfolio project exercise where you'll have to get a new
data set.
Answer some questions and perform some tasks using Python and logistic regression.
OK thanks everyone and I'll see at the next lecture.