python-3.x - ValueError:未知标签类型
问题描述
我正在尝试对 SVR 进行网格搜索,并按照使用网格搜索和交叉验证进行参数估计中给出的教程进行操作,但出现错误:
ValueError: Unknown label type: (array([[0.0970681 ],
[0.04160906],
[0.00209168],
...,
[0.92857565],
[0.64930691],
[0.20325924]]), array([6.38226813, 6.18596882, 6.03850002, ..., 4.68553846, 7.06541915,
7.8636379 ]))
我的代码是:
param = {'kernel': ['rbf'], 'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
'C': [1, 10, 100, 1000]}
regressor_1 = SVR(C=1)
TS_split = TimeSeriesSplit(n_splits=3)
scoring='neg_mean_squared_error'
clf = GridSearchCV(regressor_1, param, cv=cv=timeseries_split, verbose=True)
X_gridsearch = pre.MinMaxScaler(feature_range=(0,1)).fit(X_feature)
scaled_X_gridsearch = X_gridsearch.transform(X_feature)
y_gridsearch = pre.MinMaxScaler(feature_range=(0,1)).fit(y_label)
scaled_y_gridsearch = y_gridsearch.transform(y_label)
for scoring in scoring:
print("Hypter Parameters for %s" % scoring)
clf.fit(scaled_X_gridsearch,scaled_y_gridsearch )
print (scaled_y_gridsearch )
print (clf.best_params_)
mean = clf.cv_results_['mean_test_score']
std = clf.cv_results_['std_test_score']
for mean, std, params in zip(mean, std, clf.cv_results_['params']):
print("%0.3f (+/-%0.03f) for %r"
% (mean, std * 2, params))
print("Detailed classification report:")
y_true, y_pred= y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
我的 scaled_y_gridsearch 数据是:
[0.11321139]
[0.07218848]
...
[0.64844211]
[0.4926122 ]
[0.4030334 ]]
我的 scaled_X_gridsearch 数据是:
[[0.2681013 ]
[0.03454225]
[0.02062136]
...
[0.92857565]
[0.64930691]
[0.20325924]]
完整的回溯错误消息是:
50 y_true, y_pred= y_test, clf.predict(X_test)
---> 51 print(classification_report(y_true, y_pred))
52
53
~/anaconda3_501/lib/python3.6/site-packages/sklearn/metrics/classification.py in classification_report(y_true, y_pred, labels, target_names, sample_weight, digits)
1419
1420 if labels is None:
-> 1421 labels = unique_labels(y_true, y_pred)
1422 else:
1423 labels = np.asarray(labels)
~/anaconda3_501/lib/python3.6/site-packages/sklearn/utils/multiclass.py in unique_labels(*ys)
95 _unique_labels = _FN_UNIQUE_LABELS.get(label_type, None)
96 if not _unique_labels:
---> 97 raise ValueError("Unknown label type: %s" % repr(ys))
98
99 ys_labels = set(chain.from_iterable(_unique_labels(y) for y in ys))
我不确定为什么这可能是我尽可能密切地遵循 Scikit 学习的例子。对此的帮助将不胜感激。
解决方案
Classification report
不是用于回归,而是用于分类类型的问题。查看此链接并查看“回归指标”,例如
r2_score、mean_squared_error、均方对数误差等。
如果这是一个分类问题,则将分类器从更改SVR
为SVC
,这应该可以工作。
推荐阅读
- amazon-web-services - Jenkins 报 SSLException: Read timed out error when installed the aws sdk plugin
- knative - Knative 并发背后的推理
- python - 一次处理多个输入框时出错
- django - 在django中为不同的用户设置不同的注销时间?
- karate - 空手道 v1 中的表格问题
- excel - Excel VBA 使用 .SetSourceData 设置图表的源数据
- javascript - onload Javascript 函数返回未定义
- python - 将 IP 地址映射到组织的网络
- r - 转换没有命名空间的 xml
- react-native - 反应导航自己的标题组件