本文整理汇总了Python中sklearn.utils.as_float_array方法的典型用法代码示例。如果您正苦于以下问题:Python utils.as_float_array方法的具体用法?Python utils.as_float_array怎么用?Python utils.as_float_array使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils
的用法示例。
在下文中一共展示了utils.as_float_array方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def fit(self, X, y=None):
"""Compute the mean, whitening and dewhitening matrices.
Parameters
----------
X : array-like with shape [n_samples, n_features]
The data used to compute the mean, whitening and dewhitening
matrices.
"""
X = check_array(X, accept_sparse=None, copy=self.copy,
ensure_2d=True)
X = as_float_array(X, copy=self.copy)
self.mean_ = X.mean(axis=0)
X_ = X - self.mean_
cov = np.dot(X_.T, X_) / (X_.shape[0]-1)
U, S, _ = linalg.svd(cov)
s = np.sqrt(S.clip(self.regularization))
s_inv = np.diag(1./s)
s = np.diag(s)
self.whiten_ = np.dot(np.dot(U, s_inv), U.T)
self.dewhiten_ = np.dot(np.dot(U, s), U.T)
return self
示例2: transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def transform(self, X, y=None, copy=None):
"""Perform ZCA whitening
Parameters
----------
X : array-like with shape [n_samples, n_features]
The data to whiten along the features axis.
"""
check_is_fitted(self, 'mean_')
X = as_float_array(X, copy=self.copy)
return np.dot(X - self.mean_, self.whiten_.T)
示例3: inverse_transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def inverse_transform(self, X, copy=None):
"""Undo the ZCA transform and rotate back to the original
representation
Parameters
----------
X : array-like with shape [n_samples, n_features]
The data to rotate back.
"""
check_is_fitted(self, 'mean_')
X = as_float_array(X, copy=self.copy)
return np.dot(X, self.dewhiten_) + self.mean_
示例4: _clean_nans
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def _clean_nans(scores):
scores = as_float_array(scores, copy=True)
scores[np.isnan(scores)] = np.finfo(scores.dtype).min
return scores
示例5: test_as_float_array_nan
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def test_as_float_array_nan(X):
X[5, 0] = np.nan
X[6, 1] = np.nan
X_converted = as_float_array(X, force_all_finite='allow-nan')
assert_allclose_dense_sparse(X_converted, X)
示例6: test_np_matrix
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def test_np_matrix():
# Confirm that input validation code does not return np.matrix
X = np.arange(12).reshape(3, 4)
assert not isinstance(as_float_array(X), np.matrix)
assert not isinstance(as_float_array(np.matrix(X)), np.matrix)
assert not isinstance(as_float_array(sp.csc_matrix(X)), np.matrix)
示例7: test_memmap
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def test_memmap():
# Confirm that input validation code doesn't copy memory mapped arrays
asflt = lambda x: as_float_array(x, copy=False)
with NamedTemporaryFile(prefix='sklearn-test') as tmp:
M = np.memmap(tmp, shape=(10, 10), dtype=np.float32)
M[:] = 0
for f in (check_array, np.asarray, asflt):
X = f(M)
X[:] = 1
assert_array_equal(X.ravel(), M.ravel())
X[:] = 0
示例8: transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def transform(self, X):
if isinstance(X, pd.Series):
return X.to_frame()
X = as_float_array(X)
X = check_array(X)
return pd.DataFrame(X, index=self.index, columns=self.columns, dtype=self.dtype)
示例9: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def fit(self, X, y):
"""
Fit the model using X, y as training data.
Parameters
----------
X : {array-like, sparse matrix} of shape [n_samples, n_features]
Training vectors, where n_samples is the number of samples
and n_features is the number of features.
y : array-like of shape [n_samples, n_outputs]
Target values (class labels in classification, real numbers in
regression)
Returns
-------
self : object
Returns an instance of self.
"""
# fit random hidden layer and compute the hidden layer activations
self.hidden_activations_ = self.hidden_layer.fit_transform(X)
# solve the regression from hidden activations to outputs
self._fit_regression(as_float_array(y, copy=True))
return self
示例10: test_np_matrix
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def test_np_matrix():
# Confirm that input validation code does not return np.matrix
X = np.arange(12).reshape(3, 4)
assert_false(isinstance(as_float_array(X), np.matrix))
assert_false(isinstance(as_float_array(np.matrix(X)), np.matrix))
assert_false(isinstance(as_float_array(sp.csc_matrix(X)), np.matrix))
示例11: test_as_float_array
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def test_as_float_array():
# Test function for as_float_array
X = np.ones((3, 10), dtype=np.int32)
X = X + np.arange(10, dtype=np.int32)
X2 = as_float_array(X, copy=False)
assert_equal(X2.dtype, np.float32)
# Another test
X = X.astype(np.int64)
X2 = as_float_array(X, copy=True)
# Checking that the array wasn't overwritten
assert as_float_array(X, False) is not X
assert_equal(X2.dtype, np.float64)
# Test int dtypes <= 32bit
tested_dtypes = [np.bool,
np.int8, np.int16, np.int32,
np.uint8, np.uint16, np.uint32]
for dtype in tested_dtypes:
X = X.astype(dtype)
X2 = as_float_array(X)
assert_equal(X2.dtype, np.float32)
# Test object dtype
X = X.astype(object)
X2 = as_float_array(X, copy=True)
assert_equal(X2.dtype, np.float64)
# Here, X is of the right type, it shouldn't be modified
X = np.ones((3, 2), dtype=np.float32)
assert as_float_array(X, copy=False) is X
# Test that if X is fortran ordered it stays
X = np.asfortranarray(X)
assert np.isfortran(as_float_array(X, copy=True))
# Test the copy parameter with some matrices
matrices = [
np.matrix(np.arange(5)),
sp.csc_matrix(np.arange(5)).toarray(),
sparse_random_matrix(10, 10, density=0.10).toarray()
]
for M in matrices:
N = as_float_array(M, copy=True)
N[0, 0] = np.nan
assert not np.isnan(M).any()
示例12: test_as_float_array
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import as_float_array [as 别名]
def test_as_float_array():
# Test function for as_float_array
X = np.ones((3, 10), dtype=np.int32)
X = X + np.arange(10, dtype=np.int32)
X2 = as_float_array(X, copy=False)
assert_equal(X2.dtype, np.float32)
# Another test
X = X.astype(np.int64)
X2 = as_float_array(X, copy=True)
# Checking that the array wasn't overwritten
assert_true(as_float_array(X, False) is not X)
assert_equal(X2.dtype, np.float64)
# Test int dtypes <= 32bit
tested_dtypes = [np.bool,
np.int8, np.int16, np.int32,
np.uint8, np.uint16, np.uint32]
for dtype in tested_dtypes:
X = X.astype(dtype)
X2 = as_float_array(X)
assert_equal(X2.dtype, np.float32)
# Test object dtype
X = X.astype(object)
X2 = as_float_array(X, copy=True)
assert_equal(X2.dtype, np.float64)
# Here, X is of the right type, it shouldn't be modified
X = np.ones((3, 2), dtype=np.float32)
assert_true(as_float_array(X, copy=False) is X)
# Test that if X is fortran ordered it stays
X = np.asfortranarray(X)
assert_true(np.isfortran(as_float_array(X, copy=True)))
# Test the copy parameter with some matrices
matrices = [
np.matrix(np.arange(5)),
sp.csc_matrix(np.arange(5)).toarray(),
sparse_random_matrix(10, 10, density=0.10).toarray()
]
for M in matrices:
N = as_float_array(M, copy=True)
N[0, 0] = np.nan
assert_false(np.isnan(M).any())