An introduction to machine learning with scikit-learn
Section contents
In this section, we introduce the machine learning vocabulary that we use throughout scikit-learn and give a simple learning example.
Machine learning: the problem setting
In general, a learning problem considers a set of n samples of data and then tries to predict properties of unknown data. If each sample is more than a single number and, for instance, a multi-dimensional entry (aka multivariate data), it is said to have several attributes or features.
We can separate learning problems in a few large categories:
supervised learning, in which the data comes with additional attributes that we want to predict (Click here to go to the scikit-learn supervised learning page).This problem can be either:
- classification: samples belong to two or more classes and we want to learn from already labeled data how to predict the class of unlabeled data. An example of classification problem would be the handwritten digit recognition example, in which the aim is to assign each input vector to one of a finite number of discrete categories. Another way to think of classification is as a discrete (as opposed to continuous) form of supervised learning where one has a limited number of categories and for each of the n samples provided, one is to try to label them with the correct category or class.
- regression: if the desired output consists of one or more continuous variables, then the task is called regression. An example of a regression problem would be the prediction of the length of a salmon as a function of its age and weight.
unsupervised learning, in which the training data consists of a set of input vectors x without any corresponding target values. The goal in such problems may be to discover groups of similar examples within the data, where it is called clustering, or to determine the distribution of data within the input space, known as density estimation, or to project the data from a high-dimensional space down to two or three dimensions for the purpose of visualization (Click here to go to the Scikit-Learn unsupervised learning page).
Training set and testing set
Machine learning is about learning some properties of a data set and applying them to new data. This is why a common practice in machine learning to evaluate an algorithm is to split the data at hand into two sets, one that we call the training set on which we learn data properties and one that we call the testing set on which we test these properties.
Loading an example dataset
scikit-learn comes with a few standard datasets, for instance the iris and digits datasets for classification and the boston house prices dataset for regression.
In the following, we start a Python interpreter from our shell and then load the iris
and digits
datasets. Our notational convention is that $
denotes the shell prompt while >>>
denotes the Python interpreter prompt:
A dataset is a dictionary-like object that holds all the data and some metadata about the data. This data is stored in the .data
member, which is a n_samples, n_features
array. In the case of supervised problem, one or more response variables are stored in the .target
member. More details on the different datasets can be found in the dedicated section.
For instance, in the case of the digits dataset, digits.data
gives access to the features that can be used to classify the digits samples:
and digits.target
gives the ground truth for the digit dataset, that is the number corresponding to each digit image that we are trying to learn:
Shape of the data arrays
The data is always a 2D array, shape (n_samples, n_features)
, although the original data may have had a different shape. In the case of the digits, each original sample is an image of shape(8, 8)
and can be accessed using:
The simple example on this dataset illustrates how starting from the original problem one can shape the data for consumption in scikit-learn.
Learning and predicting
In the case of the digits dataset, the task is to predict, given an image, which digit it represents. We are given samples of each of the 10 possible classes (the digits zero through nine) on which we fitan estimator to be able to predict the classes to which unseen samples belong.
In scikit-learn, an estimator for classification is a Python object that implements the methodsfit(X, y)
and predict(T)
.
An example of an estimator is the class sklearn.svm.SVC
that implements support vector classification. The constructor of an estimator takes as arguments the parameters of the model, but for the time being, we will consider the estimator as a black box:
Choosing the parameters of the model
In this example we set the value of gamma
manually. It is possible to automatically find good values for the parameters by using tools such as grid search and cross validation.
We call our estimator instance clf
, as it is a classifier. It now must be fitted to the model, that is, it must learn from the model. This is done by passing our training set to the fit
method. As a training set, let us use all the images of our dataset apart from the last one. We select this training set with the [:-1]
Python syntax, which produces a new array that contains all but the last entry ofdigits.data
:
Now you can predict new values, in particular, we can ask to the classifier what is the digit of our last image in the digits
dataset, which we have not used to train the classifier:
The corresponding image is the following:
![../../_images/sphx_glr_plot_digits_last_image_001.png](https://i-blog.csdnimg.cn/blog_migrate/c977d9acc433aa7753ce5f13f05b0162.png)
As you can see, it is a challenging task: the images are of poor resolution. Do you agree with the classifier?
A complete example of this classification problem is available as an example that you can run and study: Recognizing hand-written digits.
Model persistence
It is possible to save a model in the scikit by using Python’s built-in persistence model, namelypickle:
In the specific case of the scikit, it may be more interesting to use joblib’s replacement of pickle (joblib.dump
& joblib.load
), which is more efficient on big data, but can only pickle to the disk and not to a string:
Later you can load back the pickled model (possibly in another Python process) with:
Note
joblib.dump
and joblib.load
functions also accept file-like object instead of filenames. More information on data persistence with Joblib is available here.
Note that pickle has some security and maintainability issues. Please refer to section Model persistence for more detailed information about model persistence with scikit-learn.
Conventions
scikit-learn estimators follow certain rules to make their behavior more predictive.
Type casting
Unless otherwise specified, input will be cast to float64
:
In this example, X
is float32
, which is cast to float64
by fit_transform(X)
.
Regression targets are cast to float64
, classification targets are maintained:
Here, the first predict()
returns an integer array, since iris.target
(an integer array) was used infit
. The second predict()
returns a string array, since iris.target_names
was for fitting.
Refitting and updating parameters
Hyper-parameters of an estimator can be updated after it has been constructed via thesklearn.pipeline.Pipeline.set_params
method. Calling fit()
more than once will overwrite what was learned by any previous fit()
:
Here, the default kernel rbf
is first changed to linear
after the estimator has been constructed via SVC()
, and changed back to rbf
to refit the estimator and to make a second prediction.
from: http://scikit-learn.org/stable/tutorial/basic/tutorial.html