Scikit-learn MOOC
https://inria.github.io/scikit-learn-mooc/index.html
57 flashcards · Computer Science · by charlie42
What variable called "_" (underscore) is for?
For storing things we're not interested in, garbage
A tool to quantify fairness issue in your model
Fairlearn.org
Tool used to explore relationship between two categorical variables
Contingency table
Pandas function to print a contingency table
pd.crosstab
What is this plot called?
pairplot
What pairplot displays?
Relationships between each two variables, plus distribution of the class for each pair
What pairplot is used for? Example.
To explore relationships between input and output variables. Here we could even manually predict class from some pairs
Explain this code:
We divide the number of correct predictions by the total number of predictions. It just happens to be the same formula as mean.
Arguments of the model.score method
X_test, y_test
What's generalization performance of a model?
Score obtained on the test set
What's computational performance of a model?
Computational cost of training a model or to use it to make predictions
How to get a baseline score for a useless model with sklearn?
Use a Dummy Classifier, it always predict the same class from sklearn.dummy import DummyClassifier DummyClassifier(strategy="most_frequent")
Why scaling data is useful for logistic regression?
It uses gradient descent which converges much faster if ranges are the same. It probably won't affect predictive (generalization) performance
Which class of models doesn't need data scaling?
Tree models (but they will not suffer from it)
In sklearn, which attributes end in "_", like scaler.mean_?
Attributes that are learned from data
Formula used in StandardScaler
What would make_pipeline helper function name StandardScaler step of the pipeline?
standardscaler
Function to get steps of a pipeline
pipe.named_steps()
What does this return: pipe.named_steps()
Dictionary where keys are names of steps and values are the objects
What does cross_validate function do to the trained models? Explain.
It discards them. The goal of cross-validation is to get an idea on generalizability of a model (and variability of this value), not to train a model.
Another useful information you can get from cross-validation besides the average performance?
Standard deviation of the performance, to estimate uncertainty of the performance score. If uncertainty is low, we can even use validation set directly in the future.
What conclusion can we make about uncertainty of the score in this case:
How to get all newly created features from the OneHotEncoder from one original feature
encoder.get_feature_names_out(input_features=["education"])
What happens if an error occures during cross-validation (cross_validate)?
It will print a warning and return NaN as a score. To force it to raise an exception you need to provide error_score="raise" argument
Sklearn functionality that helps applying different transformers to different columns
ColumnTransformer It applies different transformers to different sets of columns Define two transformers Tell which columns should use which Here we apply OneHotEncoder to categorical variables and StandardScaler to numerical variables: Then just use the ColumnTranformer in a pipeline: You can use make_column_selector to select columns of a specific type:
When should you use OneHotEncoding (vs OrdinalEncoding)?
When the data is not ordinal AND you're using non-tree based models
When is it okay to use mean imputation?
When data is normally distributed. If data is skewed, median imputation is better.
Two steps of the model.score method
It computes the predicted values using .predict method It uses scoring formula, comparing predicted values to provided target values
Another term for data standardization
Z-score normalization
What does training error refer to?
Error on the train set
Another name for training error.
Empirical error
What does test error refer to?
Error on test set
Another name for test error.
Generalization error
What's a score?
It's a metric for which higher values mean better results The opposite is error - a metric for which lower values mean better results
The parameter scoring in cross_validate always expect a function that is a score (as opposed to an error). What can you do to pass an error metric to the scoring parameter instead?
Add neg_ to the string:
Does this model underfit? Why?
No, the train error is very low, the model is flexible enough to capture all the variation in the training set.
Does this model overfit? Why?
Yes, the test error is much higher than the train error. The model doesn't generalize well.
In decision tree model, which parameter is used to control the tradeoff between overfitting and underfitting?
max_depth
How to time a cell in jupyter notebook?
%%time
What does validation curve show?
Train and test error (y axis) on different complexities of a model (x axis)
Visualize validation curve of a decision tree model with max_depth varying from 1 to 25.
On which values the model overfits?
>10, training error goes down, which test error goes up
What's between overfitting and underfitting?
Generalizing
What's plot of model performance over difference number of training examples called?
Learning curve
What learning curve is used for?
To check if adding more training examples is useful
If we see that the learning curve plateaued, how can we increase performance?
Using a more complex model
How to build a validation curve with sklearn?
from sklearn.model_selection import validation_curve train_scores, test_scores = validation_curve( ...)
How to build a learning curve with sklearn?
from sklearn.model_selection import learning_curve
Statistical word to describe a problem an overfitting model causes, explain
Variance The models trained of different samples from the population vary a lot (unstable models).
Statistical word to describe a problem an underfitting model causes, explain
Bias The models trained of different samples from the population are consistently biased (systematic errors).
If model is not overfitting or underfitting, why would we still get bad performance?
If we don't have access to important features
Example of a model with low number of parameters but high variance
The model has only two parameters (a, b) but it can interpolate any number of points by oscillating with a high enough frequency.
When Randomized Search is generally recommended over Grid Search?
When 3 or more hyperparameters are being optimized
Good number of iterations for Randomized Search
500
What kind of plot is useful to explore importance of multiple hyperparameters
Parallel coordinates plot
What we can do with parallel coordinates plot to explore hyperparameter importances?
We can select ranges of several hyperparameters and see if there are ranges on some hyperparameters that prevent the highest scores which hyperparameters don't affect the score
After you defined a GridSearch, what do you do next?
You need to fit it: train the model with each combination of parameters