在python中附加矩阵时出错 [英] Error in appending matrices in python

查看:82
本文介绍了在python中附加矩阵时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组用于6个不同星期的功能和标签,分别存储在变量FEATURES_DATATARGET中.

I have a set of features and labels for 6 different weeks stored in variable FEATURES_DATA and TARGET respectively.

我想做的是训练有关不断增长的功能和标签的决策树.因此,在数据的第一周进行培训,然后在第二周进行测试,然后在前两周进行培训,然后在第三周进行测试,依此类推...

What I want to do is to train a decision tree on growing features and labels. So, training on first week of data and testing on second week, then, training on first two weeks and testing on third week and so on...

要了解我的数据集:

print np.asarray(FEATURES_DATA).shape
print np.asarray(FEATURES_DATA[0][0]).shape
print ""
print FEATURES_DATA[0]

输出:

(6L, 1L)
(463511L, 40L)

[ 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)]

这是主要代码:

from sklearn import tree
from sklearn.tree import DecisionTreeClassifier

features = np.asarray(FEATURES_DATA)
labels = np.asarray(TARGET)
for i in xrange(5):
    Xtrain = np.concatenate(features[:i][0])
    print Xtrain.shape
    Ytrain = np.concatenate(labels[:i][0])
    Xtest = FEATURES_DATA[i+1][0]
    Ytest = TARGET[i+1][0]
    clf_DT = DecisionTreeClassifier(criterion='gini', splitter='best', max_depth=None, min_samples_split=5000)
    clf_DT.fit(Xtrain, Ytrain)

我在Xtrain串联行上收到以下错误:

I get the following error on Xtrain concatenation line:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-5d87466a6a03> in <module>()
      6 
      7 for i in xrange(5):
----> 8     Xtrain = np.concatenate(features[:i][0])
      9     print Xtrain.shape
     10     Ytrain = np.concatenate(labels[:i][0])

IndexError: index 0 is out of bounds for axis 0 with size 0

有帮助吗?谢谢

推荐答案

我已经解决了我的问题.初始化一个空矩阵将解决此问题.

I got the solution to my problem. Initializing an empty matrix will solve the problem.

Xtrain=np.empty(shape=[0, 40])
for i in xrange(5):
    Xtrain=np.concatenate((Xtrain,FEATURES_DATA[i][0]))
    print Xtrain.shape

给出输出

(463511L, 40L)
(955280L, 40L)
(1502984L, 40L)
(1969719L, 40L)
(2569141L, 40L)

这篇关于在python中附加矩阵时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆