10000 FIX dtypes to conform to the stricter type cast rules of numpy 1.10 · scikit-learn/scikit-learn@0c0de34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c0de34

Browse files
committed
FIX dtypes to conform to the stricter type cast rules of numpy 1.10
1 parent 9564b94 commit 0c0de34

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

sklearn/cross_decomposition/pls_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import numpy as np
1515
from scipy import linalg
1616
from ..utils import arpack
17-
from ..utils.validation import check_is_fitted
17+
from ..utils.validation import check_is_fitted, FLOAT_DTYPES
1818

1919
__all__ = ['PLSCanonical', 'PLSRegression', 'PLSSVD']
2020

@@ -375,14 +375,14 @@ def transform(self, X, Y=None, copy=True):
375375
x_scores if Y is not given, (x_scores, y_scores) otherwise.
376376
"""
377377
check_is_fitted(self, 'x_mean_')
378-
X = check_array(X, copy=copy)
378+
X = check_array(X, copy=copy, dtype=FLOAT_DTYPES)
379379
# Normalize
380380
X -= self.x_mean_
381381
X /= self.x_std_
382382
# Apply rotation
383383
x_scores = np.dot(X, self.x_rotations_)
384384
if Y is not None:
385-
Y = check_array(Y, ensure_2d=False, copy=copy)
385+
Y = check_array(Y, ensure_2d=False, copy=copy, dtype=FLOAT_DTYPES)
386386
if Y.ndim == 1:
387387
Y = Y.reshape(-1, 1)
388388
Y -= self.y_mean_
@@ -410,7 +410,7 @@ def predict(self, X, copy=True):
410410
be an issue in high dimensional space.
411411
"""
412412
check_is_fitted(self, 'x_mean_')
413-
X = check_array(X, copy=copy)
413+
X = check_array(X, copy=copy, dtype=FLOAT_DTYPES)
414414
# Normalize
415415
X -= self.x_mean_
416416
X /= self.x_std_

sklearn/decomposition/fastica_.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
"""
23
Python implementation of the fast ICA algorithms.
34
@@ -18,6 +19,7 @@
1819
from ..utils import check_array, as_float_array, check_random_state
1920
from ..utils.extmath import fast_dot
2021
from ..utils.validation import check_is_fitted
22+
from ..utils.validation import FLOAT_DTYPES
2123

2224
__all__ = ['fastica', 'FastICA']
2325

@@ -261,7 +263,7 @@ def my_g(x):
261263
fun_args = {} if fun_args is None else fun_args
262264
# make interface compatible with other decompositions
263265
# a copy is required only for non whitened data
264-
X = check_array(X, copy=whiten).T
266+
X = check_array(X, copy=whiten, dtype=FLOAT_DTYPES).T
265267

266268
alpha = fun_args.get('alpha', 1.0)
267269
if not 1 <= alpha <= 2:
@@ -540,7 +542,7 @@ def transform(self, X, y=None, copy=True):
540542
"""
541543
check_is_fitted(self, 'mixing_')
542544

543-
X = check_array(X, copy=copy)
545+
X = check_array(X, copy=copy, dtype=FLOAT_DTYPES)
544546
if self.whiten:
545547
X -= self.mean_
546548

@@ -563,8 +565,7 @@ def inverse_transform(self, X, copy=True):
563565
"""
564566
check_is_fitted(self, 'mixing_')
565567

566-
if copy:
567-
X = X.copy()
568+
X = as_float_array(X, copy=copy)
568569
X = fast_dot(X, self.mixing_.T)
569570
if self.whiten:
570571
X += self.mean_

sklearn/ensemble/weight_boosting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from ..utils import check_array, check_X_y, check_random_state
4141
from ..metrics import accuracy_score, r2_score
4242
from sklearn.utils.validation import has_fit_parameter, check_is_fitted
43+
from sklearn.utils.validation import as_float_array
4344

4445
__all__ = [
4546
'AdaBoostClassifier',
@@ -1003,6 +1004,8 @@ def _boost(self, iboost, X, y, sample_weight):
10031004

10041005
generator = check_random_state(self.random_state)
10051006

1007+
sample_weight = check_array(sample_weight, dtype=np.float64)
1008+
10061009
# Weighted sampling of the training set with replacement
10071010
# For NumPy >= 1.7.0 use np.random.choice
10081011
cdf = sample_weight.cumsum()
@@ -1047,9 +1050,8 @@ def _boost(self, iboost, X, y, sample_weight):
10471050
estimator_weight = self.learning_rate * np.log(1. / beta)
10481051

10491052
if not iboost == self.n_estimators - 1:
1050-
sample_weight *= np.power(
1051-
beta,
1052-
(1. - error_vect) * self.learning_rate)
1053+
sample_weight *= as_float_array(np.power(
1054+
beta, (1. - error_vect) * self.learning_rate), copy=False)
10531055

10541056
return sample_weight, estimator_weight, estimator_error
10551057

sklearn/linear_model/least_angle.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,14 @@ def _lars_path_residues(X_train, y_train, X_test, y_test, Gram=None,
916916
residues : array, shape (n_alphas, n_samples)
917917
Residues of the prediction on the test data
918918
"""
919-
X_train = _check_copy_and_writeable(X_train, copy)
920-
y_train = _check_copy_and_writeable(y_train, copy)
921-
X_test = _check_copy_and_writeable(X_test, copy)
922-
y_test = _check_copy_and_writeable(y_test, copy)
919+
X_train = as_float_array(_check_copy_and_writeable(X_train, copy),
920+
copy=False)
921+
y_train = as_float_array(_check_copy_and_writeable(y_train, copy),
922+
copy=False)
923+
X_test = as_float_array(_check_copy_and_writeable(X_test, copy),
924+
copy=False)
925+
y_test = as_float_array(_check_copy_and_writeable(y_test, copy),
926+
copy=False)
923927

924928
if fit_intercept:
925929
X_mean = X_train.mean(axis=0)

sklearn/preprocessing/data.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def transform(self, X):
323323
"""
324324
check_is_fitted(self, 'scale_')
325325

326-
X = check_array(X, copy=self.copy, ensure_2d=False)
326+
X = check_array(X, copy=self.copy, ensure_2d=False, dtype=FLOAT_DTYPES)
327327
if X.ndim == 1:
328328
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
329329

@@ -341,7 +341,7 @@ def inverse_transform(self, X):
341341
"""
342342
check_is_fitted(self, 'scale_')
343343

344-
X = check_array(X, copy=self.copy, ensure_2d=False)
344+
X = check_array(X, copy=self.copy, ensure_2d=False, dtype=FLOAT_DTYPES)
345345
if X.ndim == 1:
346346
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
347347

@@ -1068,14 +1068,14 @@ class PolynomialFeatures(BaseEstimator, TransformerMixin):
10681068
[4, 5]])
10691069
>>> poly = PolynomialFeatures(2)
10701070
>>> poly.fit_transform(X)
1071-
array([[ 1, 0, 1, 0, 0, 1],
1072-
[ 1, 2, 3, 4, 6, 9],
1073-
[ 1, 4, 5, 16, 20, 25]])
1071+
array([[ 1., 0., 1., 0., 0., 1.],
1072+
[ 1., 2., 3., 4., 6., 9.],
1073+
[ 1., 4., 5., 16., 20., 25.]])
10741074
>>> poly = PolynomialFeatures(interaction_only=True)
10751075
>>> poly.fit_transform(X)
1076-
array([[ 1, 0, 1, 0],
1077-
[ 1, 2, 3, 6],
1078-
[ 1, 4, 5, 20]])
1076+
array([[ 1., 0., 1., 0.],
1077+
[ 1., 2., 3., 6.],
1078+
[ 1., 4., 5., 20.]])
10791079
10801080
Attributes
10811081
----------
@@ -1149,7 +1149,7 @@ def transform(self, X, y=None):
11491149
"""
11501150
check_is_fitted(self, ['n_input_features_', 'n_output_features_'])
11511151

1152-
X = check_array(X)
1152+
X = check_array(X, dtype=FLOAT_DTYPES)
11531153
n_samples, n_features = X.shape
11541154

11551155
if n_features != self.n_input_features_:
@@ -1333,7 +1333,8 @@ def binarize(X, threshold=0.0, copy=True):
13331333
using the ``Transformer`` API (e.g. as part of a preprocessing
13341334
:class:`sklearn.pipeline.Pipeline`)
13351335
"""
1336-
X = check_array(X, accept_sparse=['csr', 'csc'], copy=copy)
1336+
X = check_array(X, accept_sparse=['csr', 'csc'], copy=copy,
1337+
dtype=FLOAT_DTYPES)
13371338
if sparse.issparse(X):
13381339
if threshold < 0:
13391340
raise ValueError('Cannot binarize a sparse matrix with threshold '
@@ -1438,7 +1439,7 @@ def fit(self, K, y=None):
14381439
-------
14391440
self : returns an instance of self.
14401441
"""
1441-
K = check_array(K)
1442+
K = check_array(K, dtype=FLOAT_DTYPES)
14421443
n_samples = K.shape[0]
14431444
self.K_fit_rows_ = np.sum(K, axis=0) / n_samples
14441445
self.K_fit_all_ = self.K_fit_rows_.sum() / n_samples
@@ -1461,9 +1462,7 @@ def transform(self, K, y=None, copy=True):
14611462
"""
14621463
check_is_fitted(self, 'K_fit_all_')
14631464

1464-
K = check_array(K)
1465-
if copy:
1466-
K = K.copy()
1465+
K = check_array(K, copy=copy, dtype=FLOAT_DTYPES)
14671466

14681467
K_pred_cols = (np.sum(K, axis=1) /
14691468
self.K_fit_rows_.shape[0])[:, np.newaxis]
@@ -1503,7 +1502,7 @@ def add_dummy_feature(X, value=1.0):
15031502
array([[ 1., 0., 1.],
15041503
[ 1., 1., 0.]])
15051504
"""
1506-
X = check_array(X, accept_sparse=['csc', 'csr', 'coo'])
1505+
X = check_array(X, accept_sparse=['csc', 'csr', 'coo'], dtype=FLOAT_DTYPES)
15071506
n_samples, n_features = X.shape
15081507
shape = (n_samples, n_features + 1)
15091508
if sparse.issparse(X):
@@ -1558,7 +1557,7 @@ def _transform_selected(X, transform, selected="all", copy=True):
15581557
if selected == "all":
15591558
return transform(X)
15601559

1561-
X = check_array(X, accept_sparse='csc', copy=copy)
1560+
X = check_array(X, accept_sparse='csc', copy=copy, dtype=FLOAT_DTYPES)
15621561

15631562
if len(selected) == 0:
15641563
return X

0 commit comments

Comments
 (0)
0