-
-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MRG + 1] Add conventions section to userguide. #4508 #4566
Changes from 6 commits
6df4eac
e00522c
2065707
7c05815
6035c98
5831524
234be23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,3 +250,95 @@ Note that pickle has some security and maintainability issues. Please refer to | |
section :ref:`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``:: | ||
|
||
>>> import numpy as np | ||
>>> from sklearn import random_projection | ||
|
||
>>> X = np.random.rand(10, 2000) | ||
>>> X = np.array(X, dtype='float32') | ||
>>> X.dtype | ||
dtype('float32') | ||
|
||
>>> transformer = random_projection.GaussianRandomProjection() | ||
>>> X_new = transformer.fit_transform(X) | ||
>>> X_new.dtype | ||
dtype('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:: | ||
>>> from sklearn import datasets | ||
>>> from sklearn.svm import SVC | ||
|
||
>>> iris = datasets.load_iris() | ||
>>> clf = SVC() | ||
>>> clf.fit(iris.data, iris.target) | ||
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, | ||
kernel='rbf', max_iter=-1, probability=False, random_state=None, | ||
shrinking=True, tol=0.001, verbose=False) | ||
|
||
>>> clf.predict(iris.data[:3]) | ||
array([0, 0, 0]) | ||
|
||
>>> clf.fit(iris.data, iris.target_names[iris.target]) | ||
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, | ||
kernel='rbf', max_iter=-1, probability=False, random_state=None, | ||
shrinking=True, tol=0.001, verbose=False) | ||
|
||
>>> clf.predict(iris.data[:3]) # doctest: +NORMALIZE_WHITESPACE | ||
array(['setosa', 'setosa', 'setosa'], dtype='<U10') | ||
|
||
Here, the first ``predict()`` returns an integer array, since ``iris.target`` | ||
(an integer array) was used in ``fit``. 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 by | ||
changing the corresponding member variables. Calling ``fit()`` more than once | ||
will overwrite what was learned by any previous ``fit()``:: | ||
|
||
>>> import numpy as np | ||
>>> from sklearn.svm import SVC | ||
|
||
>>> np.random.seed(0) | ||
>>> X = np.random.rand(100, 10) | ||
>>> y = np.random.binomial(1, 0.5, 100) | ||
>>> XX = np.random.rand(5, 10) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a PRNG instance to make the execution deterministic.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also rename |
||
|
||
>>> clf = SVC() | ||
>>> clf.kernel = 'linear' | ||
>>> clf.fit(X, y) | ||
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, | ||
kernel='linear', max_iter=-1, probability=False, random_state=None, | ||
shrinking=True, tol=0.001, verbose=False) | ||
>>> clf.predict(XX) | ||
array([1, 0, 1, 1, 0]) | ||
|
||
>>> clf.kernel = 'rbf' | ||
>>> clf.fit(X, y) | ||
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, | ||
kernel='rbf', max_iter=-1, probability=False, random_state=None, | ||
shrinking=True, tol=0.001, verbose=False) | ||
>>> clf.predict(XX) | ||
array([0, 0, 0, 1, 0]) | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please advertise the use of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is causing the test to fail on old versions of Python. To avoid that we could do: