本文整理汇总了Python中sklearn.utils.check_X_y方法的典型用法代码示例。如果您正苦于以下问题:Python utils.check_X_y方法的具体用法?Python utils.check_X_y怎么用?Python utils.check_X_y使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils
的用法示例。
在下文中一共展示了utils.check_X_y方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y=None, **fit_params):
# scikit-learn checks
X, y = utils.check_X_y(X, y, accept_sparse='csr', order='C')
n_terms = min(self.n_terms, X.shape[1])
# Get a list of unique labels from y
labels = np.unique(y)
# Determine the n top terms per class
self.top_terms_per_class_ = {
c: set(np.argpartition(np.sum(X[y == c], axis=0), -n_terms)[-n_terms:])
for c in labels
}
# Return the classifier
return self
示例2: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y):
"""Fit classifier.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
"""
# Validate inputs X and y
X, y = check_X_y(X, y)
X = check_array(X)
self._set_n_classes(y)
if self.pre_fitted:
print("Training skipped")
return
else:
for clf in self.base_estimators:
clf.fit(X, y)
clf.fitted_ = True
return
示例3: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X: np.array, y: np.array) -> "RandomRegressor":
"""
Fit the model using X, y as training data.
:param X: array-like, shape=(n_columns, n_samples,) training data.
:param y: array-like, shape=(n_samples,) training data.
:return: Returns an instance of self.
"""
if self.strategy not in self.allowed_strategies:
raise ValueError(
f"strategy {self.strategy} is not in {self.allowed_strategies}"
)
X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES)
self.dim_ = X.shape[1]
self.min_ = np.min(y)
self.max_ = np.max(y)
self.mu_ = np.mean(y)
self.sigma_ = np.std(y)
return self
示例4: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y):
"""
Fit the model using X, y as training data.
:param X: array-like, shape=(n_columns, n_samples, ) training data.
:param y: array-like, shape=(n_samples, ) training data.
:return: Returns an instance of self.
"""
X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES)
if self.span is not None:
if not 0 <= self.span <= 1:
raise ValueError(f"Param `span` must be 0 <= span <= 1, got: {self.span}")
if self.sigma < 0:
raise ValueError(f"Param `sigma` must be >= 0, got: {self.sigma}")
self.X_ = X
self.y_ = y
return self
示例5: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y, sample_weight=None):
"""Fit a separate classifier for each output variable."""
for _, clf in self.classifiers:
if not hasattr(clf, 'fit'):
raise ValueError('Every base classifier should implement a fit method.')
X, y = check_X_y(X, y, multi_output=True, accept_sparse=True)
if is_classifier(self):
check_classification_targets(y)
if y.ndim == 1:
raise ValueError('Output y must have at least two dimensions for multi-output classification but has only one.')
if sample_weight is not None and any([not has_fit_parameter(clf, 'sample_weight') for _, clf in self.classifiers]):
raise ValueError('One of base classifiers does not support sample weights.')
self.classifiers_ = Parallel(n_jobs=self.n_jobs)(delayed(_fit_estimator)(clf, X, y[:, i], sample_weight)
for i, (_, clf) in zip(range(y.shape[1]), self.classifiers))
return self
示例6: _check_params
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def _check_params(self, X, y):
"""
Check hyperparameters as well as X and y before proceeding with fit.
"""
# check X and y are consistent len, X is Array and y is column
X, y = check_X_y(X, y)
if self.perc <= 0 or self.perc > 100:
raise ValueError('The percentile should be between 0 and 100.')
if self.alpha <= 0 or self.alpha > 1:
raise ValueError('Alpha should be between 0 and 1.')
示例7: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y, sample_weight=None):
"""Fit the model according to the given training data.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
The training input samples.
y : array-like, shape = [n_samples]
The target values.
sample_weight : array-like, shape (n_samples,) optional
Array of weights that are assigned to individual samples.
If not provided, then each sample is given unit weight.
"""
if not isinstance(self.weakness, float) or not (0.0 < self.weakness <= 1.0):
raise ValueError('weakness should be a float in (0, 1], got %s' % self.weakness)
X, y = check_X_y(X, y, accept_sparse='csr', dtype=[np.float64, np.float32],
order="C")
n_features = X.shape[1]
weakness = 1. - self.weakness
random_state = check_random_state(self.random_state)
weights = weakness * random_state.randint(0, 1 + 1, size=(n_features,))
X_rescaled = _rescale_data(X, weights)
return super(RandomizedLogisticRegression, self).fit(X_rescaled, y, sample_weight)
示例8: _check_params
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def _check_params(self, X, y):
# checking input data and scaling it if y is continuous
X, y = check_X_y(X, y)
if not self.categorical:
ss = StandardScaler()
X = ss.fit_transform(X)
y = ss.fit_transform(y.reshape(-1, 1))
# sanity checks
methods = ['JMI', 'JMIM', 'MRMR']
if self.method not in methods:
raise ValueError('Please choose one of the following methods:\n' +
'\n'.join(methods))
if not isinstance(self.k, int):
raise ValueError("k must be an integer.")
if self.k < 1:
raise ValueError('k must be larger than 0.')
if self.categorical and np.any(self.k > np.bincount(y)):
raise ValueError('k must be smaller than your smallest class.')
if not isinstance(self.categorical, bool):
raise ValueError('Categorical must be Boolean.')
if self.categorical and np.unique(y).shape[0] > 5:
print ('Are you sure y is categorical? It has more than 5 levels.')
if not self.categorical and self._isinteger(y):
print ('Are you sure y is continuous? It seems to be discrete.')
if self._isinteger(X):
print ('The values of X seem to be discrete. MI_FS will treat them'
'as continuous.')
return X, y
示例9: test_check_array_warn_on_dtype_deprecation
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def test_check_array_warn_on_dtype_deprecation():
X = np.asarray([[0.0], [1.0]])
Y = np.asarray([[2.0], [3.0]])
with pytest.warns(DeprecationWarning,
match="'warn_on_dtype' is deprecated"):
check_array(X, warn_on_dtype=True)
with pytest.warns(DeprecationWarning,
match="'warn_on_dtype' is deprecated"):
check_X_y(X, Y, warn_on_dtype=True)
示例10: test_check_X_y_informative_error
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def test_check_X_y_informative_error():
X = np.ones((2, 2))
y = None
assert_raise_message(ValueError, "y cannot be None", check_X_y, X, y)
示例11: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y, sample_weight=None):
X, y = utils.check_X_y(X, y, accept_sparse='csr', order='C')
def pr(x, y_i, y):
p = x[y == y_i].sum(0)
return (p+1) / ((y==y_i).sum()+1)
self.r_ = sparse.csr_matrix(np.log(pr(X, 1, y) / pr(X, 0, y)))
return super().fit(X.multiply(self.r_), y, sample_weight)
示例12: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y, **fit_params):
"""Determine which are the best cut points for each column in X based on y."""
X, y = check_X_y(X, y, y_numeric=True)
self.cut_points_ = [mdlp_cut(x, y, []) for x in X.T]
return self
示例13: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y, **fit_params):
"""Fit the model
Fit the base estimators on CV folds, then use their prediction on the
validation folds to train the meta-estimator. Then re-fit base
estimators on full training set.
Parameters
----------
X : np.ndarray, list of numbers
Training data.
y : np.ndarray, list of numbers
Training targets.
**fit_params : dict of {string, object}
Parameters passed to the ``fit`` method of each estimator, where
each parameter name is prefixed such that parameter ``p`` for
estimator ``s`` has key ``s__p``.
Returns
-------
self : BaseStackedModel
This estimator
"""
self._validate_estimators()
X, y = check_X_y(X, y, multi_output=True)
# Fit base estimators on CV training folds, produce features for
# meta-estimator from predictions on CV test folds.
Xmeta, ymeta, meta_params = self._base_est_fit_predict(X, y,
**fit_params)
# Fit meta-estimator on test fold predictions of base estimators.
self.meta_estimator.fit(Xmeta, ymeta, **meta_params)
# Now fit base estimators again, this time on full training set
self._base_est_fit(X, y, **fit_params)
return self
# _replace_est copied nearly verbatim from sklearn.pipeline._BasePipeline
# v0.18.1 "_replace_step" method.
示例14: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y, sample_weight=None):
"""Fit non-negative linear model.
Parameters
----------
X : numpy array or sparse matrix of shape [n_samples, n_features]
Training data
y : numpy array of shape [n_samples,]
Target values
sample_weight : numpy array of shape [n_samples]
Individual weights for each sample
Returns
-------
self : returns an instance of self.
"""
X, y = check_X_y(X, y, y_numeric=True, multi_output=False)
if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:
raise ValueError("Sample weights must be 1D array or scalar")
X, y, X_offset, y_offset, X_scale = self._preprocess_data(
X, y, fit_intercept=self.fit_intercept, normalize=self.normalize,
copy=self.copy_X, sample_weight=sample_weight)
if sample_weight is not None:
# Sample weight can be implemented via a simple rescaling.
X, y = _rescale_data(X, y, sample_weight)
self.coef_, result = nnls(X, y.squeeze())
if np.all(self.coef_ == 0):
raise ConvergenceWarning("All coefficients estimated to be zero in"
" the non-negative least squares fit.")
self._set_intercept(X_offset, y_offset, X_scale)
self.opt_result_ = OptimizeResult(success=True, status=0, x=self.coef_,
fun=result)
return self
示例15: fit
# 需要导入模块: from sklearn import utils [as 别名]
# 或者: from sklearn.utils import check_X_y [as 别名]
def fit(self, X, y):
"""Fit classifier.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
"""
# Validate inputs X and y
X, y = check_X_y(X, y)
X = check_array(X)
check_classification_targets(y)
self._classes = len(np.unique(y))
n_samples = X.shape[0]
# save the train ground truth for evaluation purpose
self.y_train_ = y
# build KDTree out of training subspace
self.tree_ = KDTree(X)
self.y_train_predicted_ = np.zeros(
[n_samples, self.n_base_estimators_])
# train all base classifiers on X, and get their local predicted scores
# iterate over all base classifiers
for i, clf in enumerate(self.base_estimators):
clf.fit(X, y)
self.y_train_predicted_[:, i] = clf.predict(X)
clf.fitted_ = True
self.fitted_ = True
return