float() argument must be a string or a number?
data
X_train =
[[-2.7182653 -0.78567116 0.34642591 ..., -0.79730493 -0.34507018
-0.75480233]
[-0.22201943 -0.17514106 -0.3787789 ..., 0.1838979 0.46846147
0.41508417]
...,
[ 1.30514658 -0.03829811 0.75191677 ..., -0.51414598 -0.14389793
-0.23173999]]
y_train =
[[1]
[0]
...
[1]]
code
n_splits = 3
kf = KFold(n_splits=n_splits, shuffle=False, random_state=None)
kf_clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=None, max_leaf_nodes=None, random_state=0)
val_scores = np.zeros((), dtype=np.float64)
for train_index, test_index in kf.split(X_train, y_train):
# print("TRAIN:", train_index, "TEST:", test_index)
# X_train, X_test = X_train[train_index], X_train[test_index]
# y_train, y_test = y_train[train_index], y_train[test_index]
# print(X_train, X_test, y_train, y_test)
kf_clf.fit(X_train[train_index], y_train[train_index])
# X_train[test_index] = str(X_train[test_index])
# y_train[test_index] = str(y_train[test_index])
val_scores += kf_clf.score(kf_clf, X_train[test_index], y_train[test_index])
val_scores /= n_splits
print val_scores
Error
Traceback (most recent call last):
File "/home/ly/software/pycharm/myproject/first/first/Decision_Tree.py", line 242, in <module>
val_scores += kf_clf.score(kf_clf, X_train[test_index], y_train[test_index])
File "/usr/local/lib/python2.7/dist-packages/sklearn/base.py", line 350, in score
return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
File "/usr/local/lib/python2.7/dist-packages/sklearn/tree/tree.py", line 412, in predict
X = self._validate_X_predict(X, check_input)
File "/usr/local/lib/python2.7/dist-packages/sklearn/tree/tree.py", line 373, in _validate_X_predict
X = check_array(X, dtype=DTYPE, accept_sparse="csr")
File "/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py", line 402, in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
TypeError: float() argument must be a string or a number
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题已经解决:是score函数自带function参数,不需要在定义。谢谢各位!