- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我目前正在研究 ODP 文档的大规模分层文本分类。提供给我的数据集是 libSVM 格式的。我正在尝试运行 python 的 scikit-learn 的线性核 SVM 来开发模型。以下是来自训练样本的样本数据:
29 9454:1 11742:1 18884:14 26840:1 35147:1 52782:1 72083:1 73244:1 78945:1 79913:1 79986:1 86710:3 117286:1 139820:1 142458:1 146315:1 151005:2 161454:3 172237:1 1091130:1 1113562:1 1133451:1 1139046:1 1157534:1 1180618:2 1182024:1 1187711:1 1194345:3
33 2474:1 8152:1 19529:2 35038:1 48104:1 59738:1 61854:3 67943:1 74093:1 78945:1 88558:1 90848:1 97087:1 113284:16 118917:1 122375:1 124939:1
下面是我用来构建线性SVM模型的代码
from sklearn.datasets import load_svmlight_file
from sklearn import svm
X_train, y_train = load_svmlight_file("/path-to-file/train.txt")
X_test, y_test = load_svmlight_file("/path-to-file/test.txt")
clf = svm.SVC(kernel='linear')
clf.fit(X_train, y_train)
print clf.score(X_test,y_test)
运行 clf.score() 时,出现以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-b285fbfb3efe> in <module>()
1 start_time = time.time()
----> 2 print clf.score(X_test,y_test)
3 print time.time() - start_time, "seconds"
/Users/abc/anaconda/lib/python2.7/site-packages/sklearn/base.pyc in score(self, X, y)
292 """
293 from .metrics import accuracy_score
--> 294 return accuracy_score(y, self.predict(X))
295
296
/Users/abc/anaconda/lib/python2.7/site-packages/sklearn/svm/base.pyc in predict(self, X)
464 Class labels for samples in X.
465 """
--> 466 y = super(BaseSVC, self).predict(X)
467 return self.classes_.take(y.astype(np.int))
468
/Users/abc/anaconda/lib/python2.7/site-packages/sklearn/svm/base.pyc in predict(self, X)
280 y_pred : array, shape (n_samples,)
281 """
--> 282 X = self._validate_for_predict(X)
283 predict = self._sparse_predict if self._sparse else self._dense_predict
284 return predict(X)
/Users/abc/anaconda/lib/python2.7/site-packages/sklearn/svm/base.pyc in _validate_for_predict(self, X)
402 raise ValueError("X.shape[1] = %d should be equal to %d, "
403 "the number of features at training time" %
--> 404 (n_features, self.shape_fit_[1]))
405 return X
406
ValueError: X.shape[1] = 1199847 should be equal to 1199830, the number of features at training time
有人可以让我知道这段代码或我拥有的数据究竟有什么问题吗?提前致谢
下面附上X_train、y_train、X_test、y_test的值:
X_train:
(0, 9453) 1.0
(0, 11741) 1.0
(0, 18883) 14.0
(0, 26839) 1.0
(0, 35146) 1.0
(0, 52781) 1.0
(0, 72082) 1.0
(0, 73243) 1.0
(0, 78944) 1.0
(0, 79912) 1.0
(0, 79985) 1.0
(0, 86709) 3.0
(0, 117285) 1.0
(0, 139819) 1.0
(0, 142457) 1.0
(0, 146314) 1.0
(0, 151004) 2.0
(0, 161453) 3.0
(0, 172236) 1.0
(0, 187531) 2.0
(0, 202462) 1.0
(0, 210417) 1.0
(0, 250581) 1.0
(0, 251689) 1.0
(0, 296384) 2.0
: :
(4462, 735469) 1.0
(4462, 737059) 15.0
(4462, 740127) 1.0
(4462, 743798) 1.0
(4462, 766063) 1.0
(4462, 778958) 2.0
(4462, 784004) 4.0
(4462, 837264) 2.0
(4462, 839095) 22.0
(4462, 844735) 6.0
(4462, 859721) 2.0
(4462, 875267) 1.0
(4462, 910761) 1.0
(4462, 931244) 1.0
(4462, 945069) 6.0
(4462, 948728) 1.0
(4462, 948850) 2.0
(4462, 957682) 1.0
(4462, 975170) 1.0
(4462, 989192) 1.0
(4462, 1014294) 1.0
(4462, 1042424) 1.0
(4462, 1049027) 1.0
(4462, 1072931) 1.0
(4462, 1145790) 1.0
y_train:
[ 2.90000000e+01 3.30000000e+01 3.30000000e+01 ..., 1.65475000e+05
1.65518000e+05 1.65518000e+05]
X_测试:
(0, 18573) 1.0
(0, 23501) 1.0
(0, 29954) 1.0
(0, 42112) 1.0
(0, 46402) 1.0
(0, 63041) 2.0
(0, 67942) 2.0
(0, 83522) 1.0
(0, 88413) 2.0
(0, 99454) 1.0
(0, 126041) 1.0
(0, 139819) 1.0
(0, 142678) 1.0
(0, 151004) 1.0
(0, 166351) 2.0
(0, 173794) 1.0
(0, 192162) 3.0
(0, 210417) 2.0
(0, 254468) 1.0
(0, 263895) 2.0
(0, 277567) 1.0
(0, 278419) 2.0
(0, 279181) 2.0
(0, 281319) 2.0
(0, 298898) 1.0
: :
(1857, 1100504) 3.0
(1857, 1103247) 1.0
(1857, 1105578) 1.0
(1857, 1108986) 2.0
(1857, 1118486) 1.0
(1857, 1120807) 9.0
(1857, 1129243) 2.0
(1857, 1131786) 1.0
(1857, 1134029) 2.0
(1857, 1134410) 5.0
(1857, 1134494) 1.0
(1857, 1139045) 25.0
(1857, 1142239) 3.0
(1857, 1142651) 1.0
(1857, 1144787) 1.0
(1857, 1151891) 1.0
(1857, 1152094) 1.0
(1857, 1157533) 1.0
(1857, 1159376) 1.0
(1857, 1178944) 1.0
(1857, 1181310) 2.0
(1857, 1182023) 1.0
(1857, 1187098) 1.0
(1857, 1194344) 2.0
(1857, 1195819) 9.0
y_测试:
[ 2.90000000e+01 3.30000000e+01 1.56000000e+02 ..., 1.65434000e+05
1.65475000e+05 1.65518000e+05]
最佳答案
错误信息
ValueError: X.shape[1] = 1199847 should be equal to 1199830, the number of features at training time
self 解释:测试数据中的特征数量与用于训练模型的训练数据相比是不同的。也就是说,X_train.shape[1]
不等于 X_test.shape[1]
。
您应该检查为什么它们不相等,因为它们应该相等。
一种可能是它们作为稀疏矩阵加载,特征数量由 load_svmlight_file
推断。 .如果测试数据包含训练数据看不到的特征,则生成的 X_test
可能具有更大的维度。为避免这种情况,您可以通过传递参数 n_features
来指定 load_svmlight_file
中的特征数量。
关于python - 使用 scikit-learn python 的线性 SVM 时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167095/
基本上,我的问题是,由于无监督学习是机器学习的一种,是否需要机器“学习”的某些方面并根据其发现进行改进?例如,如果开发了一种算法来获取未标记的图像并找到它们之间的关联,那么它是否需要根据这些关联来改进
生成模型和判别模型似乎可以学习条件 P(x|y) 和联合 P(x,y) 概率分布。但从根本上讲,我无法说服自己“学习概率分布”意味着什么。 最佳答案 这意味着您的模型要么充当训练样本的分布估计器,要么
是否有类似于 的 scikit-learn 方法/类元成本 在 Weka 或其他实用程序中实现的算法以执行常量敏感分析? 最佳答案 不,没有。部分分类器提供 class_weight和 sample_
是否Scikit-learn支持迁移学习?请检查以下代码。 型号 clf由 fit(X,y) 获取 jar 头型号clf2在clf的基础上学习和转移学习 fit(X2,y2) ? >>> from s
我发现使用相同数据的两种交叉验证技术之间的分类性能存在差异。我想知道是否有人可以阐明这一点。 方法一:cross_validation.train_test_split 方法 2:分层折叠。 具有相同
我正在查看 scikit-learn 文档中的这个示例:http://scikit-learn.org/0.18/auto_examples/model_selection/plot_nested_c
我想训练一个具有很多标称属性的数据集。我从一些帖子中注意到,要转换标称属性必须将它们转换为重复的二进制特征。另外据我所知,这样做在概念上会使数据集稀疏。我也知道 scikit-learn 使用稀疏矩阵
我正在尝试在 scikit-learn (sklearn.feature_selection.SelectKBest) 中通过卡方方法进行特征选择。当我尝试将其应用于多标签问题时,我收到此警告: 用户
有几种算法可以构建决策树,例如 CART(分类和回归树)、ID3(迭代二分法 3)等 scikit-learn 默认使用哪种决策树算法? 当我查看一些决策树 python 脚本时,它神奇地生成了带有
我正在尝试在 scikit-learn (sklearn.feature_selection.SelectKBest) 中通过卡方方法进行特征选择。当我尝试将其应用于多标签问题时,我收到此警告: 用户
有几种算法可以构建决策树,例如 CART(分类和回归树)、ID3(迭代二分法 3)等 scikit-learn 默认使用哪种决策树算法? 当我查看一些决策树 python 脚本时,它神奇地生成了带有
有没有办法让 scikit-learn 中的 fit 方法有一个进度条? 是否可以包含自定义的类似 Pyprind 的内容? ? 最佳答案 如果您使用 verbose=1 初始化模型调用前 fit你应
我正在使用基于 rlglue 的 python-rl q 学习框架。 我的理解是,随着情节的发展,算法会收敛到一个最优策略(这是一个映射,说明在什么状态下采取什么行动)。 问题 1:这是否意味着经过若
我正在尝试使用 grisSearchCV 在 scikit-learn 中拟合一些模型,并且我想使用“一个标准错误”规则来选择最佳模型,即从分数在 1 以内的模型子集中选择最简约的模型最好成绩的标准误
我正在尝试离散数据以进行分类。它们的值是字符串,我将它们转换为数字 0,1,2,3。 这就是数据的样子(pandas 数据框)。我已将数据帧拆分为 dataLabel 和 dataFeatures L
每当我开始拥有更多的类(1000 或更多)时,MultinominalNB 就会变得非常慢并且需要 GB 的 RAM。对于所有支持 .partial_fit()(SGDClassifier、Perce
我需要使用感知器算法来研究一些非线性可分数据集的学习率和渐近误差。 为了做到这一点,我需要了解构造函数的一些参数。我花了很多时间在谷歌上搜索它们,但我仍然不太明白它们的作用或如何使用它们。 给我带来更
我知道作为功能 ordinal data could be assigned arbitrary numbers and OneHotEncoding could be done for catego
这是一个示例,其中有逐步的过程使系统学习并对输入数据进行分类。 它对给定的 5 个数据集域进行了正确分类。此外,它还对停用词进行分类。 例如 输入:docs_new = ['上帝就是爱', '什么在哪
我有一个 scikit-learn 模型,它简化了一点,如下所示: clf1 = RandomForestClassifier() clf1.fit(data_training, non_binary
我是一名优秀的程序员,十分优秀!