float() argument must be a string or a number?

发布于 2022-09-06 00:20:41 字数 2238 浏览 12 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2022-09-13 00:20:41

问题已经解决:是score函数自带function参数,不需要在定义。谢谢各位!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文