justmarkham/scikit-learn-tips

πŸ€–βš‘ 50 scikit-learn tips

Jupyter Notebook

1,747

93 commits

updated Sep 5, 2022

See the code

README

πŸ€–βš‘ scikit-learn tips

New tips are posted on LinkedIn, Twitter, and Facebook.

πŸ‘‰ Sign up to receive 2 video tips by email every week! πŸ‘ˆ

List of all tips

Click to discuss the tip on LinkedIn, click to view the Jupyter notebook for a tip, or click to watch the tip video on YouTube:

#DescriptionLinks
1Use ColumnTransformer to apply different preprocessing to different columns
2Seven ways to select columns using ColumnTransformer
3What is the difference between "fit" and "transform"?
4Use "fit_transform" on training data, but "transform" (only) on testing/new data
5Four reasons to use scikit-learn (not pandas) for ML preprocessing
6Encode categorical features using OneHotEncoder or OrdinalEncoder
7Handle unknown categories with OneHotEncoder by encoding them as zeros
8Use Pipeline to chain together multiple steps
9Add a missing indicator to encode "missingness" as a feature
10Set a "random_state" to make your code reproducible
11Impute missing values using KNNImputer or IterativeImputer
12What is the difference between Pipeline and make_pipeline?
13Examine the intermediate steps in a Pipeline
14HistGradientBoostingClassifier natively supports missing values
15Three reasons not to use drop='first' with OneHotEncoder
16Use cross_val_score and GridSearchCV on a Pipeline
17Try RandomizedSearchCV if GridSearchCV is taking too long
18Display GridSearchCV or RandomizedSearchCV results in a DataFrame
19Important tuning parameters for LogisticRegression
20Plot a confusion matrix
21Compare multiple ROC curves in a single plot
22Use the correct methods for each type of Pipeline
23Display the intercept and coefficients for a linear model
24Visualize a decision tree two different ways
25Prune a decision tree to avoid overfitting
26Use stratified sampling with train_test_split
27Two ways to impute missing values for a categorical feature
28Save a model or Pipeline using joblib
29Vectorize two text columns in a ColumnTransformer
30Four ways to examine the steps of a Pipeline
31Shuffle your dataset when using cross_val_score
32Use AUC to evaluate multiclass problems
33Use FunctionTransformer to convert functions into transformers
34Add feature selection to a Pipeline
35Don't use .values when passing a pandas object to scikit-learn
36Most parameters should be passed as keyword arguments
37Create an interactive diagram of a Pipeline in Jupyter
38Get the feature names output by a ColumnTransformer
39Load a toy dataset into a DataFrame
40Estimators only print parameters that have been changed
41Drop the first category from binary features (only) with OneHotEncoder
42Passthrough some columns and drop others in a ColumnTransformer
43Use OrdinalEncoder instead of OneHotEncoder with tree-based models
44Speed up GridSearchCV using parallel processing
45Create feature interactions using PolynomialFeatures
46Ensemble multiple models using VotingClassifer or VotingRegressor
47Tune the parameters of a VotingClassifer or VotingRegressor
48Access part of a Pipeline using slicing
49Tune multiple models simultaneously with GridSearchCV
50Adapt this pattern to solve many Machine Learning problems

You can interact with all of these notebooks online using Binder:

Note: Some of the tips do not include any code, and can only be viewed on LinkedIn.

Who creates these tips?

Hi! I'm Kevin Markham, the founder of Data School. I've been teaching data science in Python since 2014. I create these tips because I love using scikit-learn and I want to help others use it more effectively.

How can I get better at scikit-learn?

I teach three courses:

πŸ‘‰ Find out which course is right for you! πŸ‘ˆ

Do you have any other tips?

Yes! In 2019, I posted 100 pandas tricks. I also created a video featuring my top 25 pandas tricks.

Β© 2020-2021 Data School. All rights reserved.

data-school
data-science
machine-learning
python
scikit-learn

justmarkham/scikit-learn-tips

πŸ€–βš‘ 50 scikit-learn tips

Jupyter Notebook

1,747

93 commits

updated Sep 5, 2022

See the code

README

πŸ€–βš‘ scikit-learn tips

New tips are posted on LinkedIn, Twitter, and Facebook.

πŸ‘‰ Sign up to receive 2 video tips by email every week! πŸ‘ˆ

List of all tips

Click to discuss the tip on LinkedIn, click to view the Jupyter notebook for a tip, or click to watch the tip video on YouTube:

#DescriptionLinks
1Use ColumnTransformer to apply different preprocessing to different columns
2Seven ways to select columns using ColumnTransformer
3What is the difference between "fit" and "transform"?
4Use "fit_transform" on training data, but "transform" (only) on testing/new data
5Four reasons to use scikit-learn (not pandas) for ML preprocessing
6Encode categorical features using OneHotEncoder or OrdinalEncoder
7Handle unknown categories with OneHotEncoder by encoding them as zeros
8Use Pipeline to chain together multiple steps
9Add a missing indicator to encode "missingness" as a feature
10Set a "random_state" to make your code reproducible
11Impute missing values using KNNImputer or IterativeImputer
12What is the difference between Pipeline and make_pipeline?
13Examine the intermediate steps in a Pipeline
14HistGradientBoostingClassifier natively supports missing values
15Three reasons not to use drop='first' with OneHotEncoder
16Use cross_val_score and GridSearchCV on a Pipeline
17Try RandomizedSearchCV if GridSearchCV is taking too long
18Display GridSearchCV or RandomizedSearchCV results in a DataFrame
19Important tuning parameters for LogisticRegression
20Plot a confusion matrix
21Compare multiple ROC curves in a single plot
22Use the correct methods for each type of Pipeline
23Display the intercept and coefficients for a linear model
24Visualize a decision tree two different ways
25Prune a decision tree to avoid overfitting
26Use stratified sampling with train_test_split
27Two ways to impute missing values for a categorical feature
28Save a model or Pipeline using joblib
29Vectorize two text columns in a ColumnTransformer
30Four ways to examine the steps of a Pipeline
31Shuffle your dataset when using cross_val_score
32Use AUC to evaluate multiclass problems
33Use FunctionTransformer to convert functions into transformers
34Add feature selection to a Pipeline
35Don't use .values when passing a pandas object to scikit-learn
36Most parameters should be passed as keyword arguments
37Create an interactive diagram of a Pipeline in Jupyter
38Get the feature names output by a ColumnTransformer
39Load a toy dataset into a DataFrame
40Estimators only print parameters that have been changed
41Drop the first category from binary features (only) with OneHotEncoder
42Passthrough some columns and drop others in a ColumnTransformer
43Use OrdinalEncoder instead of OneHotEncoder with tree-based models
44Speed up GridSearchCV using parallel processing
45Create feature interactions using PolynomialFeatures
46Ensemble multiple models using VotingClassifer or VotingRegressor
47Tune the parameters of a VotingClassifer or VotingRegressor
48Access part of a Pipeline using slicing
49Tune multiple models simultaneously with GridSearchCV
50Adapt this pattern to solve many Machine Learning problems

You can interact with all of these notebooks online using Binder:

Note: Some of the tips do not include any code, and can only be viewed on LinkedIn.

Who creates these tips?

Hi! I'm Kevin Markham, the founder of Data School. I've been teaching data science in Python since 2014. I create these tips because I love using scikit-learn and I want to help others use it more effectively.

How can I get better at scikit-learn?

I teach three courses:

πŸ‘‰ Find out which course is right for you! πŸ‘ˆ

Do you have any other tips?

Yes! In 2019, I posted 100 pandas tricks. I also created a video featuring my top 25 pandas tricks.

Β© 2020-2021 Data School. All rights reserved.

data-school
data-science
machine-learning
python
scikit-learn

Significant stargazers

Media

65 followers Β· starred May 2020

Carlo Lemos

35 followers Β· starred Oct 2021

Alexander Popkov

32 followers Β· starred Dec 2022

JoΓ£o G. Atkinson A.

69 followers Β· starred Jan 2021