tslearn.early_classification.非近视早期分类器¶
- class tslearn.early_classification.NonMyopicEarlyClassifier(n_clusters=2, base_classifier=None, min_t=1, lamb=1.0, cost_time_parameter=1.0, random_state=None)[source]¶
使用[1]中提出的模型进行时间序列的早期分类建模。
- Parameters:
- n_clustersint
要形成的集群数量。
- base_classifierEstimator or None
要克隆并用于分类的估计器(实例)。 如果为None,则选择的分类器是使用欧几里得度量的1NN。
- min_tint
可以在时间序列上执行分类的最早时间
- lambfloat
在计算成本函数时使用的超参数lambda的值,用于评估时间序列属于给定时间序列的集群的概率。
- cost_time_parameterfloat
时间成本函数的参数。该函数的形式为: f(time) = time * cost_time_parameter
- random_state: int
基础估计器的随机状态
- Attributes:
- classifiers_list
包含为模型训练的所有分类器的列表,即(maximum_time_stamp - min_t)个元素。
- pyhatyck_array like of shape (maximum_time_stamp - min_t, n_cluster, __n_classes, __n_classes)
包含经过训练的分类器在给定类别 y 和聚类 ck 的情况下被分类为类别 y_hat 的概率。数组的倒数第二个维度与序列的真实类别相关联,最后一个维度与预测类别相关联。
- pyck_array like of shape (__n_classes, n_cluster)
包含给定聚类ck的真实类别y的概率
- X_fit_dimstuple of the same shape as the training dataset
参考文献
[1]A. Dachraoui, A. Bondu & A. Cornuejols. 时间序列的早期分类作为一个非近视的序列决策问题。ECML/PKDD 2015
示例
>>> dataset = to_time_series_dataset([[1, 2, 3, 4, 5, 6], ... [1, 2, 3, 4, 5, 6], ... [1, 2, 3, 4, 5, 6], ... [1, 2, 3, 3, 2, 1], ... [1, 2, 3, 3, 2, 1], ... [1, 2, 3, 3, 2, 1], ... [3, 2, 1, 1, 2, 3], ... [3, 2, 1, 1, 2, 3]]) >>> y = [0, 0, 0, 1, 1, 1, 0, 0] >>> model = NonMyopicEarlyClassifier(n_clusters=3, lamb=1000., ... cost_time_parameter=.1, ... random_state=0) >>> model.fit(dataset, y) NonMyopicEarlyClassifier(...) >>> print(type(model.classifiers_)) <class 'dict'> >>> print(model.pyck_) [[0. 1. 1.] [1. 0. 0.]] >>> preds, pred_times = model.predict_class_and_earliness(dataset) >>> preds array([0, 0, 0, 1, 1, 1, 0, 0]) >>> pred_times array([4, 4, 4, 4, 4, 4, 1, 1]) >>> pred_probas, pred_times = model.predict_proba_and_earliness(dataset) >>> pred_probas array([[1., 0.], [1., 0.], [1., 0.], [0., 1.], [0., 1.], [0., 1.], [1., 0.], [1., 0.]]) >>> pred_times array([4, 4, 4, 4, 4, 4, 1, 1])
方法
计算早期分类得分。
fit(X, y)拟合早期分类器。
计算集群概率 \(P(c_k | Xi)\)。
获取此对象的元数据路由。
get_params([deep])获取此估计器的参数。
predict(X)提供预测类别。
提供预测类别以及预测时间戳。
概率估计。
提供概率估计以及预测时间戳。
score(X, y[, sample_weight])返回给定测试数据和标签的平均准确率。
set_params(**params)设置此估计器的参数。
set_score_request(*[, sample_weight])传递给
score方法的请求元数据。- early_classification_cost(X, y)[source]¶
计算早期分类分数。
分数计算如下:
\[1 - acc + \alpha \frac{1}{n} \sum_i t_i\]其中 \(\alpha\) 是权衡参数 (self.cost_time_parameter) 并且 \(t_i\) 是预测时间戳。
- Parameters:
- Xarray-like of shape (n_series, n_timestamps, n_features)
要评分的向量,其中n_series是时间序列的数量, n_timestamps是系列中的时间戳数量, n_features是每个时间戳记录的特征数量。
- yarray-like, shape = (n_samples) or (n_samples, n_outputs)
X的真实标签。
- Returns:
- float
早期分类成本(一个正数,越低越好)
示例
>>> dataset = to_time_series_dataset([[1, 2, 3, 4, 5, 6], ... [1, 2, 3, 4, 5, 6], ... [1, 2, 3, 4, 5, 6], ... [1, 2, 3, 3, 2, 1], ... [1, 2, 3, 3, 2, 1], ... [1, 2, 3, 3, 2, 1], ... [3, 2, 1, 1, 2, 3], ... [3, 2, 1, 1, 2, 3]]) >>> y = [0, 0, 0, 1, 1, 1, 0, 0] >>> model = NonMyopicEarlyClassifier(n_clusters=3, lamb=1000., ... cost_time_parameter=.1, ... random_state=0) >>> model.fit(dataset, y) NonMyopicEarlyClassifier(...) >>> preds, pred_times = model.predict_class_and_earliness(dataset) >>> preds array([0, 0, 0, 1, 1, 1, 0, 0]) >>> pred_times array([4, 4, 4, 4, 4, 4, 1, 1]) >>> model.early_classification_cost(dataset, y) 0.325
- fit(X, y)[source]¶
拟合早期分类器。
- Parameters:
- Xarray-like of shape (n_series, n_timestamps, n_features)
训练数据,其中n_series是时间序列的数量, n_timestamps是序列中的时间戳数量, n_features是每个时间戳记录的特征数量。
- yarray-like of shape (n_samples,)
目标值。如果需要,将转换为X的数据类型
- Returns:
- selfreturns an instance of self.
- get_cluster_probas(Xi)[source]¶
计算集群概率 \(P(c_k | Xi)\)。
这个量是使用以下公式计算的:
\[P(c_k | Xi) = \frac{s_k(Xi)}{\sum_j s_j(Xi)}\]其中
\[s_k(Xi) = \frac{1}{1 + \exp{-\lambda \Delta_k(Xi)}}\]使用
\[\Delta_k(Xi) = \frac{\bar{D} - d(Xi, c_k)}{\bar{D}}\]并且 \(\bar{D}\) 是 Xi 与聚类中心之间距离的平均值。
- Parameters:
- Xi: numpy array, shape (t, d)
观察到时间t的时间序列
- Returns:
- probasnumpy array, shape (n_clusters, )
示例
>>> from tslearn.utils import to_time_series >>> dataset = to_time_series_dataset([[1, 2, 3, 4, 5, 6], ... [1, 2, 3, 4, 5, 6], ... [1, 2, 3, 4, 5, 6], ... [1, 2, 3, 3, 2, 1], ... [1, 2, 3, 3, 2, 1], ... [1, 2, 3, 3, 2, 1], ... [3, 2, 1, 1, 2, 3], ... [3, 2, 1, 1, 2, 3]]) >>> y = [0, 0, 0, 1, 1, 1, 0, 0] >>> ts0 = to_time_series([1, 2]) >>> model = NonMyopicEarlyClassifier(n_clusters=3, lamb=0., ... random_state=0) >>> probas = model.fit(dataset, y).get_cluster_probas(ts0) >>> probas.shape (3,) >>> probas array([0.33..., 0.33..., 0.33...]) >>> model = NonMyopicEarlyClassifier(n_clusters=3, lamb=10000., ... random_state=0) >>> probas = model.fit(dataset, y).get_cluster_probas(ts0) >>> probas.shape (3,) >>> probas array([0.5, 0.5, 0. ]) >>> ts1 = to_time_series([3, 2]) >>> model.get_cluster_probas(ts1) array([0., 0., 1.])
- get_metadata_routing()[source]¶
获取此对象的元数据路由。
请查看用户指南了解路由机制的工作原理。
- Returns:
- routingMetadataRequest
一个封装了路由信息的
MetadataRequest。
- get_params(deep=True)[source]¶
获取此估计器的参数。
- Parameters:
- deepbool, default=True
如果为True,将返回此估计器及其包含的子对象(如果也是估计器)的参数。
- Returns:
- paramsdict
参数名称映射到它们的值。
- predict(X)[source]¶
提供预测类别。
- Parameters:
- Xarray-like of shape (n_series, n_timestamps, n_features)
要评分的向量,其中n_series是时间序列的数量, n_timestamps是系列中的时间戳数量, n_features是每个时间戳记录的特征数量。
- Returns:
- array, shape (n_samples,)
预测类别。
- predict_class_and_earliness(X)[source]¶
提供预测类别以及预测时间戳。
预测时间戳是在早期分类设置中进行预测的时间戳。
- Parameters:
- Xarray-like of shape (n_series, n_timestamps, n_features)
要评分的向量,其中n_series是时间序列的数量, n_timestamps是系列中的时间戳数量, n_features是每个时间戳记录的特征数量。
- Returns:
- array, shape (n_samples,)
预测类别。
- array-like of shape (n_series, )
预测时间戳。
- predict_proba(X)[source]¶
概率估计。
返回的所有类别的估计值按类别的标签排序。
- Parameters:
- Xarray-like of shape (n_series, n_timestamps, n_features)
要评分的向量,其中n_series是时间序列的数量, n_timestamps是系列中的时间戳数量, n_features是每个时间戳记录的特征数量。
- Returns:
- array-like of shape (n_series, n_classes)
模型中每个类别的样本概率, 其中类别顺序与
self.classes_中的顺序相同。
- predict_proba_and_earliness(X)[source]¶
提供概率估计以及预测时间戳。
预测时间戳是在早期分类设置中进行预测的时间戳。 所有类别的返回估计值按类别标签排序。
- Parameters:
- Xarray-like of shape (n_series, n_timestamps, n_features)
要评分的向量,其中n_series是时间序列的数量, n_timestamps是系列中的时间戳数量, n_features是每个时间戳记录的特征数量。
- Returns:
- array-like of shape (n_series, n_classes)
模型中每个类别的样本概率,其中类别顺序与
self.classes_中的顺序相同。- array-like of shape (n_series, )
预测时间戳。
- score(X, y, sample_weight=None)[source]¶
返回给定测试数据和标签的平均准确率。
在多标签分类中,这是子集准确率,这是一个严格的指标,因为您要求每个样本的每个标签集都被正确预测。
- Parameters:
- Xarray-like of shape (n_samples, n_features)
测试样本。
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
X的真实标签。
- sample_weightarray-like of shape (n_samples,), default=None
样本权重。
- Returns:
- scorefloat
self.predict(X)相对于 y 的平均准确率。
- set_params(**params)[source]¶
设置此估计器的参数。
该方法适用于简单的估计器以及嵌套对象(如
Pipeline)。后者具有形式为<component>__<parameter>的参数,以便可以更新嵌套对象的每个组件。- Parameters:
- **paramsdict
估计器参数。
- Returns:
- selfestimator instance
估计器实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') NonMyopicEarlyClassifier[source]¶
传递给
score方法的请求元数据。请注意,此方法仅在
enable_metadata_routing=True时相关(参见sklearn.set_config())。 请参阅 用户指南 了解路由机制的工作原理。每个参数的选项是:
True: 请求元数据,并在提供时传递给score。如果未提供元数据,则忽略该请求。False: 不请求元数据,元估计器不会将其传递给score。None: 不请求元数据,如果用户提供了元数据,元估计器将引发错误。str: 元数据应该使用这个给定的别名传递给元估计器,而不是原始名称。
默认值(
sklearn.utils.metadata_routing.UNCHANGED)保留现有的请求。这允许您更改某些参数的请求,而不更改其他参数。版本1.3中的新功能。
注意
此方法仅在此估计器用作元估计器的子估计器时相关,例如在
Pipeline内部使用。否则它没有效果。- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
元数据路由用于
score中的sample_weight参数。
- Returns:
- selfobject
更新后的对象。