对csv文件进行预测,一次一行

时间:2017-12-08 12:01:47

标签: python csv scikit-learn joblib

我有一个大型的csv文件,我需要一次一个地获取一行数据,然后根据模型对其进行评分。我已经尝试了下面的代码但得到的错误是{ {1}}。我可以针对整个"X has 120839 features per sample; expecting 30"运行模型,并对每一行进行预测。但我需要一次做一行,谢谢。

dataset

在这种情况下,它似乎不会分割行,因为每行后面都有loaded_model = joblib.load('LR_model.sav') with open(r'fordTestA.csv', "r") as f: for line in f: line = f.readlines()[1:] ##minus headers result = loaded_model.predict(line) 。我试着添加

\n

这会出错:line = line.rstrip('\n') 。提前感谢您的任何反馈。

1 个答案:

答案 0 :(得分:3)

我不熟悉joblibpredict(),但是:

import csv

# other code

with open(r'fordTestA.csv', 'r', newline='') as f:
    rows = csv.reader(f, delimiter=',')
    _ = next(rows) # skip headers
    for row in rows:
        line = list(map(float, row)) # convert row of str to row of float
        results = loaded_model.predict(line)
        # or if you need a ',' delimited string
        line = ','.join(row)
        results = loaded_model.predict(row)