8000 [issue #5089] Fixed warnings for DataDimensionalityWarning, decision_function and decision_function_shape. by hasancansaral · Pull Request #5277 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

[issue #5089] Fixed warnings for DataDimensionalityWarning, decision_function and decision_function_shape. #5277

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sklearn/ensemble/tests/test_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ def fit(self, X, y):
for f in ['predict', 'predict_proba', 'predict_log_proba', 'decision_function']:
# Trained on sparse format
sparse_classifier = BaggingClassifier(
base_estimator=CustomSVC(),
base_estimator=CustomSVC(decision_function_shape='ovr'),
random_state=1,
**params
).fit(X_train_sparse, y_train)
sparse_results = getattr(sparse_classifier, f)(X_test_sparse)

# Trained on dense format
dense_classifier = BaggingClassifier(
base_estimator=CustomSVC(),
base_estimator=CustomSVC(decision_function_shape='ovr'),
random_state=1,
**params
).fit(X_train, y_train)
Expand Down Expand Up @@ -439,7 +439,7 @@ def test_parallel_classification():
assert_array_almost_equal(y1, y3)

# decision_function
ensemble = BaggingClassifier(SVC(),
ensemble = BaggingClassifier(SVC(decision_function_shape='ovr'),
n_jobs=3,
random_state=0).fit(X_train, y_train)

Expand All @@ -449,7 +449,7 @@ def test_parallel_classification():
decisions2 = ensemble.decision_function(X_test)
assert_array_almost_equal(decisions1, decisions2)

ensemble = BaggingClassifier(SVC(),
ensemble = BaggingClassifier(SVC(decision_function_shape='ovr'),
n_jobs=1,
random_state=0).fit(X_train, y_train)

Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/tests/test_least_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def test_multitarget():
for estimator in (linear_model.LassoLars(), linear_model.Lars()):
estimator.fit(X, Y)
Y_pred = estimator.predict(X)
Y_dec = estimator.decision_function(X)
Y_dec = ignore_warnings(estimator.decision_function)(X)
assert_array_almost_equal(Y_pred, Y_dec)
alphas, active, coef, path = (estimator.alphas_, estimator.active_,
estimator.coef_, estimator.coef_path_)
Expand Down
3 changes: 2 additions & 1 deletion sklearn/linear_model/tests/test_sgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sklearn.utils.testing import assert_false, assert_true
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_raises_regexp
from sklearn.utils.testing import ignore_warnings

from sklearn import linear_model, datasets, metrics
from sklearn.base import clone
Expand Down Expand Up @@ -1019,7 +1020,7 @@ def test_partial_fit(self):
clf.partial_fit(X[:third], Y[:third])
assert_equal(clf.coef_.shape, (X.shape[1], ))
assert_equal(clf.intercept_.shape, (1,))
assert_equal(clf.decision_function([[0, 0]]).shape, (1, ))
assert_equal(ignore_warnings(clf.decision_function)([[0, 0]]).shape, (1, ))
id1 = id(clf.coef_.data)

clf.partial_fit(X[third:], Y[third:])
Expand Down
27 changes: 14 additions & 13 deletions sklearn/neighbors/tests/test_approximate.py
1E0A
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_neighbors_accuracy_with_n_candidates():

for i, n_candidates in enumerate(n_candidates_values):
lshf = LSHForest(n_candidates=n_candidates)
lshf.fit(X)
ignore_warnings(lshf.fit)(X)
for j in range(n_iter):
query = X[rng.randint(0, n_samples)].reshape(1, -1)

Expand Down Expand Up @@ -74,7 +74,7 @@ def test_neighbors_accuracy_with_n_estimators():

for i, t in enumerate(n_estimators):
lshf = LSHForest(n_candidates=500, n_estimators=t)
lshf.fit(X)
ignore_warnings(lshf.fit)(X)
for j in range(n_iter):
query = X[rng.randint(0, n_samples)].reshape(1, -1)
neighbors = lshf.kneighbors(query, n_neighbors=n_points,
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_kneighbors():
# Test unfitted estimator
assert_raises(ValueError, lshf.kneighbors, X[0])

lshf.fit(X)
ignore_warnings(lshf.fit)(X)

for i in range(n_iter):
n_neighbors = rng.randint(0, n_samples)
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_radius_neighbors():
# Test unfitted estimator
assert_raises(ValueError, lshf.radius_neighbors, X[0])

lshf.fit(X)
ignore_warnings(lshf.fit)(X)

for i in range(n_iter):
# Select a random point in the dataset as the query
Expand Down Expand Up @@ -218,6 +218,7 @@ def test_radius_neighbors():
sorted_dists_approx)))


@ignore_warnings
def test_radius_neighbors_boundary_handling():
X = [[0.999, 0.001], [0.5, 0.5], [0, 1.], [-1., 0.001]]
n_points = len(X)
Expand Down Expand Up @@ -286,7 +287,7 @@ def test_distances():
X = rng.rand(n_samples, n_features)

lshf = LSHForest()
lshf.fit(X)
ignore_warnings(lshf.fit)(X)

for i in range(n_iter):
n_neighbors = rng.randint(0, n_samples)
Expand All @@ -312,7 +313,7 @@ def test_fit():
X = rng.rand(n_samples, n_features)

lshf = LSHForest(n_estimators=n_estimators)
lshf.fit(X)
ignore_warnings(lshf.fit)(X)

# _input_array = X
assert_array_equal(X, lshf._fit_X)
Expand Down Expand Up @@ -343,16 +344,16 @@ def test_partial_fit():
lshf = LSHForest()

# Test unfitted estimator
lshf.partial_fit(X)
ignore_warnings(lshf.partial_fit)(X)
assert_array_equal(X, lshf._fit_X)

lshf.fit(X)
ignore_warnings(lshf.fit)(X)

# Insert wrong dimension
assert_raises(ValueError, lshf.partial_fit,
np.random.randn(n_samples_partial_fit, n_features - 1))

lshf.partial_fit(X_partial_fit)
ignore_warnings(lshf.partial_fit)(X_partial_fit)

# size of _input_array = samples + 1 after insertion
assert_equal(lshf._fit_X.shape[0],
Expand All @@ -379,7 +380,7 @@ def test_hash_functions():

lshf = LSHForest(n_estimators=n_estimators,
random_state=rng.randint(0, np.iinfo(np.int32).max))
lshf.fit(X)
ignore_warnings(lshf.fit)(X)

hash_functions = []
for i in range(n_estimators):
Expand All @@ -405,7 +406,7 @@ def test_candidates():

# For zero candidates
lshf = LSHForest(min_hash_match=32)
lshf.fit(X_train)
ignore_warnings(lshf.fit)(X_train)

message = ("Number of candidates is not sufficient to retrieve"
" %i neighbors with"
Expand All @@ -419,7 +420,7 @@ def test_candidates():

# For candidates less than n_neighbors
lshf = LSHForest(min_hash_match=31)
lshf.fit(X_train)
ignore_warnings(lshf.fit)(X_train)

message = ("Number of candidates is not sufficient to retrieve"
" %i neighbors with"
Expand All @@ -441,7 +442,7 @@ def test_graphs():
for n_samples in n_samples_sizes:
X = rng.rand(n_samples, n_features)
lshf = LSHForest(min_hash_match=0)
lshf.fit(X)
ignore_warnings(lshf.fit)(X)

kneighbors_graph = lshf.kneighbors_graph(X)
radius_neighbors_graph = lshf.radius_neighbors_graph(X)
Expand Down
15 changes: 11 additions & 4 deletions sklearn/svm/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sklearn.utils import ConvergenceWarning
from sklearn.utils.extmath import safe_sparse_dot
from sklearn.utils.testing import assert_warns, assert_raise_message
from sklearn.utils.testing import ignore_warnings

# test sample 1
X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]])
Expand Down Expand Up @@ -63,7 +64,7 @@ def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
else:
assert_array_almost_equal(dense_svm.predict_proba(X_test_dense),
sparse_svm.predict_proba(X_test), 4)
sparse_svm.predict_proba(X_test), 4)
msg = "cannot use sparse input in 'SVC' trained on dense data"
if sparse.isspmatrix(X_test):
assert_raise_message(ValueError, msg, dense_svm.predict, X_test)
Expand All @@ -81,8 +82,14 @@ def test_svc():
kernels = ["linear", "poly", "rbf", "sigmoid"]
for dataset in datasets:
for kernel in kernels:
clf = svm.SVC(kernel=kernel, probability=True, random_state=0)
sp_clf = svm.SVC(kernel=kernel, probability=True, random_state=0)
clf = svm.SVC(kernel=kernel,
probability=True,
random_state=0,
decision_function_shape='ovo')
sp_clf = svm.SVC(kernel=kernel,
probability=True,
random_state=0,
decision_function_shape='ovo')
check_svm_model_equal(clf, sp_clf, *dataset)


Expand Down Expand Up @@ -152,7 +159,7 @@ def test_sparse_decision_function():

dec = safe_sparse_dot(iris.data, clf.coef_.T) + clf.intercept_

assert_array_almost_equal(dec, clf.decision_function(iris.data))
assert_array_almost_equal(dec, ignore_warnings(clf.decision_function)(iris.data))

# binary:
clf.fit(X, Y)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/svm/tests/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ def test_svr_decision_function():
reg = svm.SVR(kernel='linear', C=0.1).fit(X, y)

dec = np.dot(X, reg.coef_.T) + reg.intercept_
assert_array_almost_equal(dec.ravel(), reg.decision_function(X).ravel())
assert_array_almost_equal(dec.ravel(), ignore_warnings(reg.decision_function)(X).ravel())

# rbf kernel
reg = svm.SVR(kernel='rbf', gamma=1).fit(X, y)

rbfs = rbf_kernel(X, reg.support_vectors_, gamma=reg.gamma)
dec = np.dot(rbfs, reg.dual_coef_.T) + reg.intercept_
assert_array_almost_equal(dec.ravel(), reg.decision_function(X).ravel())
assert_array_almost_equal(dec.ravel(), ignore_warnings(reg.decision_function)(X).ravel())


def test_weight():
Expand Down
2 changes: 2 additions & 0 deletions sklearn/tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
assert_array_equal,
assert_raises,
assert_warns_message)
from sklearn.utils.testing import ignore_warnings
from sklearn.datasets import make_classification, make_blobs
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
Expand All @@ -23,6 +24,7 @@
from sklearn.calibration import calibration_curve


@ignore_warnings
def test_calibration():
"""Test calibration objects with isotonic and sigmoid"""
n_samples = 100
Expand Down
7 changes: 4 additions & 3 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def test_non_meta_estimators():
if name.startswith("_"):
continue
for check in _yield_all_checks(name, Estimator):
yield check, name, Estimator
with ignore_warnings():
yield check, name, Estimator


def test_configure():
Expand Down Expand Up @@ -105,7 +106,7 @@ def test_class_weight_balanced_linear_classifiers():
(name, clazz)
for name, clazz in classifiers
if 'class_weight' in clazz().get_params().keys()
and issubclass(clazz, LinearClassifierMixin)]
and issubclass(clazz, LinearClassifierMixin)]

for name, Classifier in linear_classifiers:
if name == "LogisticRegressionCV":
Expand Down Expand Up @@ -167,7 +168,7 @@ def test_non_transformer_estimators_n_iter():

# Tested in test_transformer_n_iter below
elif (name in CROSS_DECOMPOSITION or
name in ['LinearSVC', 'LogisticRegression']):
name in ['LinearSVC', 'LogisticRegression']):
continue

else:
Expand Down
2 changes: 1 addition & 1 deletion sklearn/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_pipeline_methods_preprocessing_svm():
n_classes = len(np.unique(y))
scaler = StandardScaler()
pca = RandomizedPCA(n_components=2, whiten=True)
clf = SVC(probability=True, random_state=0)
clf = SVC(probability=True, random_state=0, decision_function_shape='ovr')

for preprocessing in [scaler, pca]:
pipe = Pipeline([('preprocess', preprocessing), ('svc', clf)])
Expand Down
0