简体   繁体   English

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

[英]Make a prediction on csv file, one line at a time

I have a large csv file that i need to take a row of data, one at a time, and score it against a model. 我有一个很大的csv文件,我需要一次获取一行数据,并根据模型对其进行评分。 I have tried the code below but get an error of "X has 120839 features per sample; expecting 30" . 我已经尝试过下面的代码,但收到错误消息"X has 120839 features per sample; expecting 30" I can run the model against the entire dataset and it makes predictions on each row. 我可以对整个dataset运行模型,并在每一行进行预测。 But i need to do it one line at a time, thank you. 但是我需要一次做一行,谢谢。

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)

In this scenario, it doesnt seem to split the lines as there is \\n after each row. 在这种情况下,似乎没有拆分行,因为每行后面都有\\n I tried to add 我试图添加

line = line.rstrip('\n')

This gives an error : " 'list' object has no attribute 'rstrip'" . 这给出了一个错误: " 'list' object has no attribute 'rstrip'" Thanks in advance for any feedback. 预先感谢您的任何反馈。

I'm not familiar with joblib or predict() , but: 我不熟悉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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM