这是几行:
<小时/>
from sklearn import tree
X = [[181,80,44], [177, 70, 43], [160, 60, 38], 154, 54, 37],
[166,64,40], [190,90,47], [175,64,39],[177,70,40],[159,55,37],
[171,75,42],[181,85,43]
Y = ['male', 'female', 'female', 'female', 'male', 'male', 'male',
'female', 'male', 'female', 'male']
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)
prediction = clf.predict([[182,78,43]])
print (prediction)
<小时/>
结果:
Traceback (most recent call last):
File "C:\Python\code\test.py", line 14, in <module>
clf = clf.fit(X,Y)
File "C:\Python\lib\site-packages\sklearn\tree\tree.py", line 790, in fit
X_idx_sorted=X_idx_sorted)
File "C:\Python\lib\site-packages\sklearn\tree\tree.py", line 116, in fit
X = check_array(X, dtype=DTYPE, accept_sparse="csc")
File "C:\Python\lib\site-packages\sklearn\utils\validation.py", line 402,
in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.
[Finished in 0.5s]
<小时/>
预期结果:
应显示根据 body 测量预测的性别:"182,78,43"
示例:男性或女性
在 Sublime 上使用 sklearn、numpy+mkl 和 scipy 运行 Python 3.6。
代码来源于:https://www.youtube.com/watch?v=T5pRlIbr6gg 。整个 YouTube 评论区都没有对此做出回应。如果可以在这里找到答案,将不胜感激,找不到任何答案。
如果这确实是您的代码,那么问题就从一开始就开始了:
X = [[181,80,44], [177, 70, 43], [160, 60, 38], 154, 54, 37],
[166,64,40], [190,90,47], [175,64,39],[177,70,40],[159,55,37],
[171,75,42],[181,85,43]
正在创造一些距离可用还很远的东西:
print(X)
# ([[181, 80, 44], [177, 70, 43], [160, 60, 38], 154, 54, 37],)
因此,每个方向都需要添加一个括号(如果您不清楚:请阅读 sklearn 关于数据格式的文档:2d-array of shape (n_samples, n_features);还请考虑阅读一些 numpy 简介,其中单词形状来自 -> 内部一切都是基于 numpy 的):
X = [[181,80,44], [177, 70, 43], [160, 60, 38], [154, 54, 37], # before 154
[166,64,40], [190,90,47], [175,64,39],[177,70,40],[159,55,37],
[171,75,42],[181,85,43]] # at end
我必须承认:这是应该立即找到的东西,我无法理解为什么有人会花时间为 SO 创建帖子,而不花时间检查简单数组创建的语法。
公平地说:我认为它一开始不会经过语法检查(如上所述,这确实是一个奇怪的结构)。
编辑:公平地说#2:链接视频中的情况也确实很糟糕...我不知道对此有何看法(很好地使用 DTree 来完成此任务可能是已经疯了,甚至线性回归似乎更可行)!
是的,按照上述方法进行校正后,代码会预测男性
!
我是一名优秀的程序员,十分优秀!