Skip to content
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
2 changes: 1 addition & 1 deletion sklearn/cluster/affinity_propagation_.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def fit(self, X, y=None):
affinity_propagation(
self.affinity_matrix_, self.preference, max_iter=self.max_iter,
convergence_iter=self.convergence_iter, damping=self.damping,
copy=self.copy, verbose=self.verbose, return_n_iter=True)
copy=False, verbose=self.verbose, return_n_iter=True)

if self.affinity != "precomputed":
self.cluster_centers_ = X[self.cluster_centers_indices_].copy()
Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/birch.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def fit(self, X, y=None):
return self._fit(X)

def _fit(self, X):
X = check_array(X, accept_sparse='csr', copy=self.copy)
X = check_array(X, accept_sparse='csr', copy=False)
threshold = self.threshold
branching_factor = self.branching_factor

Expand Down
12 changes: 8 additions & 4 deletions sklearn/cross_decomposition/pls_.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ def fit(self, X, Y):

# copy since this will contains the residuals (deflated) matrices
check_consistent_length(X, Y)
X = check_array(X, dtype=np.float64, copy=self.copy)
Y = check_array(Y, dtype=np.float64, copy=self.copy, ensure_2d=False)
Xcopy = not X.flags.writable or self.copy
Ycopy = not Y.flags.writable or self.copy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should maybe factored into check_array:
Parameters copy=False|True|'on_readonly'

X = check_array(X, dtype=np.float64, copy=Xcopy)
Y = check_array(Y, dtype=np.float64, copy=Ycopy, ensure_2d=False)
if Y.ndim == 1:
Y = Y.reshape(-1, 1)

Expand Down Expand Up @@ -724,8 +726,10 @@ def __init__(self, n_components=2, scale=True, copy=True):
def fit(self, X, Y):
# copy since this will contains the centered data
check_consistent_length(X, Y)
X = check_array(X, dtype=np.float64, copy=self.copy)
Y = check_array(Y, dtype=np.float64, copy=self.copy, ensure_2d=False)
Xcopy = not X.flags.writable or self.copy
Ycopy = not Y.flags.writable or self.copy
X = check_array(X, dtype=np.float64, copy=Xcopy)
Y = check_array(Y, dtype=np.float64, copy=Ycopy, ensure_2d=False)
if Y.ndim == 1:
Y = Y.reshape(-1, 1)

Expand Down
10 changes: 6 additions & 4 deletions sklearn/decomposition/factor_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def fit(self, X, y=None):
-------
self
"""
X = check_array(X, copy=self.copy, dtype=np.float64)
copy = not X.flags.writable or self.copy
X = check_array(X, copy=copy, dtype=np.float64)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem 'on_readonly' in check_array ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'if_readonly' ?


n_samples, n_features = X.shape
n_components = self.n_components
Expand Down Expand Up @@ -247,14 +248,15 @@ def transform(self, X):
"""
check_is_fitted(self, 'components_')

X = check_array(X)
copy = copy if copy is not None else self.copy
X = check_array(X, copy=copy)
Ih = np.eye(len(self.components_))

X_transformed = X - self.mean_
X -= self.mean_

Wpsi = self.components_ / self.noise_variance_
cov_z = linalg.inv(Ih + np.dot(Wpsi, self.components_.T))
tmp = fast_dot(X_transformed, Wpsi.T)
tmp = fast_dot(X, Wpsi.T)
X_transformed = fast_dot(tmp, cov_z)

return X_transformed
Expand Down
4 changes: 3 additions & 1 deletion sklearn/decomposition/incremental_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def fit(self, X, y=None):
self.explained_variance_ratio_ = None
self.noise_variance_ = None

X = check_array(X, copy=self.copy, dtype=[np.float64, np.float32])
# because fit will perform inplace modifications
copy = not X.flags.writable or self.copy
X = check_array(X, copy=copy, dtype=[np.float64, np.float32])
n_samples, n_features = X.shape

if self.batch_size is None:
Expand Down
44 changes: 24 additions & 20 deletions sklearn/preprocessing/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def partial_fit(self, X, y=None):
raise TypeError("MinMaxScaler does no support sparse input. "
"You may consider to use MaxAbsScaler instead.")

X = check_array(X, copy=self.copy, ensure_2d=False, warn_on_dtype=True,
X = check_array(X, copy=False, ensure_2d=False, warn_on_dtype=True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy=False is default, should it be explicit ?

estimator=self, dtype=FLOAT_DTYPES)

if X.ndim == 1:
Expand All @@ -332,7 +332,7 @@ def partial_fit(self, X, y=None):
self.data_range_ = data_range
return self

def transform(self, X):
def transform(self, X, copy=None):
"""Scaling features of X according to feature_range.

Parameters
Expand All @@ -341,16 +341,16 @@ def transform(self, X):
Input data that will be transformed.
"""
check_is_fitted(self, 'scale_')

X = check_array(X, copy=self.copy, ensure_2d=False, dtype=FLOAT_DTYPES)
copy = copy if copy is not None else self.copy
X = check_array(X, copy=copy, ensure_2d=False, dtype=FLOAT_DTYPES)
if X.ndim == 1:
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)

X *= self.scale_
X += self.min_
return X

def inverse_transform(self, X):
def inverse_transform(self, X, copy=None):
"""Undo the scaling of X according to feature_range.

Parameters
Expand All @@ -359,8 +359,8 @@ def inverse_transform(self, X):
Input data that will be transformed. It cannot be sparse.
"""
check_is_fitted(self, 'scale_')

X = check_array(X, copy=self.copy, ensure_2d=False, dtype=FLOAT_DTYPES)
copy = copy if copy is not None else self.copy
X = check_array(X, copy=copy, ensure_2d=False, dtype=FLOAT_DTYPES)
if X.ndim == 1:
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)

Expand Down Expand Up @@ -557,7 +557,7 @@ def partial_fit(self, X, y=None):

y: Passthrough for ``Pipeline`` compatibility.
"""
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
X = check_array(X, accept_sparse=('csr', 'csc'), copy=False,
ensure_2d=False, warn_on_dtype=True,
estimator=self, dtype=FLOAT_DTYPES)

Expand Down Expand Up @@ -748,7 +748,7 @@ def partial_fit(self, X, y=None):

y: Passthrough for ``Pipeline`` compatibility.
"""
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
X = check_array(X, accept_sparse=('csr', 'csc'), copy=False,
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)

if X.ndim == 1:
Expand All @@ -772,7 +772,7 @@ def partial_fit(self, X, y=None):
self.scale_ = _handle_zeros_in_scale(max_abs)
return self

def transform(self, X, y=None):
def transform(self, X, y=None, copy=None):
"""Scale the data

Parameters
Expand All @@ -781,7 +781,8 @@ def transform(self, X, y=None):
The data that should be scaled.
"""
check_is_fitted(self, 'scale_')
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
copy = copy if copy is not None else self.copy
X = check_array(X, accept_sparse=('csr', 'csc'), copy=copy,
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)

if X.ndim == 1:
Expand All @@ -796,7 +797,7 @@ def transform(self, X, y=None):
X /= self.scale_
return X

def inverse_transform(self, X):
def inverse_transform(self, X, copy=None):
"""Scale back the data to the original representation

Parameters
Expand All @@ -805,6 +806,7 @@ def inverse_transform(self, X):
The data that should be transformed back.
"""
check_is_fitted(self, 'scale_')
copy = copy if copy is not None else self.copy
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)
if X.ndim == 1:
Expand Down Expand Up @@ -934,9 +936,9 @@ def __init__(self, with_centering=True, with_scaling=True, copy=True):
self.with_scaling = with_scaling
self.copy = copy

def _check_array(self, X, copy):
def _check_array(self, X, copy=False):
"""Makes sure centering is not enabled for sparse matrices."""
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
X = check_array(X, accept_sparse=('csr', 'csc'), copy=copy,
ensure_2d=False, estimator=self, dtype=FLOAT_DTYPES)

if X.ndim == 1:
Expand All @@ -960,7 +962,7 @@ def fit(self, X, y=None):
"""
if sparse.issparse(X):
raise TypeError("RobustScaler cannot be fitted on sparse inputs")
X = self._check_array(X, self.copy)
X = self._check_array(X, copy=False)
if X.ndim == 1:
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)
if self.with_centering:
Expand All @@ -972,7 +974,7 @@ def fit(self, X, y=None):
self.scale_ = _handle_zeros_in_scale(self.scale_, copy=False)
return self

def transform(self, X, y=None):
def transform(self, X, y=None, copy=None):
"""Center and scale the data

Parameters
Expand All @@ -984,7 +986,8 @@ def transform(self, X, y=None):
check_is_fitted(self, 'center_')
if self.with_scaling:
check_is_fitted(self, 'scale_')
X = self._check_array(X, self.copy)
copy = copy if copy is not None else self.copy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.copy if copy is None else copy

X = self._check_array(X, copy)
if X.ndim == 1:
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)

Expand All @@ -1001,7 +1004,7 @@ def transform(self, X, y=None):
X /= self.scale_
return X

def inverse_transform(self, X):
def inverse_transform(self, X, copy=None):
"""Scale back the data to the original representation

Parameters
Expand All @@ -1013,7 +1016,8 @@ def inverse_transform(self, X):
check_is_fitted(self, 'center_')
if self.with_scaling:
check_is_fitted(self, 'scale_')
X = self._check_array(X, self.copy)
copy = copy if copy is not None else self.copy
X = self._check_array(X, copy)
if X.ndim == 1:
warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)

Expand Down Expand Up @@ -1449,7 +1453,7 @@ def fit(self, X, y=None):
This method is just there to implement the usual API and hence
work in pipelines.
"""
check_array(X, accept_sparse='csr')
check_array(X, accept_sparse='csr', copy=False)
return self

def transform(self, X, y=None, copy=None):
Expand Down
5 changes: 3 additions & 2 deletions sklearn/preprocessing/imputation.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _dense_fit(self, X, strategy, missing_values, axis):

return most_frequent

def transform(self, X):
def transform(self, X, copy=None):
"""Impute all missing values in X.

Parameters
Expand All @@ -311,7 +311,8 @@ def transform(self, X):
check_is_fitted(self, 'statistics_')

# Copy just once
X = as_float_array(X, copy=self.copy, force_all_finite=False)
copy = copy if copy is not None else self.copy
X = as_float_array(X, copy=copy, force_all_finite=False)

# Since two different arrays can be provided in fit(X) and
# transform(X), the imputation data need to be recomputed
Expand Down
3 changes: 3 additions & 0 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from sklearn.linear_model.base import LinearClassifierMixin
from sklearn.utils.estimator_checks import (
_yield_all_checks,
_clear_temp_memory,
CROSS_DECOMPOSITION,
check_parameters_default_constructible,
check_class_weight_balanced_linear_classifier,
Expand Down Expand Up @@ -73,6 +74,8 @@ def test_non_meta_estimators():
yield check, name, Estimator
else:
yield check, name, Estimator
_clear_temp_memory(warn=False)


def test_configure():
# Smoke test the 'configure' step of setup, this tests all the
Expand Down
Loading