Now that you have learned some of the basics of object-oriented programming with scikit-learn, let's practice applying it!
In this lesson you will practice:
- Recall the distinction between mutable and immutable types
- Define the four main inherited object types in scikit-learn
- Instantiate scikit-learn transformers and models
- Invoke scikit-learn methods
- Access scikit-learn attributes
For each example below, think to yourself whether it is a mutable or immutable type. Then expand the details tag to reveal the answer.
-
Python dictionary (click to reveal)
Mutable. For example, the `update` method can be used to modify values within a dictionary.
-
Python tuple (click to reveal)
Immutable. If you want to create a modified version of a tuple, you need to use
=
to reassign it. -
pandas
DataFrame
(click to reveal)Mutable. Using the
inplace=True
argument with various different methods allows you to modify a dataframe in place. -
scikit-learn
OneHotEncoder
(click to reveal)Mutable. Calling the
fit
method causes the transformer to store information about the data that is passed in, modifying its internal attributes.
For this lab we'll use data from the built-in iris dataset:
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True, as_frame=True)
X
y
For the following exercises, follow the documentation link to understand the class you are working with, but do not worry about understanding the underlying algorithm. The goal is just to get used to creating and using these types of objects.
For all estimators, the steps are:
- Import the class from the
sklearn
library - Instantiate an object from the class
- Pass in the appropriate data to the
fit
method
MinMaxScaler
(documentation here)
Import this scaler, instantiate an object called scaler
with default parameters, and fit
the scaler on X
.
# Import
# Instantiate
# Fit
DecisionTreeClassifier
(documentation here)
Import the classifier, instantiate an object called clf
(short for "classifier") with default parameters, and fit
the classifier on X
and y
.
# Import
# Instantiate
# Fit
One of the two objects instantiated above (scaler
or clf
) is a transformer. Which one is it? Consult the documentation.
Hint (click to reveal)
The class with a transform
method is a transformer.
Using the transformer, print out two of the fitted attributes along with descriptions from the documentation.
Hint (click to reveal)
Attributes ending with _
are fitted attributes.
# Your code here
# Your code here
The other of the two scikit-learn objects instantiated above (scaler
or clf
) is a predictor and a model. Which one is it? Consult the documentation.
Hint (click to reveal)
The class with a predict
method and a score
method is a predictor and a model.
Using the predictor, print out two of the fitted attributes along with descriptions from the documentation.
# Your code here
# Your code here
# Your code here
"""
Your answer here
"""
In this lab, you practiced identifying mutable and immutable types as well as identifying transformers, predictors, and models using scikit-learn. You also instantiated scikit-learn objects, invoked the most common scikit-learn methods, and accessed some scikit-learn attributes.