Unblock all features
Sign in to track progress, get AI help and save conversations.
Our current model is called logistic regression and it is actually a very popular model for binary classification tasks. However, we need to make a small adjustment to it.
Let's write our model's formula again, but with some additional notation:
What we are currently missing for the complete logistic regression model is the correct function to calculate the logit. The complete formula for the logit calculation is:
And for our case with only one input the logistic regression model will be:
We already know that are the inputs (also known as features).
What are the ? These are parameters (also known as weights). They give the model its flexibility. Depending on the values of parameters the model will perform differently.
If we draw the graph of the logistic regression function and mark our example messages on that graph, we can see how the model changes with different parameters.
With and we got the loss value close to 0 and 100% accuracy.
The model becomes:
We improved our model decreasing the loss value from 0.84 to 0 and increasing the accuracy from 40% to 100%.
However, it is very obvious that in reality the model is still bad. If we try to test it with random messages, we will probably not be satisfied in most cases.
The evaluation metrics tell us the model is good even though we know that it is bad. This is a bad sign meaning that we need to fix several things.
Currently we adjust the parameters using the same dataset we use to evaluate the model. But to make a fair evaluation we should use different data for it which is called a training set.
Then we will have two different loss values:
Let's try to evaluate our current model on a completely new dataset:
| Message | Label |
|---|---|
| Your account is locked, verify now!! | spam |
| Reset your password immediately!!! | spam |
| Congrats on the new job!! | not spam |
Now the loss value for the generalisation error is 0.8, although the training error was close to 0.
The issue when the generalisation error is high compared to the training error is called overfitting.
It happens when the model learns the training data too closely instead of learning the general patterns. It can be caused by several things, in our case it is because the training set is too small.
The whole idea of machine learning is to train a model on large datasets so it can learn patterns and perform well on new, unseen data. With a small training set our model can easily fit most of the messages but fails on new ones.
After increasing the datasets (900 messages in the training set and 100 messages in the test set) the best we can do is 0.64 as a training error and 0.61 as a generalisation error.
The messages for the increased datasets are taken from the public collection. I am not showing these messages here but keep in mind that we will use these datasets going forward.
The errors are almost equal now, but the problem is they are both quite high. Well, at least now we have honest values for our current model's performance.
The issue when both of the errors are high is called underfitting. It is basically impossible for our current model to be good for all the examples, regardless of how we adjust the parameters.
That is because our model is too simple.
One way we can improve it is to use more features counting other words and symbols beside !:
| Feature | Meaning |
|---|---|
| x₁ | number of "!" |
| x₂ | number of "asap" |
| x₃ | number of "free" |
The model formula will be:
With adjusted parameters for 30 features the model achieves 0.11 as a training error and 0.14 as a generalisation error.
Much better than before. But how exactly do we adjust these parameters for 30 features and 1000 messages?