使用numpy - header参数无效

时间:2014-09-05 20:15:42

标签: python numpy

我在基本的机器学习教程中使用numpy,而使用PyDev的Eclipse会抛出以下错误:

header='MoleculeId,PredictedProbability', comments = '')
TypeError: savetxt() got an unexpected keyword argument 'header'

我查找了numpy.savetxt函数,并且标题包含在结构中,所以我不确定错误在哪里。仅供参考,如果我完全删除标题部分,文件将根据需要运行,但我确实需要它来标记我的CSV列。

这是我正在使用的代码。

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

def main():
    #create the training & test sets, skipping the header row with [1:]
    dataset = genfromtxt(open('Data/train.csv','r'), delimiter=',', dtype='f8')[1:]    
    target = [x[0] for x in dataset]
    train = [x[1:] for x in dataset]
    test = genfromtxt(open('Data/test.csv','r'), delimiter=',', dtype='f8')[1:]

    #create and train the random forest
    #multi-core CPUs can use: rf = RandomForestClassifier(n_estimators=100, n_jobs=2)
    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('Data/submission.csv', predicted_probs, delimiter=',', fmt='%d,%f', 
            header='MoleculeId,PredictedProbability', comments = '')

if __name__=="__main__":
    main()

0 个答案:

没有答案