2011-12-18 19:39:53 +08:00
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
==========================================================================
|
|
|
|
|
Statistical learning: the setting and the estimator object in scikit-learn
|
|
|
|
|
==========================================================================
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
Datasets
|
|
|
|
|
=========
|
|
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
Scikit-learn deals with learning information from one or more
|
2011-12-18 19:39:53 +08:00
|
|
|
datasets that are represented as 2D arrays. They can be understood as a
|
|
|
|
|
list of multi-dimensional observations. We say that the first axis of
|
|
|
|
|
these arrays is the **samples** axis, while the second is the
|
|
|
|
|
**features** axis.
|
|
|
|
|
|
2017-11-14 18:06:50 +08:00
|
|
|
.. topic:: A simple example shipped with scikit-learn: iris dataset
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
>>> from sklearn import datasets
|
2011-12-18 19:39:53 +08:00
|
|
|
>>> iris = datasets.load_iris()
|
|
|
|
|
>>> data = iris.data
|
|
|
|
|
>>> data.shape
|
|
|
|
|
(150, 4)
|
|
|
|
|
|
|
|
|
|
It is made of 150 observations of irises, each described by 4
|
|
|
|
|
features: their sepal and petal length and width, as detailed in
|
2014-07-06 19:33:20 +08:00
|
|
|
``iris.DESCR``.
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
When the data is not initially in the ``(n_samples, n_features)`` shape, it
|
|
|
|
|
needs to be preprocessed in order to be used by scikit-learn.
|
2011-12-18 19:39:53 +08:00
|
|
|
|
[MRG+1] Fix: Replace pylab with matplotlib.pyplot #6754 (#6762)
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 22 occurrences of pylab replaced with matplotlib.pyplot
- bench_glm.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 21 remaining occurrences of pylab replaced with
matplotlib.pyplot
- bench_glmnet.py now free of pylab references
- code does not execute for extraneous reason: ImportError: No module named
glmnet.elastic_net
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 19 occurrences of pylab replaced with matplotlib.pyplot
- bench_lasso.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 18 occurrences of pylab replaced with matplotlib.pyplot
- bench_plot_neighbors.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 17 occurrences of pylab replaced with matplotlib.pyplot
- bench_plot_omp_lars.py now free of pylab references
- code does not execute for extraneous reasons:
- File "bench_plot_omp_lars.py", line 111, in <module>
- ax = fig.add_subplot(1, 2, i)
- ValueError: num must be 1 <= num <= 2, not 0
- line 111 should probably be ax = fig.add_subplot(1, 2, i+1)
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_plot_parallel_pairwise.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_plot_ward.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_sgd_regression.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_tree.py now free of pylab references
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_glm.py clean
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_glm.py clean of pl
- code does not execute for extraneous reasons
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_lasso.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_neighbors.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_omp_lars.py clean of pl
- code does not execute for extraneous reasons
* fix: Fix bug that prevented graphs from displaying
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_parallel_pairwise.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_ward.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_sgd_regression.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_tree.py clean of pl
- code executes properly
* docs: removed pylab references from comments
* docs: removed all pylab references
- replaced with matplotlib.pyplot
- pl --> plt
* docs: removed pylab references from comments
- replaced with matplotlib.pyplot
- pl --> plt
* docs: removed all pylab references
- replaced with matplotlib.pyplot
- pl --> plt
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- mlcomp_sparse_document_classification.py clean of pl
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- plot_gpr_noisy_targets.py clean of pl
- code does not execute for extraneous reasons
- File "examples/gaussian_process/plot_gpr_noisy_targets.py", line 31, in
<module>
- from sklearn.gaussian_process import GaussianProcessRegressor
- ImportError: cannot import name GaussianProcessRegressor
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- plot_gpc_isoprobability.py clean of pl
- code does not execute for extraneous reasons
- File "examples/gaussian_process/plot_gpc_isoprobability.py", line 24, in
<module>
- from sklearn.gaussian_process import GaussianProcessClassifier
- ImportError: cannot import name GaussianProcessClassifier
* docs: removed all pylab references
- replaced with matplotlib.pyplot
- pl --> plt
* docs: removed all pylab references
- replaced with matplotlib.pyplot
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- plot_sparse_coding.py clean of pl
- code executes properly
* docs: removed all pylab references
- replaced with matplotlib.pyplot
* docs: removed all pylab references
- replaced with matplotlib.pyplot
* style: Indent properly
* style: indent properly
* style: Indent properly
* docs: Add missing .pyplot
* docs: Fix typo
* style: Indent properly
2016-05-10 17:34:33 +08:00
|
|
|
.. topic:: An example of reshaping data would be the digits dataset
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
The digits dataset is made of 1797 8x8 images of hand-written
|
|
|
|
|
digits ::
|
|
|
|
|
|
|
|
|
|
>>> digits = datasets.load_digits()
|
|
|
|
|
>>> digits.images.shape
|
|
|
|
|
(1797, 8, 8)
|
[MRG+1] Fix: Replace pylab with matplotlib.pyplot #6754 (#6762)
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 22 occurrences of pylab replaced with matplotlib.pyplot
- bench_glm.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 21 remaining occurrences of pylab replaced with
matplotlib.pyplot
- bench_glmnet.py now free of pylab references
- code does not execute for extraneous reason: ImportError: No module named
glmnet.elastic_net
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 19 occurrences of pylab replaced with matplotlib.pyplot
- bench_lasso.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 18 occurrences of pylab replaced with matplotlib.pyplot
- bench_plot_neighbors.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- one instance of 17 occurrences of pylab replaced with matplotlib.pyplot
- bench_plot_omp_lars.py now free of pylab references
- code does not execute for extraneous reasons:
- File "bench_plot_omp_lars.py", line 111, in <module>
- ax = fig.add_subplot(1, 2, i)
- ValueError: num must be 1 <= num <= 2, not 0
- line 111 should probably be ax = fig.add_subplot(1, 2, i+1)
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_plot_parallel_pairwise.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_plot_ward.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_sgd_regression.py now free of pylab references
- code executes properly
* Fix: Replace pylab with matplotlib.pyplot #6754
- bench_tree.py now free of pylab references
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_glm.py clean
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_glm.py clean of pl
- code does not execute for extraneous reasons
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_lasso.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_neighbors.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_omp_lars.py clean of pl
- code does not execute for extraneous reasons
* fix: Fix bug that prevented graphs from displaying
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_parallel_pairwise.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_plot_ward.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_sgd_regression.py clean of pl
- code executes properly
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- bench_tree.py clean of pl
- code executes properly
* docs: removed pylab references from comments
* docs: removed all pylab references
- replaced with matplotlib.pyplot
- pl --> plt
* docs: removed pylab references from comments
- replaced with matplotlib.pyplot
- pl --> plt
* docs: removed all pylab references
- replaced with matplotlib.pyplot
- pl --> plt
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- mlcomp_sparse_document_classification.py clean of pl
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- plot_gpr_noisy_targets.py clean of pl
- code does not execute for extraneous reasons
- File "examples/gaussian_process/plot_gpr_noisy_targets.py", line 31, in
<module>
- from sklearn.gaussian_process import GaussianProcessRegressor
- ImportError: cannot import name GaussianProcessRegressor
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- plot_gpc_isoprobability.py clean of pl
- code does not execute for extraneous reasons
- File "examples/gaussian_process/plot_gpc_isoprobability.py", line 24, in
<module>
- from sklearn.gaussian_process import GaussianProcessClassifier
- ImportError: cannot import name GaussianProcessClassifier
* docs: removed all pylab references
- replaced with matplotlib.pyplot
- pl --> plt
* docs: removed all pylab references
- replaced with matplotlib.pyplot
* refactor: Replace pl with plt
- replace instances of pl (as on import pylab as pl)
with plt (as in import matplotlib.pyplot as plt)
- plot_sparse_coding.py clean of pl
- code executes properly
* docs: removed all pylab references
- replaced with matplotlib.pyplot
* docs: removed all pylab references
- replaced with matplotlib.pyplot
* style: Indent properly
* style: indent properly
* style: Indent properly
* docs: Add missing .pyplot
* docs: Fix typo
* style: Indent properly
2016-05-10 17:34:33 +08:00
|
|
|
>>> import matplotlib.pyplot as plt #doctest: +SKIP
|
2020-08-29 22:32:33 +08:00
|
|
|
>>> plt.imshow(digits.images[-1],
|
|
|
|
|
... cmap=plt.cm.gray_r) #doctest: +SKIP
|
2011-12-18 19:39:53 +08:00
|
|
|
<matplotlib.image.AxesImage object at ...>
|
2020-07-14 17:26:25 +08:00
|
|
|
|
|
|
|
|
.. image:: /auto_examples/datasets/images/sphx_glr_plot_digits_last_image_001.png
|
|
|
|
|
:target: ../../auto_examples/datasets/plot_digits_last_image.html
|
2020-08-29 22:32:33 +08:00
|
|
|
:align: center
|
|
|
|
|
|
2017-11-14 18:06:50 +08:00
|
|
|
To use this dataset with scikit-learn, we transform each 8x8 image into a
|
2011-12-18 19:39:53 +08:00
|
|
|
feature vector of length 64 ::
|
|
|
|
|
|
2020-08-29 22:32:33 +08:00
|
|
|
>>> data = digits.images.reshape(
|
|
|
|
|
... (digits.images.shape[0], -1)
|
|
|
|
|
... )
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
Estimators objects
|
|
|
|
|
===================
|
|
|
|
|
|
|
|
|
|
.. Some code to make the doctests run
|
|
|
|
|
|
2012-03-06 00:18:05 +08:00
|
|
|
>>> from sklearn.base import BaseEstimator
|
2011-12-18 19:39:53 +08:00
|
|
|
>>> class Estimator(BaseEstimator):
|
|
|
|
|
... def __init__(self, param1=0, param2=0):
|
|
|
|
|
... self.param1 = param1
|
|
|
|
|
... self.param2 = param2
|
|
|
|
|
... def fit(self, data):
|
|
|
|
|
... pass
|
|
|
|
|
>>> estimator = Estimator()
|
|
|
|
|
|
2012-08-04 07:21:40 +08:00
|
|
|
**Fitting data**: the main API implemented by scikit-learn is that of the
|
|
|
|
|
`estimator`. An estimator is any object that learns from data;
|
2012-12-06 09:20:28 +08:00
|
|
|
it may be a classification, regression or clustering algorithm or
|
2014-07-06 19:33:20 +08:00
|
|
|
a *transformer* that extracts/filters useful features from raw data.
|
2012-08-04 07:21:40 +08:00
|
|
|
|
2014-07-06 19:33:20 +08:00
|
|
|
All estimator objects expose a ``fit`` method that takes a dataset
|
2012-08-04 07:21:40 +08:00
|
|
|
(usually a 2-d array):
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
>>> estimator.fit(data)
|
|
|
|
|
|
|
|
|
|
**Estimator parameters**: All the parameters of an estimator can be set
|
2012-12-06 09:20:28 +08:00
|
|
|
when it is instantiated or by modifying the corresponding attribute::
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
>>> estimator = Estimator(param1=1, param2=2)
|
|
|
|
|
>>> estimator.param1
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
|
**Estimated parameters**: When data is fitted with an estimator,
|
|
|
|
|
parameters are estimated from the data at hand. All the estimated
|
|
|
|
|
parameters are attributes of the estimator object ending by an
|
|
|
|
|
underscore::
|
|
|
|
|
|
|
|
|
|
>>> estimator.estimated_param_ #doctest: +SKIP
|