本文整理汇总了Python中sklearn.utils.check_array函数的典型用法代码示例。如果您正苦于以下问题:Python check_array函数的具体用法?Python check_array怎么用?Python check_array使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_array函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: pinball_loss
def pinball_loss(y_true, y_pred, probs):
"""Compute the pinball loss.
Parameters
----------
pred : {array-like}, shape = [n_quantiles, n_samples] or [n_samples]
Predictions.
y : {array-like}, shape = [n_samples]
Targets.
Returns
-------
l : {array}, shape = [n_quantiles]
Average loss for each quantile level.
"""
probs = asarray(probs).reshape(-1)
check_consistent_length(y_true, y_pred.T)
y_true = check_array(y_true.reshape((-1, 1)),
ensure_2d=True)
y_pred = check_array(y_pred.T.reshape((y_true.shape[0], -1)),
ensure_2d=True)
residual = y_true - y_pred
loss = npsum([fmax(prob * res, (prob - 1) * res) for (res, prob) in
zip(residual.T, probs)], axis=1)
return loss / y_true.size
开发者ID:operalib,项目名称:operalib,代码行数:25,代码来源:quantile.py
示例2: query
def query(self, X, **query_kwargs):
"""
Finds the n_instances most informative point in the data provided by calling
the query_strategy function. Returns the queried instances and its indices.
Parameters
----------
X: numpy.ndarray of shape (n_samples, n_features)
The pool of samples from which the query strategy should choose
instances to request labels.
query_kwargs: keyword arguments
Keyword arguments for the query strategy function
Returns
-------
query_idx: numpy.ndarray of shape (n_instances, )
The indices of the instances from X_pool chosen to be labelled.
X[query_idx]: numpy.ndarray of shape (n_instances, n_features)
The instances from X_pool chosen to be labelled.
"""
check_array(X, ensure_2d=True)
query_idx, query_instances = self.query_strategy(self, X, **query_kwargs)
return query_idx, X[query_idx]
开发者ID:zhuwenxiao,项目名称:modAL,代码行数:26,代码来源:models.py
示例3: __call__
def __call__(self, y_true, y_pred, eps=1e-15, normalize=True, sample_weight=None):
if self.lb_ is None:
self.lb_ = LabelBinarizer()
T = self.lb_.fit_transform(y_true)
else:
T = self.lb_.transform(y_true)
if T.shape[1] == 1:
T = np.append(1 - T, T, axis=1)
Y = np.clip(y_pred, eps, 1 - eps)
if not isinstance(Y, np.ndarray):
raise ValueError("y_pred should be an array of floats.")
if Y.ndim == 1:
Y = Y[:, np.newaxis]
if Y.shape[1] == 1:
Y = np.append(1 - Y, Y, axis=1)
check_consistent_length(T, Y)
T = check_array(T)
Y = check_array(Y)
if T.shape[1] != Y.shape[1]:
raise ValueError("y_true and y_pred have different number of classes " "%d, %d" % (T.shape[1], Y.shape[1]))
Y /= Y.sum(axis=1)[:, np.newaxis]
loss = -(T * np.log(Y)).sum(axis=1)
return _weighted_sum(loss, sample_weight, normalize)
开发者ID:joshloyal,项目名称:Nettie,代码行数:30,代码来源:mxnet_backend.py
示例4: vote
def vote(self, X, **predict_kwargs):
"""
Predicts the labels for the supplied data for each learner in
the Committee.
Parameters
----------
X: numpy.ndarray of shape (n_samples, n_features)
The samples to cast votes.
predict_kwargs: keyword arguments
Keyword arguments to be passed for the learners .predict() method.
Returns
-------
vote: numpy.ndarray of shape (n_samples, n_learners)
The predicted class for each learner in the Committee
and each sample in X.
"""
check_array(X, ensure_2d=True)
prediction = np.zeros(shape=(X.shape[0], len(self._learner_list)))
for learner_idx, learner in enumerate(self._learner_list):
prediction[:, learner_idx] = learner.predict(X, **predict_kwargs)
return prediction
开发者ID:zhuwenxiao,项目名称:modAL,代码行数:26,代码来源:models.py
示例5: fit
def fit(self, X, y=None):
if self.encoding not in ['similarity',
'target',
'ordinal',
'onehot',
'onehot-dense',
'ngram-count',
'ngram-presence',
'ngram-tfidf']:
template = ("Encoding %s has not been implemented yet")
raise ValueError(template % self.handle_unknown)
if self.handle_unknown not in ['error', 'ignore']:
template = ("handle_unknown should be either 'error' or "
"'ignore', got %s")
raise ValueError(template % self.handle_unknown)
if self.encoding == 'ordinal' and self.handle_unknown == 'ignore':
raise ValueError("handle_unknown='ignore' is not supported for"
" encoding='ordinal'")
if self.categories != 'auto':
for cats in self.categories:
if not np.all(np.sort(cats) == np.array(cats)):
raise ValueError("Unsorted categories are not yet "
"supported")
X_temp = check_array(X, dtype=None)
if not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_):
X = check_array(X, dtype=np.object)
else:
X = X_temp
n_samples, n_features = X.shape
self._label_encoders_ = [LabelEncoder() for _ in range(n_features)]
for i in range(n_features):
le = self._label_encoders_[i]
Xi = X[:, i]
if self.categories == 'auto':
le.fit(Xi)
else:
if self.handle_unknown == 'error':
valid_mask = np.in1d(Xi, self.categories[i])
if not np.all(valid_mask):
diff = np.unique(Xi[~valid_mask])
msg = ("Found unknown categories {0} in column {1}"
" during fit".format(diff, i))
raise ValueError(msg)
le.classes_ = np.array(self.categories[i])
self.categories_ = [le.classes_ for le in self._label_encoders_]
if self.encoding == 'target':
self.Eyx_ = [{cat: np.mean(y[X[:, i] == cat])
for cat in self.categories_[i]}
for i in range(len(self.categories_))]
self.Ey_ = [np.mean(y)
for i in range(len(self.categories_))]
return self
开发者ID:dfayzur,项目名称:dirty-cat,代码行数:60,代码来源:categorical_encoding.py
示例6: fit
def fit(self, X_train, y_train, n_more_iter=0):
""" Fit model with specified loss.
Parameters
----------
X : scipy.sparse.csc_matrix, (n_samples, n_features)
y : float | ndarray, shape = (n_samples, )
n_more_iter : int
Number of iterations to continue from the current Coefficients.
"""
check_consistent_length(X_train, y_train)
y_train = check_array(y_train, ensure_2d=False, dtype=np.float64)
X_train = check_array(X_train, accept_sparse="csc", dtype=np.float64,
order="F")
self.n_iter = self.n_iter + n_more_iter
if n_more_iter > 0:
_check_warm_start(self, X_train)
self.warm_start = True
self.w0_, self.w_, self.V_ = ffm.ffm_als_fit(self, X_train, y_train)
if self.iter_count != 0:
self.iter_count = self.iter_count + n_more_iter
else:
self.iter_count = self.n_iter
# reset to default setting
self.warm_start = False
return self
开发者ID:bdaskalov,项目名称:fastFM,代码行数:35,代码来源:als.py
示例7: log_loss
def log_loss(y_true, y_pred, eps=1e-15, normalize=True, sample_weight=None):
lb = LabelBinarizer()
T = lb.fit_transform(y_true)
if T.shape[1] == 1:
T = np.append(1 - T, T, axis=1)
# Clipping
Y = np.clip(y_pred, eps, 1 - eps)
# This happens in cases when elements in y_pred have type "str".
if not isinstance(Y, np.ndarray):
raise ValueError("y_pred should be an array of floats.")
# If y_pred is of single dimension, assume y_true to be binary
# and then check.
if Y.ndim == 1:
Y = Y[:, np.newaxis]
if Y.shape[1] == 1:
Y = np.append(1 - Y, Y, axis=1)
# Check if dimensions are consistent.
check_consistent_length(T, Y)
T = check_array(T)
Y = check_array(Y)
if T.shape[1] != Y.shape[1]:
raise ValueError("y_true and y_pred have different number of classes "
"%d, %d" % (T.shape[1], Y.shape[1]))
# Renormalize
Y /= Y.sum(axis=1)[:, np.newaxis]
loss = -(T * np.log(Y)).sum(axis=1)
return loss
开发者ID:Zheng-JIA,项目名称:kernelsubsampling,代码行数:32,代码来源:log_loss.py
示例8: fit_transform
def fit_transform(self,X,y=None):
"""
Generates sets of hyper-spheres for anomaly scores
Parameters
----------
X : numpy array (nb_samples, nb_features)
data set
Returns
-------
self
"""
t_0 = time()
check_array(X)
self._sets_of_spheres = []
if self.verbose:
logger.info('generating sets of spheres...')
for j in range(self.ensemble_size):
X_s = np.random.permutation(X)[:self.sample_size,:]
spheres = self._generate_spheres(X_s)
self._sets_of_spheres.append(spheres)
t_f = time() - t_0
m,s = divmod(t_f, 60)
h,m = divmod(m, 60)
if self.verbose:
logger.info('Total run time: %i:%i:%i'
% (h,m,s))
return self
开发者ID:smsahu,项目名称:seldon-server,代码行数:34,代码来源:AnomalyDetection.py
示例9: csr_to_fm
def csr_to_fm(self, X_csr, return_oh=True, indices=None):
assert (X_csr.shape == (self.n_samples, self.n_features))
if indices is None:
y = check_array(X_csr.data, ensure_2d=False, copy=True)
else:
if isinstance(indices, tuple):
indices_samples, indices_features = indices
elif isinstance(indices, sp.csc_matrix):
indices_samples, indices_features = self.fm_to_indices(indices)
y = X_csr[indices_samples, indices_features].A[0].copy()
if not return_oh:
return y
else:
X = check_array(X_csr, accept_sparse='coo',
force_all_finite=False)
n_rows, n_cols = X_csr.shape
assert ((n_rows, n_cols) == (self.n_samples, self.n_features))
if indices is None:
encoder = OneHotEncoder(n_values=[self.n_samples,
self.n_features])
X_ix = np.column_stack([X.row, X.col])
else:
assert (np.sorted(indices_samples) == np.sorted(X.row))
assert (np.sorted(indices_features) == np.sorted(X.col))
X_ix = np.column_stack([indices_samples, indices_features])
X_oh = encoder.fit_transform(X_ix)
return X_oh, y
开发者ID:arthurmensch,项目名称:scikit-learn-sandbox,代码行数:28,代码来源:base.py
示例10: fit
def fit(self, X, y):
"""Fit OVK ridge regression model.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training data.
y : {array-like}, shape = [n_samples] or [n_samples, n_targets]
Target values. numpy.NaN for missing targets (semi-supervised
learning).
Returns
-------
self : returns an instance of self.
"""
X = check_array(X, force_all_finite=True, accept_sparse=False,
ensure_2d=True)
y = check_array(y, force_all_finite=False, accept_sparse=False,
ensure_2d=False)
if y.ndim == 1:
y = check_array(y, force_all_finite=True, accept_sparse=False,
ensure_2d=False)
self._validate_params()
self.linop_ = self._get_kernel_map(X, y)
Gram = self.linop_._Gram(X)
if self.lbda > 0:
self.dual_coefs_ = dlyap(-Gram / self.lbda, self.linop_.A,
y / self.lbda)
else:
# TODO: Check A is invertible!!
self.dual_coefs_ = solve(Gram, y)
return self
开发者ID:operalib,项目名称:operalib,代码行数:34,代码来源:ridge.py
示例11: _transform
def _transform(self, X, handle_unknown='error'):
X_temp = check_array(X, dtype=None)
if not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_):
X = check_array(X, dtype=np.object)
else:
X = X_temp
_, n_features = X.shape
X_int = np.zeros_like(X, dtype=np.int)
X_mask = np.ones_like(X, dtype=np.bool)
for i in range(n_features):
Xi = X[:, i]
valid_mask = np.in1d(Xi, self.categories_[i])
if not np.all(valid_mask):
if handle_unknown == 'error':
diff = np.unique(X[~valid_mask, i])
msg = ("Found unknown categories {0} in column {1}"
" during transform".format(diff, i))
raise ValueError(msg)
else:
# Set the problematic rows to an acceptable value and
# continue `The rows are marked `X_mask` and will be
# removed later.
X_mask[:, i] = valid_mask
Xi = Xi.copy()
Xi[~valid_mask] = self.categories_[i][0]
X_int[:, i] = self._label_encoders_[i].transform(Xi)
return X_int, X_mask
开发者ID:a-geng,项目名称:handson-ml,代码行数:32,代码来源:future_encoders.py
示例12: _add_training_data
def _add_training_data(self, X, y):
"""
Adds the new data and label to the known data, but does
not retrain the model.
Parameters
----------
X: numpy.ndarray of shape (n_samples, n_features)
The new samples for which the labels are supplied
by the expert.
y: numpy.ndarray of shape (n_samples, )
Labels corresponding to the new instances in X.
Note
----
If the classifier has been fitted, the features in X
have to agree with the training samples which the
classifier has seen.
"""
X, y = check_array(X), check_array(y, ensure_2d=False)
assert len(X) == len(y), 'the number of new data points and number of labels must match'
if type(self._X_training) != type(None):
try:
self._X_training = np.vstack((self._X_training, X))
self._y_training = np.concatenate((self._y_training, y))
except ValueError:
raise ValueError('the dimensions of the new training data and label must'
'agree with the training data and labels provided so far')
else:
self._X_training = X
self._y_training = y
开发者ID:zhuwenxiao,项目名称:modAL,代码行数:34,代码来源:models.py
示例13: predict_proba
def predict_proba(self,X):
"""Create predictions. Start a vw process. Convert data to vw format and send.
Returns class probability estimates for the given test data.
X : pandas dataframe or array-like
Test samples
Returns
-------
proba : array-like, shape = (n_samples, n_outputs)
Class probability estimates.
Caveats :
1. A seldon specific fork of wabbit_wappa is needed to allow vw to run in server mode without save_resume. Save_resume seems to cause issues with the scores returned. Maybe connected to https://github.com/JohnLangford/vowpal_wabb#it/issues/262
"""
self._start_vw_if_needed("test")
if isinstance(X,pd.DataFrame):
df = X
df_base = self._exclude_include_features(df)
df_base = df_base.fillna(0)
else:
check_array(X)
df_base = pd.DataFrame(X)
df_vw = df_base.apply(self._convert_row,axis=1)
predictions = None
for (index,val) in df_vw.iteritems():
prediction = self.vw.send_line(val,parse_result=True)
self._start_raw_predictions()
scores = self._get_full_scores()
if predictions is None:
predictions = np.array([scores])
else:
predictions = np.vstack([predictions,scores])
return predictions
开发者ID:rlugojr,项目名称:seldon-server,代码行数:34,代码来源:vw.py
示例14: fit
def fit(self, X, y):
check_array(X, y)
for x_i, y_i in izip(X, y):
self.partial_fit(x_i, y_i)
return self
开发者ID:jsouza,项目名称:pamtl,代码行数:7,代码来源:pa_regression.py
示例15: _transform_new
def _transform_new(self, X):
"""New implementation assuming categorical input"""
X_temp = check_array(X, dtype=None)
if not hasattr(X, 'dtype') and np.issubdtype(X_temp.dtype, np.str_):
X = check_array(X, dtype=np.object)
else:
X = X_temp
n_samples, n_features = X.shape
X_int, X_mask = self._transform(X, handle_unknown=self.handle_unknown)
mask = X_mask.ravel()
n_values = [cats.shape[0] for cats in self.categories_]
n_values = np.array([0] + n_values)
feature_indices = np.cumsum(n_values)
indices = (X_int + feature_indices[:-1]).ravel()[mask]
indptr = X_mask.sum(axis=1).cumsum()
indptr = np.insert(indptr, 0, 0)
data = np.ones(n_samples * n_features)[mask]
out = sparse.csr_matrix((data, indices, indptr),
shape=(n_samples, feature_indices[-1]),
dtype=self.dtype)
if not self.sparse:
return out.toarray()
else:
return out
开发者ID:a-geng,项目名称:handson-ml,代码行数:29,代码来源:future_encoders.py
示例16: test_check_array_force_all_finiteinvalid
def test_check_array_force_all_finiteinvalid(value, force_all_finite,
match_msg, retype):
X = retype(np.arange(4).reshape(2, 2).astype(np.float))
X[0, 0] = value
with pytest.raises(ValueError, match=match_msg):
check_array(X, force_all_finite=force_all_finite,
accept_sparse=True)
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:7,代码来源:test_validation.py
示例17: transform
def transform(self, X):
check_array(X, accept_sparse=['csr', 'csc'])
if issparse(X):
mult = spdiags(self.weights_, 0, self.length, self.length)
X *= mult
else:
X *= self.weights_
return X
开发者ID:AlexeySorokin,项目名称:pyparadigm,代码行数:8,代码来源:feature_selector.py
示例18: __init__
def __init__(self, X, y, n_classes, batch_size):
self.X = check_array(X, dtype=np.float32, ensure_2d=False,
allow_nd=True)
self.y = check_array(y, ensure_2d=False, dtype=None)
self.n_classes = n_classes
self.batch_size = batch_size
self._input_shape = [batch_size] + list(X.shape[1:])
self._output_shape = [batch_size, n_classes] if n_classes > 1 else [batch_size]
开发者ID:Erkhamion,项目名称:skflow,代码行数:8,代码来源:data_feeder.py
示例19: test_check_array_on_mock_dataframe
def test_check_array_on_mock_dataframe():
arr = np.array([[0.2, 0.7], [0.6, 0.5], [0.4, 0.1], [0.7, 0.2]])
mock_df = MockDataFrame(arr)
checked_arr = check_array(mock_df)
assert_equal(checked_arr.dtype,
arr.dtype)
checked_arr = check_array(mock_df, dtype=np.float32)
assert_equal(checked_arr.dtype, np.dtype(np.float32))
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:8,代码来源:test_validation.py
示例20: fit
def fit(self, X, y):
X = check_array(X)
y = check_array(y)
for x_i, y_i in izip(X, y):
self.partial_fit(x_i.reshape(-1, 1), y_i.reshape(1, -1))
return self
开发者ID:jsouza,项目名称:pamtl,代码行数:8,代码来源:partl_regression.py
注:本文中的sklearn.utils.check_array函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论