本文整理汇总了Python中sklearn.utils.validation.column_or_1d方法的典型用法代码示例。如果您正苦于以下问题:Python validation.column_or_1d方法的具体用法?Python validation.column_or_1d怎么用?Python validation.column_or_1d使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils.validation
的用法示例。
在下文中一共展示了validation.column_or_1d方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_constant
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def is_constant(x):
"""Test ``x`` for constancy.
Determine whether a vector is composed of all of the same elements
and nothing else.
Parameters
----------
x : array-like, shape=(n_samples,)
The time series vector.
Examples
--------
>>> import numpy as np
>>> x = np.array([1, 2, 3])
>>> y = np.ones(3)
>>> [is_constant(x), is_constant(y)]
[False, True]
"""
x = column_or_1d(x) # type: np.ndarray
return (x == x[0]).all()
示例2: fit
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def fit(self, y):
"""Fit label encoder
Parameters
----------
y : array-like of shape (n_samples,)
Target values.
Returns
-------
self : returns an instance of self.
"""
y = column_or_1d(y, warn=True)
y = numpy.append(y, ['UNK'])
self.classes_ = numpy.unique(y)
return self
示例3: fit_transform
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def fit_transform(self, y, **kwargs):
"""Fit label encoder and return encoded labels
Parameters
----------
y : array-like of shape [n_samples]
Target values.
Returns
-------
y : array-like of shape [n_samples]
:param **kwargs:
"""
y = column_or_1d(y, warn=True)
y = numpy.append(y, ['UNK'])
self.classes_, y = numpy.unique(y, return_inverse=True)
return y
示例4: transform
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def transform(self, y):
"""Transform labels to normalized encoding.
Parameters
----------
y : array-like of shape [n_samples]
Target values.
Returns
-------
y : array-like of shape [n_samples]
"""
check_is_fitted(self, 'classes_')
y = column_or_1d(y, warn=True)
y = numpy.array(list(map(lambda x: x if x in self.classes_ else 'UNK', y)))
classes = numpy.unique(y)
if len(numpy.intersect1d(classes, self.classes_)) < len(classes):
diff = numpy.setdiff1d(classes, self.classes_)
raise ValueError("y contains new labels: %s" % str(diff))
return numpy.searchsorted(self.classes_, y)
示例5: _train_predictor
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def _train_predictor(self, problem, classes=None, hyperparams=None):
if problem == SupervisedLearningPipeline.CLASSIFICATION:
if 'bifurcated' in hyperparams['algorithm']:
learning_class = BifurcatedSupervisedClassifier
# Strip 'bifurcated-' from algorithm for SupervisedClassifier.
hyperparams['algorithm'] = '-'.join(hyperparams['algorithm'].split('-')[1:])
else:
learning_class = SupervisedClassifier
self._predictor = learning_class(classes, hyperparams)
elif problem == SupervisedLearningPipeline.REGRESSION:
learning_class = Regressor
self._predictor = learning_class(algorithm=algorithm)
status = self._predictor.train(self._X_train, column_or_1d(self._y_train),
groups = self._patIds_train)
return status
示例6: setUp
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def setUp(self):
log.level = logging.ERROR
# Use simple classifier and test case for testing non-ROC analyses.
X = RANDOM_10_TEST_CASE['X']
y = RANDOM_10_TEST_CASE['y']
self._list_classifier = ListPredictor([0, 1])
self._lc_analyzer = ClassifierAnalyzer(self._list_classifier, X, y)
# Use ml classifier and complex test case.
X = RANDOM_100_TEST_CASE['X']
y = RANDOM_100_TEST_CASE['y']
# Generate train/test split.
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=123456789)
# Train logistic regression model.
hyperparams = {
'algorithm': SupervisedClassifier.REGRESS_AND_ROUND,
'random_state': 123456789
}
self._ml_classifier = SupervisedClassifier([0, 1], hyperparams)
self._ml_classifier.train(X_train, column_or_1d(y_train))
self._ml_analyzer = ClassifierAnalyzer(self._ml_classifier, X_test, y_test)
示例7: _select_features
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def _select_features(self):
# Use FeatureSelector to prune all but 100 variables.
fs = FeatureSelector(algorithm=FeatureSelector.RECURSIVE_ELIMINATION, \
problem=FeatureSelector.CLASSIFICATION)
fs.set_input_matrix(self._X_train, column_or_1d(self._y_train))
num_features_to_select = int(0.01*len(self._X_train.columns.values))
fs.select(k=num_features_to_select)
# Enumerate eliminated features pre-transformation.
self._feature_ranks = fs.compute_ranks()
for i in range(len(self._feature_ranks)):
if self._feature_ranks[i] > num_features_to_select:
self._eliminated_features.append(self._X_train.columns[i])
self._X_train = fs.transform_matrix(self._X_train)
self._X_test = fs.transform_matrix(self._X_test)
示例8: _validate
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def _validate(self, y):
"""Validates input time series. Also adjusts box_cox if necessary."""
try:
y = c1d(check_array(y, ensure_2d=False, force_all_finite=True, ensure_min_samples=1,
copy=True, dtype=np.float64)) # type: np.ndarray
except Exception as validation_exception:
self.context.get_exception_handler().exception(
"y series is invalid", error.InputArgsException, previous_exception=validation_exception
)
return False
if np.any(y <= 0):
if self.use_box_cox is True:
self.context.get_exception_handler().warn(
"Box-Cox transformation (use_box_cox) was forced to True "
"but there are negative values in input series. "
"Setting use_box_cox to False.",
error.InputArgsWarning
)
self.use_box_cox = False
return y
示例9: as_series
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def as_series(x):
"""Cast as pandas Series.
Cast an iterable to a Pandas Series object. Note that the index
will simply be a positional ``arange`` and cannot be set in this
function.
Parameters
----------
x : array-like, shape=(n_samples,)
The 1d array on which to compute the auto correlation.
Examples
--------
>>> as_series([1, 2, 3])
0 1
1 2
2 3
dtype: int64
>>> as_series(as_series((1, 2, 3)))
0 1
1 2
2 3
dtype: int64
>>> import pandas as pd
>>> as_series(pd.Series([4, 5, 6], index=['a', 'b', 'c']))
a 4
b 5
c 6
dtype: int64
Returns
-------
s : pd.Series
A pandas Series object.
"""
if isinstance(x, pd.Series):
return x
return pd.Series(column_or_1d(x))
示例10: check_endog
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def check_endog(y, dtype=DTYPE, copy=True, force_all_finite=False):
"""Wrapper for ``check_array`` and ``column_or_1d`` from sklearn
Parameters
----------
y : array-like, shape=(n_samples,)
The 1d endogenous array.
dtype : string, type or None (default=np.float64)
Data type of result. If None, the dtype of the input is preserved.
If "numeric", dtype is preserved unless array.dtype is object.
copy : bool, optional (default=False)
Whether a forced copy will be triggered. If copy=False, a copy might
still be triggered by a conversion.
force_all_finite : bool, optional (default=False)
Whether to raise an error on np.inf and np.nan in an array. The
possibilities are:
- True: Force all values of array to be finite.
- False: accept both np.inf and np.nan in array.
Returns
-------
y : np.ndarray, shape=(n_samples,)
A 1d numpy ndarray
"""
return column_or_1d(
check_array(y, ensure_2d=False, force_all_finite=force_all_finite,
copy=copy, dtype=dtype)) # type: np.ndarray
示例11: fit
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def fit(self, y):
"""Fit label encoder.
Parameters
----------
y : array-like of shape (n_samples,)
Label values.
Returns
-------
self : RobustLabelEncoder.
"""
y = column_or_1d(y, warn=True)
self.classes_ = self._check_labels_and_sort() or _encode(y)
return self
示例12: transform
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def transform(self, y):
"""Transform labels to normalized encoding.
If ``self.fill_unseen_labels`` is ``True``, use ``self.fill_encoded_label_value`` for unseen values.
Seen labels are encoded with value between 0 and n_classes-1. Unseen labels are encoded with
``self.fill_encoded_label_value`` with a default value of n_classes.
Parameters
----------
y : array-like of shape [n_samples]
Label values.
Returns
-------
y_encoded : array-like of shape [n_samples]
Encoded label values.
"""
check_is_fitted(self, "classes_")
y = column_or_1d(y, warn=True)
# transform of empty array is empty array
if _num_samples(y) == 0:
return np.array([])
if self.fill_unseen_labels:
_, mask = _encode_check_unknown(y, self.classes_, return_mask=True)
y_encoded = np.searchsorted(self.classes_, y)
fill_encoded_label_value = self.fill_encoded_label_value or len(self.classes_)
y_encoded[~mask] = fill_encoded_label_value
else:
_, y_encoded = _encode(y, uniques=self.classes_, encode=True)
return y_encoded
示例13: inverse_transform
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def inverse_transform(self, y):
"""Transform labels back to original encoding.
If ``self.fill_unseen_labels`` is ``True``, use ``self.fill_label_value`` for unseen values.
Parameters
----------
y : numpy array of shape [n_samples]
Encoded label values.
Returns
-------
y_decoded : numpy array of shape [n_samples]
Label values.
"""
check_is_fitted(self, "classes_")
y = column_or_1d(y, warn=True)
if y.dtype.kind not in ("i", "u"):
try:
y = y.astype(np.float).astype(np.int)
except ValueError:
raise ValueError("`y` contains values not convertible to integer.")
# inverse transform of empty array is empty array
if _num_samples(y) == 0:
return np.array([])
labels = np.arange(len(self.classes_))
diff = np.setdiff1d(y, labels)
if diff and not self.fill_unseen_labels:
raise ValueError("y contains previously unseen labels: %s" % str(diff))
y_decoded = [self.classes_[idx] if idx in labels else self.fill_label_value for idx in y]
return y_decoded
示例14: _maybe_reshape_y
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def _maybe_reshape_y(self, y):
# If necessary, reshape y from (n_samples, 1) to (n_samples, )
try:
num_cols = y.shape[1]
y = column_or_1d(y)
log.debug('Reshaped y to 1d.')
except IndexError:
log.debug('Did not need to reshape y to 1d.')
return y
示例15: check_inputs
# 需要导入模块: from sklearn.utils import validation [as 别名]
# 或者: from sklearn.utils.validation import column_or_1d [as 别名]
def check_inputs(X, y, sample_weight=None, ensure_2d=True):
"""Input validation for debiasing algorithms.
Checks all inputs for consistent length, validates shapes (optional for X),
and returns an array of all ones if sample_weight is ``None``.
Args:
X (array-like): Input data.
y (array-like, shape = (n_samples,)): Target values.
sample_weight (array-like, optional): Sample weights.
ensure_2d (bool, optional): Whether to raise a ValueError if X is not
2D.
Returns:
tuple:
* **X** (`array-like`) -- Validated X. Unchanged.
* **y** (`array-like`) -- Validated y. Possibly converted to 1D if
not a :class:`pandas.Series`.
* **sample_weight** (`array-like`) -- Validated sample_weight. If no
sample_weight is provided, returns a consistent-length array of
ones.
"""
if ensure_2d and X.ndim != 2:
raise ValueError("Expected X to be 2D, got ndim == {} instead.".format(
X.ndim))
if not isinstance(y, pd.Series): # don't cast Series -> ndarray
y = column_or_1d(y)
if sample_weight is not None:
sample_weight = column_or_1d(sample_weight)
else:
sample_weight = np.ones(X.shape[0])
check_consistent_length(X, y, sample_weight)
return X, y, sample_weight