ValueError: setting an array element with a sequence with Decision Tree where all the rows have equal elements?
ValueError: setting an array element with a sequence with Decision Tree where all the rows have equal elements?
我正在尝试将决策树拟合到特征和标签矩阵。这是我的代码:
print FEATURES_DATA[0]
print ""
print TARGET[0]
print ""
print np.unique(list(map(len, FEATURES_DATA[0])))
给出以下输出:
[ array([[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
...,
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6]], dtype=uint8)]
[ array([[31],
[31],
[31],
...,
[22],
[22],
[22]], dtype=uint8)]
[463511]
矩阵实际包含463511个样本。
此后,我运行以下块:
from sklearn.tree import DecisionTreeClassifier
for i in xrange(5):
Xtrain=FEATURES_DATA[i]
Ytrain=TARGET[i]
clf=DecisionTreeClassifier()
clf.fit(Xtrain,Ytrain)
这给了我以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-3d8b2a7a3e5f> in <module>()
4 Ytrain=TARGET[i]
5 clf=DecisionTreeClassifier()
----> 6 clf.fit(Xtrain,Ytrain)
C:\Users\singhg2\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\tree\tree.pyc in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
152 random_state = check_random_state(self.random_state)
153 if check_input:
--> 154 X = check_array(X, dtype=DTYPE, accept_sparse="csc")
155 if issparse(X):
156 X.sort_indices()
C:\Users\singhg2\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
371 force_all_finite)
372 else:
--> 373 array = np.array(array, dtype=dtype, order=order, copy=copy)
374
375 if ensure_2d:
ValueError: setting an array element with a sequence.
我搜索了 SO 上的其他帖子,发现大多数答案都是矩阵不完全是数字,或者数组在样本之间的长度不同。但是,我的问题不是这样吗?
有什么帮助吗?
如果print FEATURES_DATA[0]
实际上打印
[ array([[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
...,
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6]], dtype=uint8)]
那么问题是 FEATURES_DATA[0] 是一个 python 列表,里面有一个 numpy 数组。 (从[
和]
可以理解)
您可以select列表的第一个(也是唯一一个)元素来修复它
from sklearn.tree import DecisionTreeClassifier
for i in xrange(5):
Xtrain=FEATURES_DATA[i][0]
Ytrain=TARGET[i][0]
clf=DecisionTreeClassifier()
clf.fit(Xtrain,Ytrain)
我正在尝试将决策树拟合到特征和标签矩阵。这是我的代码:
print FEATURES_DATA[0]
print ""
print TARGET[0]
print ""
print np.unique(list(map(len, FEATURES_DATA[0])))
给出以下输出:
[ array([[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
...,
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6]], dtype=uint8)]
[ array([[31],
[31],
[31],
...,
[22],
[22],
[22]], dtype=uint8)]
[463511]
矩阵实际包含463511个样本。
此后,我运行以下块:
from sklearn.tree import DecisionTreeClassifier
for i in xrange(5):
Xtrain=FEATURES_DATA[i]
Ytrain=TARGET[i]
clf=DecisionTreeClassifier()
clf.fit(Xtrain,Ytrain)
这给了我以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-3d8b2a7a3e5f> in <module>()
4 Ytrain=TARGET[i]
5 clf=DecisionTreeClassifier()
----> 6 clf.fit(Xtrain,Ytrain)
C:\Users\singhg2\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\tree\tree.pyc in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
152 random_state = check_random_state(self.random_state)
153 if check_input:
--> 154 X = check_array(X, dtype=DTYPE, accept_sparse="csc")
155 if issparse(X):
156 X.sort_indices()
C:\Users\singhg2\AppData\Local\Enthought\Canopy\User\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
371 force_all_finite)
372 else:
--> 373 array = np.array(array, dtype=dtype, order=order, copy=copy)
374
375 if ensure_2d:
ValueError: setting an array element with a sequence.
我搜索了 SO 上的其他帖子,发现大多数答案都是矩阵不完全是数字,或者数组在样本之间的长度不同。但是,我的问题不是这样吗?
有什么帮助吗?
如果print FEATURES_DATA[0]
实际上打印
[ array([[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
[3, 3, 3, ..., 7, 7, 7],
...,
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6],
[2, 2, 2, ..., 6, 6, 6]], dtype=uint8)]
那么问题是 FEATURES_DATA[0] 是一个 python 列表,里面有一个 numpy 数组。 (从[
和]
可以理解)
您可以select列表的第一个(也是唯一一个)元素来修复它
from sklearn.tree import DecisionTreeClassifier
for i in xrange(5):
Xtrain=FEATURES_DATA[i][0]
Ytrain=TARGET[i][0]
clf=DecisionTreeClassifier()
clf.fit(Xtrain,Ytrain)