本文整理汇总了Python中sklearn.utils.multiclass.unique_labels方法的典型用法代码示例。如果您正苦于以下问题:Python multiclass.unique_labels方法的具体用法?Python multiclass.unique_labels怎么用?Python multiclass.unique_labels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils.multiclass
的用法示例。
在下文中一共展示了multiclass.unique_labels方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_story_evaluation
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def run_story_evaluation(story_file, policy_model_path, nlu_model_path,
out_file, max_stories):
"""Run the evaluation of the stories, plots the results."""
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
test_y, preds = collect_story_predictions(story_file, policy_model_path,
nlu_model_path, max_stories)
log_evaluation_table(test_y, preds)
cnf_matrix = confusion_matrix(test_y, preds)
plot_confusion_matrix(cnf_matrix, classes=unique_labels(test_y, preds),
title='Action Confusion matrix')
fig = plt.gcf()
fig.set_size_inches(int(20), int(20))
fig.savefig(out_file, bbox_inches='tight')
示例2: test_unique_labels_non_specific
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def test_unique_labels_non_specific():
# Test unique_labels with a variety of collected examples
# Smoke test for all supported format
for format in ["binary", "multiclass", "multilabel-indicator"]:
for y in EXAMPLES[format]:
unique_labels(y)
# We don't support those format at the moment
for example in NON_ARRAY_LIKE_EXAMPLES:
assert_raises(ValueError, unique_labels, example)
for y_type in ["unknown", "continuous", 'continuous-multioutput',
'multiclass-multioutput']:
for example in EXAMPLES[y_type]:
assert_raises(ValueError, unique_labels, example)
示例3: fit
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def fit(self, y):
"""Fit label binarizer
Parameters
----------
y : array of shape [n_samples,] or [n_samples, n_classes]
Target values. The 2-d matrix should only contain 0 and 1,
represents multilabel classification.
Returns
-------
self : returns an instance of self.
"""
self.y_type_ = type_of_target(y)
if 'multioutput' in self.y_type_:
raise ValueError("Multioutput target data is not supported with "
"label binarization")
if _num_samples(y) == 0:
raise ValueError('y has 0 samples: %r' % y)
self.sparse_input_ = sp.issparse(y)
self.classes_ = unique_labels(y)
return self
示例4: _init_classes
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def _init_classes(self, y):
"""Map all possible classes to the range [0,..,C-1]
Parameters
----------
y : list of arrays of int, each element has shape=[samples_i,]
Labels of the samples for each subject
Returns
-------
new_y : list of arrays of int, each element has shape=[samples_i,]
Mapped labels of the samples for each subject
Note
----
The mapping of the classes is saved in the attribute classes_.
"""
self.classes_ = unique_labels(utils.concatenate_not_none(y))
new_y = [None] * len(y)
for s in range(len(y)):
new_y[s] = np.digitize(y[s], self.classes_) - 1
return new_y
示例5: plot_story_evaluation
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def plot_story_evaluation(test_y, predictions,
report, precision, f1, accuracy,
in_training_data_fraction,
out_directory):
"""Plot the results of story evaluation"""
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
import matplotlib.pyplot as plt
from rasa_nlu.test import plot_confusion_matrix
log_evaluation_table(test_y, "ACTION",
report, precision, f1, accuracy,
in_training_data_fraction,
include_report=True)
cnf_matrix = confusion_matrix(test_y, predictions)
plot_confusion_matrix(cnf_matrix,
classes=unique_labels(test_y, predictions),
title='Action Confusion matrix')
fig = plt.gcf()
fig.set_size_inches(int(20), int(20))
fig.savefig(os.path.join(out_directory, "story_confmat.pdf"),
bbox_inches='tight')
示例6: _preprocess_labels
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def _preprocess_labels(self, y):
self.classes_ = unique_labels(y)
n_labels = len(self.classes_)
if n_labels == 1:
raise ValueError("Classifier can't train when only one class "
"is present.")
if self.classes_.dtype in [numpy.int32, numpy.int64]:
self.label_to_ind_ = {int(lab): ind
for ind, lab in enumerate(self.classes_)}
else:
self.label_to_ind_ = {lab: ind
for ind, lab in enumerate(self.classes_)}
y_ind = numpy.array(
[self.label_to_ind_[lab] for lab in y]
)
y_ = to_categorical(y_ind)
if n_labels == 2:
y_ = y_[:, 1:] # Keep only indicator of positive class
return y_
示例7: __init__
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def __init__(self, X, y, mode='LAL_iterative', data_path='.', cls_est=50, train_slt=True, **kwargs):
super(QueryInstanceLAL, self).__init__(X, y)
if len(unique_labels(self.y)) != 2:
warnings.warn("This query strategy is implemented for binary classification only.",
category=FunctionWarning)
if not os.path.isdir(data_path):
raise ValueError("Please pass the directory of the file.")
self._iter_path = os.path.join(data_path, 'LAL-iterativetree-simulatedunbalanced-big.npz')
self._rand_path = os.path.join(data_path, 'LAL-randomtree-simulatedunbalanced-big.npz')
assert mode in ['LAL_iterative', 'LAL_independent']
self._mode = mode
self._selector = None
self.model = RandomForestClassifier(n_estimators=cls_est, oob_score=True, n_jobs=8)
if train_slt:
self.download_data()
self.train_selector_from_file()
示例8: fit
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def fit(self, X, y):
"""
Fit the 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.estimator, dtype=FLOAT_DTYPES)
if not isinstance(self.estimator, ProbabilisticClassifier):
raise ValueError(
"The ConfusionBalancer meta model only works on classifcation models with .predict_proba."
)
self.estimator.fit(X, y)
self.classes_ = unique_labels(y)
cfm = confusion_matrix(y, self.estimator.predict(X)).T + self.cfm_smooth
self.cfm_ = cfm / cfm.sum(axis=1).reshape(-1, 1)
return self
示例9: evaluate_intents
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def evaluate_intents(targets, predictions): # pragma: no cover
"""Creates a confusion matrix and summary statistics for intent predictions.
Only considers those examples with a set intent.
Others are filtered out."""
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
import matplotlib.pyplot as plt
# remove empty intent targets
num_examples = len(targets)
targets, predictions = remove_empty_intent_examples(targets, predictions)
logger.info("Intent Evaluation: Only considering those "
"{} examples that have a defined intent out "
"of {} examples".format(targets.size, num_examples))
log_evaluation_table(targets, predictions)
cnf_matrix = confusion_matrix(targets, predictions)
labels = unique_labels(targets, predictions)
plot_confusion_matrix(cnf_matrix,
classes=labels,
title='Intent Confusion matrix')
plt.show()
示例10: fit
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def fit(self, X, y):
"""A reference implementation of a fitting function for a classifier.
Parameters
----------
X : array-like, shape (n_samples, n_features)
The training input samples.
y : array-like, shape (n_samples,)
The target values. An array of int.
Returns
-------
self : object
Returns self.
"""
# Check that X and y have correct shape
X, y = check_X_y(X, y)
# Store the classes seen during fit
self.classes_ = unique_labels(y)
self.X_ = X
self.y_ = y
# Return the classifier
return self
示例11: partial_fit
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def partial_fit(self, X, y, classes=None, sample_weight=None):
"""Fit the LVQ model to the given training data and parameters using
gradient ascent.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vector, where n_samples in the number of samples and
n_features is the number of features.
y : numpy.ndarray of shape (n_samples, n_targets)
An array-like with the class labels of all samples in X
classes : numpy.ndarray, optional (default=None)
Contains all possible/known class labels. Usage varies depending
on the learning method.
sample_weight : Not used.
Returns
--------
self
"""
if set(unique_labels(y)).issubset(set(self.classes_)) or \
self.initial_fit is True:
X, y = self._validate_train_parms(X, y, classes=classes)
else:
raise ValueError('Class {} was not learned - please declare all \
classes in first call of fit/partial_fit'
.format(y))
self._optimize(X, y)
return self
开发者ID:scikit-multiflow,项目名称:scikit-multiflow,代码行数:32,代码来源:robust_soft_learning_vector_quantization.py
示例12: test_unique_labels
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def test_unique_labels():
# Empty iterable
assert_raises(ValueError, unique_labels)
# Multiclass problem
assert_array_equal(unique_labels(range(10)), np.arange(10))
assert_array_equal(unique_labels(np.arange(10)), np.arange(10))
assert_array_equal(unique_labels([4, 0, 2]), np.array([0, 2, 4]))
# Multilabel indicator
assert_array_equal(unique_labels(np.array([[0, 0, 1],
[1, 0, 1],
[0, 0, 0]])),
np.arange(3))
assert_array_equal(unique_labels(np.array([[0, 0, 1],
[0, 0, 0]])),
np.arange(3))
# Several arrays passed
assert_array_equal(unique_labels([4, 0, 2], range(5)),
np.arange(5))
assert_array_equal(unique_labels((0, 1, 2), (0,), (2, 1)),
np.arange(3))
# Border line case with binary indicator matrix
assert_raises(ValueError, unique_labels, [4, 0, 2], np.ones((5, 5)))
assert_raises(ValueError, unique_labels, np.ones((5, 4)), np.ones((5, 5)))
assert_array_equal(unique_labels(np.ones((4, 5)), np.ones((5, 5))),
np.arange(5))
示例13: test_unique_labels_mixed_types
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def test_unique_labels_mixed_types():
# Mix with binary or multiclass and multilabel
mix_clf_format = product(EXAMPLES["multilabel-indicator"],
EXAMPLES["multiclass"] +
EXAMPLES["binary"])
for y_multilabel, y_multiclass in mix_clf_format:
assert_raises(ValueError, unique_labels, y_multiclass, y_multilabel)
assert_raises(ValueError, unique_labels, y_multilabel, y_multiclass)
assert_raises(ValueError, unique_labels, [[1, 2]], [["a", "d"]])
assert_raises(ValueError, unique_labels, ["1", 2])
assert_raises(ValueError, unique_labels, [["1", 2], [1, 3]])
assert_raises(ValueError, unique_labels, [["1", "2"], [2, 3]])
示例14: test_split1_allclass
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def test_split1_allclass():
train_idx, test_idx, label_idx, unlabel_idx = split(X=X,
y=y,
all_class=True, split_count=split_count,
test_ratio=0.3, initial_label_rate=0.05,
saving_path=None,
query_type='AllLabels')
assert len(train_idx) == split_count
assert len(test_idx) == split_count
assert len(label_idx) == split_count
assert len(unlabel_idx) == split_count
for i in range(split_count):
train = set(train_idx[i])
test = set(test_idx[i])
lab = set(label_idx[i])
unl = set(unlabel_idx[i])
assert len(test) == round(0.3 * instance_num)
assert len(lab) == round(0.05 * len(train))
# validity
traintest = train.union(test)
labun = lab.union(unl)
assert traintest == set(range(instance_num))
assert labun == train
# is all-class
len(unique_labels(y[label_idx[i]])) == label_num
示例15: fit
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import unique_labels [as 别名]
def fit(self, X: np.ndarray, y: np.ndarray):
"""
Fit the model using X, y as training data.
:param X: array-like, shape=(n_features, n_samples)
:param y: array-like, shape=(n_samples)
:return: Returns an instance of self
"""
X, y = check_X_y(X, y, estimator=self, dtype=FLOAT_DTYPES)
self.classes_ = unique_labels(y)
self.models_, self.priors_logp_ = {}, {}
for target_label in self.classes_:
x_subset = X[y == target_label]
# Computing joint distribution
self.models_[target_label] = KernelDensity(
bandwidth=self.bandwidth,
kernel=self.kernel,
algorithm=self.algorithm,
metric=self.metric,
atol=self.atol,
rtol=self.rtol,
breadth_first=self.breath_first,
leaf_size=self.leaf_size,
metric_params=self.metric_params,
).fit(x_subset)
# Computing target class prior
self.priors_logp_[target_label] = np.log(len(x_subset) / len(X))
return self