“将1列替换为...". numpy中的错误 [英] "Got 1 columns instead of ..." error in numpy

查看:325
本文介绍了“将1列替换为...". numpy中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究以下代码,以对训练集和测试集执行随机森林分类;

I'm working on the following code for performing Random Forest Classification on train and test sets;

from sklearn.ensemble import RandomForestClassifier
from numpy import genfromtxt, savetxt

def main():
    dataset = genfromtxt(open('filepath','r'), delimiter=' ', dtype='f8')   
    target = [x[0] for x in dataset]
    train = [x[1:] for x in dataset]
    test = genfromtxt(open('filepath','r'), delimiter=' ', dtype='f8')

    rf = RandomForestClassifier(n_estimators=100)
    rf.fit(train, target)
    predicted_probs = [[index + 1, x[1]] for index, x in enumerate(rf.predict_proba(test))]

    savetxt('filepath', predicted_probs, delimiter=',', fmt='%d,%f', 
            header='Id,PredictedProbability', comments = '')

if __name__=="__main__":
    main()

但是我在执行时遇到以下错误;

However I get the following error on execution;

---->      dataset = genfromtxt(open('C:/Users/Saurabh/Desktop/pgm/Cora/a_train.csv','r'), delimiter='', dtype='f8')

ValueError: Some errors were detected !
    Line #88 (got 1435 columns instead of 1434)
    Line #93 (got 1435 columns instead of 1434)
    Line #164 (got 1435 columns instead of 1434)
    Line #169 (got 1435 columns instead of 1434)
    Line #524 (got 1435 columns instead of 1434)
...
...
...

关于如何避免它的任何建议?谢谢.

Any suggestions as to how avoid it?? Thanks.

推荐答案

genfromtxt如果列数不相等,则会出现此错误.

genfromtxt will give this error if the number of columns is unequal.

我可以想到3种解决方法:

I can think of 3 ways around it:

1.使用 usecols 参数

np.genfromtxt('yourfile.txt',delimiter=',',usecols=np.arange(0,1434))

但是-这可能意味着您丢失了一些数据(行长于1434列)-不管这是否重要都取决于您.

However - this may mean that you lose some data (where rows are longer than 1434 columns) - whether or not that matters is down to you.

2.调整输入数据文件,使其具有相等的列数.

3.使用除genfromtxt:

3. Use something other than genfromtxt:

.............

.............like this

这篇关于“将1列替换为...". numpy中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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