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

阅读 12.2k
1 个回答
✓ 已被采纳新手上路,请多包涵

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题