本文整理汇总了Python中sklearn.utils.check_array方法的典型用法代码示例。如果您正苦于以下问题:Python utils.check_array方法的具体用法?Python utils.check_array怎么用?Python utils.check_array使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils
的用法示例。
在下文中一共展示了utils.check_array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_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: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def fit(self, X, y=None):
"""Fit data X by computing the binning thresholds.
Parameters
----------
X: array-like
The data to bin
Returns
-------
self : object
"""
X = check_array(X)
self.numerical_thresholds_ = _find_binning_thresholds(
X, self.max_bins, subsample=self.subsample,
random_state=self.random_state)
self.n_bins_per_feature_ = np.array(
[thresholds.shape[0] + 1
for thresholds in self.numerical_thresholds_],
dtype=np.uint32
)
return self
示例3: andb
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def andb(arrs):
"""
Sums arrays in `arrs`
Parameters
----------
arrs : :obj:`list`
List of boolean or integer arrays to be summed
Returns
-------
result : :obj:`numpy.ndarray`
Integer array of summed `arrs`
"""
# coerce to integer and ensure all arrays are the same shape
arrs = [check_array(arr, dtype=int, ensure_2d=False, allow_nd=True) for arr in arrs]
if not np.all([arr1.shape == arr2.shape for arr1 in arrs for arr2 in arrs]):
raise ValueError('All input arrays must have same shape.')
# sum across arrays
result = np.sum(arrs, axis=0)
return result
示例4: transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def transform(self, X):
"""Apply the approximate feature map to X.
Parameters
----------
X : {array-like}, shape (n_samples, n_features)
New data, where n_samples in the number of samples
and n_features is the number of features.
Returns
-------
X_new : array-like, shape (n_samples, n_components)
"""
X = check_array(X, dtype=np.float64)
X_padded = self._pad_with_zeros(X)
HGPHBX = self._apply_approximate_gaussian_matrix(
self._B, self._G, self._P, X_padded
)
VX = self._scale_transformed_data(self._S, HGPHBX)
return self._phi(VX)
示例5: transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def transform(self, X):
"""Transforms X to cluster-distance space.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_query, n_features), \
or (n_query, n_indexed) if metric == 'precomputed'
Data to transform.
Returns
-------
X_new : {array-like, sparse matrix}, shape=(n_query, n_clusters)
X transformed in the new space of distances to cluster centers.
"""
X = check_array(X, accept_sparse=["csr", "csc"])
if self.metric == "precomputed":
check_is_fitted(self, "medoid_indices_")
return X[:, self.medoid_indices_]
else:
check_is_fitted(self, "cluster_centers_")
Y = self.cluster_centers_
return pairwise_distances(X, Y=Y, metric=self.metric)
示例6: load_data
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def load_data(dtype=np.float32, order='F'):
"""Load the data, then cache and memmap the train/test split"""
######################################################################
# Load dataset
safe_print("Loading dataset...")
data = fetch_mldata('MNIST original')
X = check_array(data['data'], dtype=dtype, order=order)
y = data["target"]
# Normalize features
X = X / 255
# Create train-test split (as [Joachims, 2006])
safe_print("Creating train-test split...")
n_train = 60000
X_train = X[:n_train]
y_train = y[:n_train]
X_test = X[n_train:]
y_test = y[n_train:]
return X_train, X_test, y_train, y_test
示例7: decision_function
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def decision_function(self, X):
"""Predict raw anomaly score of X using the fitted detector.
The anomaly score of an input sample is computed based on different
detector algorithms. For consistency, outliers are assigned with
larger anomaly scores.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['discriminator'])
X = check_array(X)
pred_scores = self.discriminator.predict(X)
return pred_scores
示例8: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def fit(self, X, y=None):
"""Fit detector. y is ignored in unsupervised methods.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : Ignored
Not used, present for API consistency by convention.
Returns
-------
self : object
Fitted estimator.
"""
X = check_array(X)
self._set_n_classes(y)
self.decision_scores_ = self.decision_function(X)
self._process_decision_scores()
return self
示例9: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def fit(self, X, y=None):
"""Fit detector. y is ignored in unsupervised methods.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : Ignored
Not used, present for API consistency by convention.
Returns
-------
self : object
Fitted estimator.
"""
# validate inputs X and y (optional)
X = check_array(X)
self._set_n_classes(y)
self.decision_scores_ = self.decision_function(X)
self._process_decision_scores()
return self
示例10: test_ordering
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def test_ordering():
# Check that ordering is enforced correctly by validation utilities.
# We need to check each validation utility, because a 'copy' without
# 'order=K' will kill the ordering.
X = np.ones((10, 5))
for A in X, X.T:
for copy in (True, False):
B = check_array(A, order='C', copy=copy)
assert B.flags['C_CONTIGUOUS']
B = check_array(A, order='F', copy=copy)
assert B.flags['F_CONTIGUOUS']
if copy:
assert A is not B
X = sp.csr_matrix(X)
X.data = X.data[::-1]
assert not X.data.flags['C_CONTIGUOUS']
示例11: test_check_array_accept_sparse_type_exception
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def test_check_array_accept_sparse_type_exception():
X = [[1, 2], [3, 4]]
X_csr = sp.csr_matrix(X)
invalid_type = SVR()
msg = ("A sparse matrix was passed, but dense data is required. "
"Use X.toarray() to convert to a dense numpy array.")
assert_raise_message(TypeError, msg,
check_array, X_csr, accept_sparse=False)
msg = ("Parameter 'accept_sparse' should be a string, "
"boolean or list of strings. You provided 'accept_sparse={}'.")
assert_raise_message(ValueError, msg.format(invalid_type),
check_array, X_csr, accept_sparse=invalid_type)
msg = ("When providing 'accept_sparse' as a tuple or list, "
"it must contain at least one string value.")
assert_raise_message(ValueError, msg.format([]),
check_array, X_csr, accept_sparse=[])
assert_raise_message(ValueError, msg.format(()),
check_array, X_csr, accept_sparse=())
assert_raise_message(TypeError, "SVR",
check_array, X_csr, accept_sparse=[invalid_type])
示例12: test_check_input_false
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def test_check_input_false():
X, y, _, _ = build_dataset(n_samples=20, n_features=10)
X = check_array(X, order='F', dtype='float64')
y = check_array(X, order='F', dtype='float64')
clf = ElasticNet(selection='cyclic', tol=1e-8)
# Check that no error is raised if data is provided in the right format
clf.fit(X, y, check_input=False)
# With check_input=False, an exhaustive check is not made on y but its
# dtype is still cast in _preprocess_data to X's dtype. So the test should
# pass anyway
X = check_array(X, order='F', dtype='float32')
clf.fit(X, y, check_input=False)
# With no input checking, providing X in C order should result in false
# computation
X = check_array(X, order='C', dtype='float64')
assert_raises(ValueError, clf.fit, X, y, check_input=False)
示例13: fit_transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def fit_transform(self, X, y=None, sample_weight=None):
X = check_array(X, accept_sparse=['csc'], ensure_2d=False)
if sp.issparse(X):
# Pre-sort indices to avoid that each individual tree of the
# ensemble sorts the indices.
X.sort_indices()
X_, y_ = generate_discriminative_dataset(X)
super(RandomForestEmbedding, self).fit(X_, y_,
sample_weight=sample_weight)
self.one_hot_encoder_ = OneHotEncoder(sparse=True)
if self.sparse_output:
return self.one_hot_encoder_.fit_transform(self.apply(X))
return self.apply(X)
示例14: predict
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def predict(self, X):
check_is_fitted(self, "cluster_centers_")
# Check that the array is good and attempt to convert it to
# Numpy array if possible
X = check_array(X)
# Apply distance metric wrt. cluster centers (medoids)
D = self.distance_func(X, Y=self.cluster_centers_)
# Assign data points to clusters based on
# which cluster assignment yields
# the smallest distance
labels = np.argmin(D, axis=1)
return labels
示例15: transform
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_array [as 别名]
def transform(self, X, y=None):
# scikit-learn checks
X = check_array(X)
if X.shape[1] != len(self.maximums_):
raise ValueError("X has different shape than during fitting. "
"Expected %d, got %d." % (len(self.maximums_), X.shape[1]))
return np.vstack((
np.array([
np.cos(2 * np.pi * x / (maximum + 1))
for x, maximum in zip(X.T, self.maximums_)
]),
np.array([
np.sin(2 * np.pi * x / (maximum + 1))
for x, maximum in zip(X.T, self.maximums_)
])
)).T