8000 rebase on top of #4347 · scikit-learn/scikit-learn@0166186 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0166186

Browse files
rebase on top of #4347
1 parent cabd2ca commit 0166186

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

doc/whats_new.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Enhancements
6161
option, which has a simpler forumlar and interpretation.
6262
By Hanna Wallach and `Andreas Müller`_.
6363

64+
- Add ``class_weight`` parameter to automatically weight samples by class
65+
frequency for :class:`linear_model.PassiveAgressiveClassifier`. By
66+
`Trevor Stephens`_.
67+
6468
- Added backlinks from the API reference pages to the user guide. By
6569
`Andreas Müller`_.
6670

@@ -572,7 +576,7 @@ API changes summary
572576

573577
- The ``shuffle`` option of :class:`.linear_model.SGDClassifier`,
574578
:class:`linear_model.SGDRegressor`, :class:`linear_model.Perceptron`,
575-
:class:`linear_model.PassiveAgressiveClassivier` and
579+
:class:`linear_model.PassiveAgressiveClassifier` and
576580
:class:`linear_model.PassiveAgressiveRegressor` now defaults to ``True``.
577581

578582
- :class:`cluster.DBSCAN` now uses a deterministic initialization. The

sklearn/linear_model/passive_aggressive.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ class PassiveAggressiveClassifier(BaseSGDClassifier):
4949
When set to True, reuse the solution of the previous call to fit as
5050
initialization, otherwise, just erase the previous solution.
5151
52-
class_weight : dict, {class_label: weight} or "auto" or None, optional
52+
class_weight : dict, {class_label: weight} or "balanced" or None, optional
5353
Preset for the class_weight fit parameter.
5454
5555
Weights associated with classes. If not given, all classes
5656
are supposed to have weight one.
5757
58-
The "auto" mode uses the values of y to automatically adjust
59-
weights inversely proportional to class frequencies.
58+
The "balanced" mode uses the values of y to automatically adjust
59+
weights inversely proportional to class frequencies in the input data
60+
as ``n_samples / (n_classes * np.bincount(y))``
6061
6162
Attributes
6263
----------
@@ -120,10 +121,11 @@ def partial_fit(self, X, y, classes=None):
120121
-------
121122
self : returns an instance of self.
122123
"""
123-
if self.class_weight == 'auto':
124-
raise ValueError("class_weight 'auto' is not supported for "
125-
"partial_fit. In order to use 'auto' weights, "
126-
"use compute_class_weight('auto', classes, y). "
124+
if self.class_weight == 'balanced':
125+
raise ValueError("class_weight 'balanced' is not supported for "
126+
"partial_fit. In order to use 'balanced' "
127+
"weights, use "
128+
"compute_class_weight('balanced', classes, y). "
127129
"In place of y you can us a large enough sample "
128130
"of the full training set target to properly "
129131
"estimate the class frequency distributions. "

sklearn/linear_model/tests/test_passive_aggressive.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ def test_class_weights():
148148
assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([-1]))
149149

150150

151-
def test_partial_fit_weight_class_auto():
152-
# partial_fit with class_weight='auto' not supported
153-
clf = PassiveAggressiveClassifier(class_weight="auto")
151+
def test_partial_fit_weight_class_balanced():
152+
# partial_fit with class_weight='balanced' not supported
153+
clf = PassiveAggressiveClassifier(class_weight="balanced")
154154
assert_raises(ValueError, clf.partial_fit, X, y, classes=np.unique(y))
155155

156156

@@ -160,18 +160,18 @@ def test_equal_class_weight():
160160
clf = PassiveAggressiveClassifier(C=0.1, n_iter=1000, class_weight=None)
161161
clf.fit(X2, y2)
162162

163-
# Already balanced, so "auto" weights should have no effect
164-
clf_auto = PassiveAggressiveClassifier(C=0.1, n_iter=1000,
165-
class_weight="auto")
166-
clf_auto.fit(X2, y2)
163+
# Already balanced, so "balanced" weights should have no effect
164+
clf_balanced = PassiveAggressiveClassifier(C=0.1, n_iter=1000,
165+
class_weight="balanced")
166+
clf_balanced.fit(X2, y2)
167167

168168
clf_weighted = PassiveAggressiveClassifier(C=0.1, n_iter=1000,
169169
class_weight={0: 0.5, 1: 0.5})
170170
clf_weighted.fit(X2, y2)
171171

172172
# should be similar up to some epsilon due to learning rate schedule
173173
assert_almost_equal(clf.coef_, clf_weighted.coef_, decimal=2)
174-
assert_almost_equal(clf.coef_, clf_auto.coef_, decimal=2)
174+
assert_almost_equal(clf.coef_, clf_balanced.coef_, decimal=2)
175175

176176

177177
def test_wrong_class_weight_label():

0 commit comments

Comments
 (0)
0