-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG + 1] Add conventions section to userguide. #4508 #4566
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6df4eac
Update documentation of predict_proba in tree module
cangermueller e00522c
Merge remote-tracking branch 'upstream/master'
cangermueller 2065707
Merge remote-tracking branch 'upstream/master'
cangermueller 7c05815
Merge remote-tracking branch 'upstream/master'
cangermueller 6035c98
Add conventions section to user guide
cangermueller 5831524
Update conventions section in userguide
cangermueller 234be23
Use RandomState() in tutorial and rename variables
cangermueller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,3 +250,102 @@ 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 | ||
|
||
>>> rng = np.random.RandomState(0) | ||
>>> X = rng.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 | ||
via the :func:`sklearn.pipeline.Pipeline.set_params` method. Calling ``fit()`` | ||
more than once will overwrite what was learned by any previous ``fit()``:: | ||
|
||
>>> import numpy as np | ||
>>> from sklearn.svm import SVC | ||
|
||
>>> rng = np.random.RandomState(0) | ||
>>> X = rng.rand(100, 10) | ||
>>> y = rng.binomial(1, 0.5, 100) | ||
>>> X_test = rng.rand(5, 10) | ||
|
||
>>> clf = SVC() | ||
>>> clf.set_params(kernel='linear') | ||
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.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(X_test) | ||
array([1, 0, 1, 1, 0]) | ||
|
||
>>> clf.set_params(kernel='rbf') | ||
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.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(X_test) | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: