python - NP 阵列的形状为 (2186, 128)。我有兴趣将获得的数组应用于 SVM
问题描述
我提取了 CNN 倒数第二层(# layer 12)的输出。提取的 NP 阵列具有形状 (2186, 128)。我有兴趣将获得的数组应用于 SVM。
提取特征的代码:
import numpy as np
X_train=np.array(get_activations(model=model,layer=12, X_batch=x_train)[0], dtype=np.float32)
print(X_train)
这给了我形状的输出 (2186, 128)
将上述 np 数组应用于 SVM 的代码:
from sklearn.svm import SVC
clf = SVC()
clf.fit(X_train, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
这是给出错误:
ValueError Traceback (most recent call last)
<ipython-input-54-2d6b8b03f3c1> in <module>()
----> 1 clf.fit(X_train, y)
2 SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
3 decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
4 max_iter=-1, probability=False, random_state=None, shrinking=True,
5 tol=0.001, verbose=False)
~/anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
147 self._sparse = sparse and not callable(self.kernel)
148
--> 149 X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
150 y = self._validate_targets(y)
151
~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
576 dtype=None)
577 else:
--> 578 y = column_or_1d(y, warn=True)
579 _assert_all_finite(y)
580 if y_numeric and y.dtype.kind == 'O':
~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in column_or_1d(y, warn)
612 return np.ravel(y)
613
--> 614 raise ValueError("bad input shape {0}".format(shape))
615
616
ValueError: bad input shape (2186, 3)
解决方案
好像你y
有一个热编码(3个类)。将其转换为整数标签 (0, 1, 2) 就可以了。
推荐阅读
- mysql - 为什么我们需要在两个选择中使用 () 和和?
- r - 搜索时闪亮的数据框
- jmeter - 如何在 Jmeter 从 csv 文件中获取输入时跳过特定列?
- typescript - 无法从打字稿导入类
- python - 度量和损失函数 keras
- javascript - 第二次调用 fetch 时没有调用超时
- node.js - socket.io 确定用户是在线还是离线。我应该将数据存储在数据库中吗?
- python - 写入数据库时将元数据用于数据类型
- javascript - 冲浪后丢失参考创建窗口
- java - 一切看起来都很完美,但 Eclipse 安装程序无法安装,原因是找不到 JDK