8000 ENH fix test_estimators_overwrite_params to test regressors and trans… · deepatdotnet/scikit-learn@0adb851 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0adb851

Browse files
amuellerlarsmans
authored andcommitted
ENH fix test_estimators_overwrite_params to test regressors and transformers
Then fix all the regressors and transformers ... meh!
1 parent aefa925 commit 0adb851

File tree

8 files changed

+90
-67
lines changed

8 files changed

+90
-67
lines changed

sklearn/decomposition/dict_learning.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def fit(self, X, y=None):
955955
self: object
956956
Returns the object itself
957957
"""
958-
self.random_state = check_random_state(self.random_state)
958+
random_state = check_random_state(self.random_state)
959959
X = array2d(X)
960960
if self.n_components is None:
961961
n_components = X.shape[1]
@@ -969,7 +969,7 @@ def fit(self, X, y=None):
969969
code_init=self.code_init,
970970
dict_init=self.dict_init,
971971
verbose=self.verbose,
972-
random_state=self.random_state)
972+
random_state=random_state)
973973
self.components_ = U
974974
self.error_ = E
975975
return self
@@ -1120,7 +1120,7 @@ def fit(self, X, y=None):
11201120
self : object
11211121
Returns the instance itself.
11221122
"""
1123-
self.random_state = check_random_state(self.random_state)
1123+
random_state = check_random_state(self.random_state)
11241124
X = array2d(X)
11251125
if self.n_components is None:
11261126
n_components = X.shape[1]
@@ -1134,7 +1134,7 @@ def fit(self, X, y=None):
11341134
dict_init=self.dict_init,
11351135
batch_size=self.batch_size,
11361136
shuffle=self.shuffle, verbose=self.verbose,
1137-
random_state=self.random_state)
1137+
random_state=random_state)
11381138
self.components_ = U
11391139
return self
11401140

@@ -1152,7 +1152,8 @@ def partial_fit(self, X, y=None, iter_offset=0):
11521152
self : object
11531153
Returns the instance itself.
11541154
"""
1155-
self.random_state = check_random_state(self.random_state)
1155+
if not hasattr(self.random_state_):
1156+
self.random_state_ = check_random_state(self.random_state)
11561157
X = array2d(X)
11571158
if hasattr(self, 'components_'):
11581159
dict_init = self.components_
@@ -1165,6 +1166,6 @@ def partial_fit(self, X, y=None, iter_offset=0):
11651166
batch_size=len(X), shuffle=False,
11661167
verbose=self.verbose, return_code=False,
11671168
iter_offset=iter_offset,
1168-
random_state=self.random_state)
1169+
random_state=self.random_state_)
11691170
self.components_ = U
11701171
return self

sklearn/decomposition/nmf.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def _init(self, X):
376376
n_samples, n_features = X.shape
377377
init = self.init
378378
if init is None:
379-
if self.n_components < n_features:
379+
if self.n_components_ < n_features:
380380
init = 'nndsvd'
381381
else:
382382
init = 'random'
@@ -392,19 +392,19 @@ def _init(self, X):
392392
random_state = self.random_state
393393

394394
if init == 'nndsvd':
395-
W, H = _initialize_nmf(X, self.n_components)
395+
W, H = _initialize_nmf(X, self.n_components_)
396396
elif init == 'nndsvda':
397-
W, H = _initialize_nmf(X, self.n_components, variant='a')
397+
W, H = _initialize_nmf(X, self.n_components_, variant='a')
398398
elif init == 'nndsvdar':
399-
W, H = _initialize_nmf(X, self.n_components, variant='ar')
399+
W, H = _initialize_nmf(X, self.n_components_, variant='ar')
400400
elif init == "random":
401401
rng = check_random_state(random_state)
402-
W = rng.randn(n_samples, self.n_components)
402+
W = rng.randn(n_samples, self.n_components_)
403403
# we do not write np.abs(W, out=W) to stay compatible with
404404
# numpy 1.5 and earlier where the 'out' keyword is not
405405
# supported as a kwarg on ufuncs
406406
np.abs(W, W)
407-
H = rng.randn(self.n_components, n_features)
407+
H = rng.randn(self.n_components_, n_features)
408408
np.abs(H, H)
409409
else:
410410
raise ValueError(
@@ -422,14 +422,14 @@ def _update_W(self, X, H, W, tolW):
422422
W, gradW, iterW = _nls_subproblem(
423423
safe_vstack([X.T, np.zeros((1, n_samples))]),
424424
safe_vstack([H.T, np.sqrt(self.beta) * np.ones((1,
425-
self.n_components))]),
425+
self.n_components_))]),
426426
W.T, tolW, self.nls_max_iter)
427427
elif self.sparseness == 'components':
428428
W, gradW, iterW = _nls_subproblem(
429429
safe_vstack([X.T,
430-
np.zeros((self.n_components, n_samples))]),
430+
np.zeros((self.n_components_, n_samples))]),
431431
safe_vstack([H.T,
432-
np.sqrt(self.eta) * np.eye(self.n_components)]),
432+
np.sqrt(self.eta) * np.eye(self.n_components_)]),
433433
W.T, tolW, self.nls_max_iter)
434434

435435
return W, gradW, iterW
@@ -442,16 +442,16 @@ def _update_H(self, X, H, W, tolH):
442442
self.nls_max_iter)
443443
elif self.sparseness == 'data':
444444
H, gradH, iterH = _nls_subproblem(
445-
safe_vstack([X, np.zeros((self.n_components, n_features))]),
445+
safe_vstack([X, np.zeros((self.n_components_, n_features))]),
446446
safe_vstack([W,
447-
np.sqrt(self.eta) * np.eye(self.n_components)]),
447+
np.sqrt(self.eta) * np.eye(self.n_components_)]),
448448
H, tolH, self.nls_max_iter)
449449
elif self.sparseness == 'components':
450450
H, gradH, iterH = _nls_subproblem(
451451
safe_vstack([X, np.zeros((1, n_features))]),
452452
safe_vstack([W,
453453
np.sqrt(self.beta)
454-
* np.ones((1, self.n_components))]),
454+
* np.ones((1, self.n_components_))]),
455455
H, tolH, self.nls_max_iter)
456456

457457
return H, gradH, iterH
@@ -478,7 +478,9 @@ def fit_transform(self, X, y=None):
478478
n_samples, n_features = X.shape
479479

480480
if not self.n_components:
481-
self.n_components = n_features
481+
self.n_components_ = n_features
482+
else:
483+
self.n_components_ = self.n_components
482484

483485
W, H = self._init(X)
484486

@@ -556,7 +558,7 @@ def transform(self, X):
556558
Transformed data
557559
"""
558560
X = atleast2d_or_csr(X)
559-
W = np.zeros((X.shape[0], self.n_components))
561+
W = np.zeros((X.shape[0], self.n_components_))
560562
for j in xrange(0, X.shape[0]):
561563
W[j, :], _ = nnls(self.components_.T, X[j, :])
562564
return W

sklearn/decomposition/sparse_pca.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def fit(self, X, y=None):
103103
self : object
104104
Returns the instance itself.
105105
"""
106-
self.random_state = check_random_state(self.random_state)
106+
random_state = check_random_state(self.random_state)
107107
X = array2d(X)
108108
if self.n_components is None:
109109
n_components = X.shape[1]
@@ -115,7 +115,7 @@ def fit(self, X, y=None):
115115
tol=self.tol, max_iter=self.max_iter,
116116
method=self.method, n_jobs=self.n_jobs,
117117
verbose=self.verbose,
118-
random_state=self.random_state,
118+
random_state=random_state,
119119
code_init=code_init,
120120
dict_init=dict_init)
121121
self.components_ = Vt.T
@@ -254,7 +254,7 @@ def fit(self, X, y=None):
254254
self : object
255255
Returns the instance itself.
256256
"""
257-
self.random_state = check_random_state(self.random_state)
257+
random_state = check_random_state(self.random_state)
258258
X = array2d(X)
259259
if self.n_components is None:
260260
n_components = X.shape[1]
@@ -267,6 +267,6 @@ def fit(self, X, y=None):
267267
batch_size=self.batch_size,
268268
shuffle=self.shuffle,
269269
n_jobs=self.n_jobs, method=self.method,
270-
random_state=self.random_state)
270+
random_state=random_state)
271271
self.components_ = Vt.T
272272
return self

sklearn/kernel_approximation.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ def fit(self, X, y=None):
6767
"""
6868

6969
X = atleast2d_or_csr(X)
70-
self.random_state = check_random_state(self.random_state)
70+
random_state = check_random_state(self.random_state)
7171
n_features = X.shape[1]
7272

73-
self.random_weights_ = (np.sqrt(self.gamma)
74-
* self.random_state.normal(size=(n_features,
75-
self.n_components)))
73+
self.random_weights_ = (np.sqrt(self.gamma) * random_state.normal(
74+
size=(n_features, self.n_components)))
7675

77-
self.random_offset_ = self.random_state.uniform(0,
78-
2 * np.pi,
79-
size=self.n_components)
76+
self.random_offset_ = random_state.uniform(0, 2 * np.pi,
77+
size=self.n_components)
8078
return self
8179

8280
def transform(self, X, y=None):
@@ -153,16 +151,14 @@ def fit(self, X, y=None):
153151
"""
154152

155153
X = array2d(X)
156-
self.random_state = check_random_state(self.random_state)
154+
random_state = check_random_state(self.random_state)
157155
n_features = X.shape[1]
158-
uniform = self.random_state.uniform(size=(n_features,
159-
self.n_components))
156+
uniform = random_state.uniform(size=(n_features, self.n_components))
160157
# transform by inverse CDF of sech
161158
self.random_weights_ = (1. / np.pi
162159
* np.log(np.tan(np.pi / 2. * uniform)))
163-
self.random_offset_ = self.random_state.uniform(0,
164-
2 * np.pi,
165-
size=self.n_components)
160+
self.random_offset_ = random_state.uniform(0, 2 * np.pi,
161+
size=self.n_components)
166162
return self
167163

168164
def transform(self, X, y=None):
@@ -246,14 +242,16 @@ def fit(self, X, y=None):
246242
if self.sample_interval is None:
247243
# See reference, figure 2 c)
248244
if self.sample_steps == 1:
249-
self.sample_interval = 0.8
245+
self.sample_interval_ = 0.8
250246
elif self.sample_steps == 2:
251-
self.sample_interval = 0.5
247+
self.sample_interval_ = 0.5
252248
elif self.sample_steps == 3:
253-
self.sample_interval = 0.4
249+
self.sample_interval_ = 0.4
254250
else:
255251
raise ValueError("If sample_steps is not in [1, 2, 3],"
256252
" you need to provide sample_interval")
253+
else:
254+
self.sample_interval_ = self.interval
257255
return self
258256

259257
def transform(self, X, y=None):
@@ -289,16 +287,16 @@ def _transform_dense(self, X):
289287
X_nz = X[non_zero]
290288

291289
X_step = np.zeros_like(X)
292-
X_step[non_zero] = np.sqrt(X_nz * self.sample_interval)
290+
X_step[non_zero] = np.sqrt(X_nz * self.sample_interval_)
293291

294292
X_new = [X_step]
295293

296-
log_step_nz = self.sample_interval * np.log(X_nz)
297-
step_nz = 2 * X_nz * self.sample_interval
294+
log_step_nz = self.sample_interval_ * np.log(X_nz)
295+
step_nz = 2 * X_nz * self.sample_interval_
298296

299297
for j in xrange(1, self.sample_steps):
300298
factor_nz = np.sqrt(step_nz /
301-
np.cosh(np.pi * j * self.sample_interval))
299+
np.cosh(np.pi * j * self.sample_interval_))
302300

303301
X_step = np.zeros_like(X)
304302
X_step[non_zero] = factor_nz * np.cos(j * log_step_nz)
@@ -314,17 +312,17 @@ def _transform_sparse(self, X):
314312
indices = X.indices.copy()
315313
indptr = X.indptr.copy()
316314

317-
data_step = np.sqrt(X.data * self.sample_interval)
315+
data_step = np.sqrt(X.data * self.sample_interval_)
318316
X_step = sp.csr_matrix((data_step, indices, indptr),
319317
shape=X.shape, dtype=X.dtype, copy=False)
320318
X_new = [X_step]
321319

322-
log_step_nz = self.sample_interval * np.log(X.data)
323-
step_nz = 2 * X.data * self.sample_interval
320+
log_step_nz = self.sample_interval_ * np.log(X.data)
321+
step_nz = 2 * X.data * self.sample_interval_
324322

325323
for j in xrange(1, self.sample_steps):
326324
factor_nz = np.sqrt(step_nz /
327-
np.cosh(np.pi * j * self.sample_interval))
325+
np.cosh(np.pi * j * self.sample_interval_))
328326

329327
data_step = factor_nz * np.cos(j * log_step_nz)
330328
X_step = sp.csr_matrix((data_step, indices, indptr),

sklearn/linear_model/omp.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ def fit(self, X, y, Gram=None, Xy=None):
518518
if self.n_nonzero_coefs is None and self.tol is None:
519519
# default for n_nonzero_coefs is 0.1 * n_features
520520
# but at least one.
521-
self.n_nonzero_coefs = max(int(0.1 * n_features), 1)
521+
self.n_nonzero_coefs_ = max(int(0.1 * n_features), 1)
522+
else:
523+
self.n_nonzero_coefs_ = self.n_nonzero_coefs
522524
if (Gram is not None or Xy is not None) and (self.fit_intercept
523525
or self.normalize):
524526
warnings.warn('Mean subtraction (fit_intercept) and normalization '
@@ -546,14 +548,14 @@ def fit(self, X, y, Gram=None, Xy=None):
546548
Gram /= X_std[:, np.newaxis]
547549

548550
norms_sq = np.sum(y ** 2, axis=0) if self.tol is not None else None
549-
self.coef_ = orthogonal_mp_gram(Gram, Xy, self.n_nonzero_coefs,
551+
self.coef_ = orthogonal_mp_gram(Gram, Xy, self.n_nonzero_coefs_,
550552
self.tol, norms_sq,
551553
self.copy_Gram, True).T
552554
else:
553555
precompute_gram = self.precompute_gram
554556
if precompute_gram == 'auto':
555557
precompute_gram = X.shape[0] > X.shape[1]
556-
self.coef_ = orthogonal_mp(X, y, self.n_nonzero_coefs, self.tol,
558+
self.coef_ = orthogonal_mp(X, y, self.n_nonzero_coefs_, self.tol,
557559
precompute_gram=self.precompute_gram,
558560
copy_X=self.copy_X).T
559561

sklearn/manifold/locally_linear.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def _fit_transform(self, X):
609609
self.nbrs_ = NearestNeighbors(self.n_neighbors,
610610
algorithm=self.neighbors_algorithm)
611611

612-
self.random_state = check_random_state(self.random_state)
612+
random_state = check_random_state(self.random_state)
613613
X, = check_arrays(X, sparse_format='dense')
614614
self.nbrs_.fit(X)
615615
self.embedding_, self.reconstruction_error_ = \
@@ -618,7 +618,7 @@ def _fit_transform(self, X):
618618
eigen_solver=self.eigen_solver, tol=self.tol,
619619
max_iter=self.max_iter, method=self.method,
620620
hessian_tol=self.hessian_tol, modified_tol=self.modified_tol,
621-
random_state=self.random_state)
621+
random_state=random_state)
622622

623623
def fit(self, X, y=None):
624624
"""Compute the embedding vectors for data X

sklearn/manifold/spectral_embedding.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,19 +412,18 @@ def _get_affinity_matrix(self, X, Y=None):
412412
"rbf affinity")
413413
self.affinity = "rbf"
414414
else:
415-
if self.gamma is None:
416-
self.gamma = 1.0 / X.shape[1]
417-
if self.n_neighbors is None:
418-
self.n_neighbors = max(int(X.shape[0] / 10), 1)
419-
self.affinity_matrix_ = kneighbors_graph(X, self.n_neighbors)
415+
self.n_neighbors_ = (self.n_neighbors
416+
if self.n_neighbors is not None
417+
else max(int(X.shape[0] / 10), 1))
418+
self.affinity_matrix_ = kneighbors_graph(X, self.n_neighbors_)
420419
# currently only symmetric affinity_matrix supported
421420
self.affinity_matrix_ = 0.5 * (self.affinity_matrix_ +
422421
self.affinity_matrix_.T)
423422
return self.affinity_matrix_
424423
if self.affinity == 'rbf':
425-
if self.gamma is None:
426-
self.gamma = 1.0 / X.shape[1]
427-
self.affinity_matrix_ = rbf_kernel(X, gamma=self.gamma)
424+
self.gamma_ = (self.gamma
425+
if self.gamma is not None else 1.0 / X.shape[1])
426+
self.affinity_matrix_ = rbf_kernel(X, gamma=self.gamma_)
428427
return self.affinity_matrix_
429428
self.affinity_matrix_ = self.affinity(X)
430429
return self.affinity_matrix_
@@ -448,7 +447,7 @@ def fit(self, X, y=None):
448447
self : object
449448
Returns the instance itself.
450449
"""
451-
self.random_state = check_random_state(self.random_state)
450+
random_state = check_random_state(self.random_state)
452451
if isinstance(self.affinity, basestring):
453452
if self.affinity not in set(("nearest_neighbors", "rbf",
454453
"precomputed")):
@@ -463,7 +462,7 @@ def fit(self, X, y=None):
463462
self.embedding_ = spectral_embedding(affinity_matrix,
464463
n_components=self.n_components,
465464
eigen_solver=self.eigen_solver,
466-
random_state=self.random_state)
465+
random_state=random_state)
467466
return self
468467

469468
def fit_transform(self, X, y=None):

0 commit comments

Comments
 (0)
0