Supervised Learning With Scikit-Learn: How Good Is Your Model?
Supervised Learning With Scikit-Learn: How Good Is Your Model?
How good is
your model?
Supervised Learning with scikit-learn
Classification metrics
● Measuring model performance with accuracy:
● Fraction of correctly classified samples
● Not always a useful metric
Supervised Learning with scikit-learn
● Accuracy:
Supervised Learning with scikit-learn
● Recall :
● F1 score :
Let’s practice!
SUPERVISED LEARNING WITH SCIKIT-LEARN
Logistic regression
and the ROC curve
Supervised Learning with scikit-learn
Source: Andreas Müller & Sarah Guido, Introduction to Machine Learning with Python
Supervised Learning with scikit-learn
Probability thresholds
● By default, logistic regression threshold = 0.5
● Not specific to logistic regression
● k-NN classifiers also have thresholds
● What happens if we vary the threshold?
Supervised Learning with scikit-learn
p = 0.5
p=1
Supervised Learning with scikit-learn
In [9]: plt.show();
Supervised Learning with scikit-learn
logreg.predict_proba(X_test)[:,1]
SUPERVISED LEARNING WITH SCIKIT-LEARN
Let’s practice!
SUPERVISED LEARNING WITH SCIKIT-LEARN
Area under
the ROC curve
Supervised Learning with scikit-learn
AUC in scikit-learn
In [1]: from sklearn.metrics import roc_auc_score
In [9]: print(cv_scores)
[ 0.99673203 0.99183007 0.99583796 1. 0.96140652]
SUPERVISED LEARNING WITH SCIKIT-LEARN
Let’s practice!
SUPERVISED LEARNING WITH SCIKIT-LEARN
Hyperparameter
tuning
Supervised Learning with scikit-learn
Hyperparameter tuning
● Linear regression: Choosing parameters
● Ridge/lasso regression: Choosing alpha
● k-Nearest Neighbors: Choosing n_neighbors
● Parameters like alpha and k: Hyperparameters
● Hyperparameters cannot be learned by fi!ing the model
Supervised Learning with scikit-learn
Alpha
Supervised Learning with scikit-learn
GridSearchCV in scikit-learn
In [1]: from sklearn.model_selection import GridSearchCV
In [5]: knn_cv.fit(X, y)
In [6]: knn_cv.best_params_
Out[6]: {'n_neighbors': 12}
In [7]: knn_cv.best_score_
Out[7]: 0.933216168717
SUPERVISED LEARNING WITH SCIKIT-LEARN
Let’s practice!
SUPERVISED LEARNING WITH SCIKIT-LEARN
Let’s practice!