- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用来自 scikit 学习网站的这段代码:
http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html
我正在使用自己的数据。我的问题是,我有两个以上的功能。如果我想将特征从 2 个“扩展”到 3 个或 4 个....
我得到:
“查询数据维度必须匹配训练数据维度”
def machine():
with open("test.txt",'r') as csvr:
reader= csv.reader(csvr,delimiter='\t')
for i,row in enumerate(reader):
if i==0:
pass
elif '' in row[2:]:
pass
else:
liste.append(map(float,row[2:]))
a = np.array(liste)
h = .02
names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Decision Tree",
"Random Forest", "AdaBoost", "Naive Bayes", "LDA", "QDA"]
classifiers = [
KNeighborsClassifier(1),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
AdaBoostClassifier(),
GaussianNB(),
LDA(),
QDA()]
X = a[:,:3]
y = np.ravel(a[:,13])
linearly_separable = (X, y)
datasets =[linearly_separable]
figure = plt.figure(figsize=(27, 9))
i = 1
for ds in datasets:
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4)
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
i += 1
for name, clf in zip(names, classifiers):
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
print clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
print y.shape, X.shape
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
print Z
else:
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
alpha=0.6)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(name)
ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
size=15, horizontalalignment='right')
i += 1
figure.subplots_adjust(left=.02, right=.98)
plt.show()
在本例中,我使用了三个特征。我在代码中做错了什么,是 X_train 和 X_test 数据的问题吗?只有两个功能,一切都很好。
我的 X 值:
(array([[ 1., 1., 0.],
[ 1., 0., 0.],
[ 1., 0., 0.],
[ 1., 0., 0.],
[ 1., 1., 0.],
[ 1., 0., 0.],
[ 1., 0., 0.],
[ 3., 3., 0.],
[ 1., 1., 0.],
[ 1., 1., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 4., 4., 2.],
[ 0., 0., 0.],
[ 6., 3., 0.],
[ 5., 3., 2.],
[ 2., 2., 0.],
[ 4., 4., 2.],
[ 2., 1., 0.],
[ 2., 2., 0.]]), array([ 1., 1., 1., 1., 0., 1., 1., 0., 1., 1., 0., 1., 1.,
1., 1., 1., 0., 1., 1., 0., 1., 0., 1., 1.]))
第一个数组是 X 数组,第二个数组是 y(目标)数组。
我很抱歉错误的格式 = 错误:
Traceback (most recent call last):
File "allM.py", line 144, in <module>
mainplot(namePlot,1,2)
File "allM.py", line 117, in mainplot
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
File "/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/classification.py", line 191, in predict_proba
neigh_dist, neigh_ind = self.kneighbors(X)
File "/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/base.py", line 332, in kneighbors
return_distance=return_distance)
File "binary_tree.pxi", line 1298, in sklearn.neighbors.kd_tree.BinaryTree.query (sklearn/neighbors/kd_tree.c:10433)
ValueError: query data dimension must match training data dimension
这是没有将他放入数据集“ds”的X数组。
[[ 1. 1. 0.][ 1. 0. 0.][ 1. 0. 0.][ 1. 0. 0.][ 1. 1. 0.][ 1. 0. 0.][ 1. 0. 0.][ 3. 3. 0.][ 1. 1. 0.][ 1. 1. 0.][ 0. 0. 0.][ 0. 0. 0.][ 0. 0. 0.][ 0. 0. 0.][ 0. 0. 0.][ 0. 0. 0.][ 4. 4. 2.][ 0. 0. 0.][ 6. 3. 0.][ 5. 3. 2.][ 2. 2. 0.][ 4. 4. 2.][ 2. 1. 0.][ 2. 2. 0.]]
最佳答案
发生这种情况是因为 clf.predict_proba()
需要一个数组,其中每一行的元素数与训练数据中的行数相同——换句话说,输入的形状为 (num_rows, 3)
.
当您使用二维范例时,这是有效的,因为 np.c_[xx.ravel(), yy.ravel()]
的结果是一个包含两个元素行的数组:
print np.c_[xx.ravel(), yy.ravel()].shape
(45738, 2)
这些范例有两个元素,因为它们是由 np.meshgrid
创建的,示例代码使用它来创建一组输入以覆盖二维空间,这将很好地绘制。尝试将包含三项行的数组传递给 clf.predict_proba
,事情应该会正常进行。
如果您想重现这段特定的示例代码,您必须创建一个 3D 网格,如 this 中所述。关于SO的问题。您还将以 3D 形式绘制结果,其中 mplot3d将作为一个很好的起点,尽管基于我在示例代码中对绘图的(公认的简短)外观,我怀疑这可能比它的值(value)更麻烦。我真的不确定这些图的 3D 模拟看起来如何。
关于python - scikit-learn 查询数据维度必须匹配训练数据维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29948156/
real adaboost Logit boost discrete adaboost 和 gentle adaboost in train cascade parameter 有什么区别.. -bt
我想为 book crossing 构建训练数据矩阵和测试数据矩阵数据集。但作为 ISBN 代码的图书 ID 可能包含字符。因此,我无法应用此代码(来自 tutorial ): #Create two
我找到了 JavaANPR 库,我想对其进行自定义以读取我所在国家/地区的车牌。 似乎包含的字母表与我们使用的字母表不同 ( http://en.wikipedia.org/wiki/FE-Schri
我有一个信用卡数据集,其中 98% 的交易是非欺诈交易,2% 是欺诈交易。 我一直在尝试在训练和测试拆分之前对多数类别进行欠采样,并在测试集上获得非常好的召回率和精度。 当我仅在训练集上进行欠采样并在
我打算: 在数据集上从头开始训练 NASNet 只重新训练 NASNet 的最后一层(迁移学习) 并比较它们的相对性能。从文档中我看到: keras.applications.nasnet.NASNe
我正在训练用于分割的 uNet 模型。训练模型后,输出全为零,我不明白为什么。 我看到建议我应该使用特定的损失函数,所以我使用了 dice 损失函数。这是因为黑色区域 (0) 比白色区域 (1) 大得
我想为新角色训练我现有的 tesseract 模型。我已经尝试过 上的教程 https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesser
我的机器中有两个 NVidia GPU,但我没有使用它们。 我的机器上运行了三个神经网络训练。当我尝试运行第四个时,脚本出现以下错误: my_user@my_machine:~/my_project/
我想在python的tensorflow中使用稀疏张量进行训练。我找到了很多代码如何做到这一点,但没有一个有效。 这里有一个示例代码来说明我的意思,它会抛出一个错误: import numpy as
我正在训练一个 keras 模型,它的最后一层是单个 sigmoid单元: output = Dense(units=1, activation='sigmoid') 我正在用一些训练数据训练这个模型
所以我需要使用我自己的数据集重新训练 Tiny YOLO。我正在使用的模型可以在这里找到:keras-yolo3 . 我开始训练并遇到多个优化器错误,添加了错误代码以防止混淆。 我注意到即使它应该使用
将 BERT 模型中的标记化范式更改为其他东西是否有意义?也许只是一个简单的单词标记化或字符级标记化? 最佳答案 这是论文“CharacterBERT: Reconciling ELMo and BE
假设我有一个非常简单的神经网络,比如多层感知器。对于每一层,激活函数都是 sigmoid 并且网络是全连接的。 在 TensorFlow 中,这可能是这样定义的: sess = tf.Inte
有没有办法在 PyBrain 中保存和恢复经过训练的神经网络,这样我每次运行脚本时都不必重新训练它? 最佳答案 PyBrain 的神经网络可以使用 python 内置的 pickle/cPickle
我尝试使用 Keras 训练一个对手写数字进行分类的 CNN 模型,但训练的准确度很低(低于 10%)并且误差很大。我尝试了一个简单的神经网络,但没有效果。 这是我的代码。 import tensor
我在 Windows 7 64 位上使用 tesseract 3.0.1。我用一种新语言训练图书馆。 我的示例数据间隔非常好。当我为每个角色的盒子定义坐标时,盒子紧贴角色有多重要?我使用其中一个插件,
如何对由 dropout 产生的许多变薄层进行平均?在测试阶段要使用哪些权重?我真的很困惑这个。因为每个变薄的层都会学习一组不同的权重。那么反向传播是为每个细化网络单独完成的吗?这些细化网络之间的权重
我尝试训练超正方语言。我正在使用 Tess4J 进行 OCR 处理。我使用jTessBoxEditor和SerakTesseractTrainer进行训练操作。准备好训练数据后,我将其放在 Tesse
我正在构建一个 Keras 模型,将数据分类为 3000 个不同的类别,我的训练数据由大量样本组成,因此在用一种热编码对训练输出进行编码后,数据非常大(item_count * 3000 * 的大小)
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!