Fasttext как загрузить столбец .csv в model.predict - PullRequest
0 голосов
/ 24 сентября 2019

Я новичок в Python и NLP.

Я следовал этому руководству (https://fasttext.cc/docs/en/supervised-tutorial.html), чтобы обучить мою контролируемую модель fasttxt на Python.

У меня есть CSV с текстомстолбец, и я хотел бы предсказать метки, чтобы когда-либо строки из файла. Мой вопрос заключается в том, как я могу загрузить (преобразовать) столбец CSV в предиктивный ввод и сохранить метку.

model.predict("Which baking dish is best to bake a banana bread ?", k=-1, threshold=0.5)

вместо этого (текст в "" Какие выпечки .... ") Я хотел бы загрузить строку за строкой и сохранить метку предпочтительно в новом столбце в том же CSV.

Я бы хотел любую помощь или, возможно, учебник, который яможет последовать.

До сих пор я пытался преобразовать столбец в в список с пандами и массивом numpy, но оба возвращались с "AttributeError: у объекта 'function' нет атрибута 'find'"

1 Ответ

0 голосов
/ 25 сентября 2019

Взять этот CSV как пример:

index;id;text;author 0;id26305;This process, however, afforded me no means of...;EAP 1;id17569;It never once occurred to me that the fumbling...;HPL 2;id11008;In his left hand was a gold snuff box, from wh...;EAP 3;id27763;How lovely is spring As we looked from Windsor...;MWS 4;id12958;Finding nothing else, not even gold, the Super...;HPL 5;id22965;A youth passed in solitude, my best years spen...;MWS 6;id09674;The astronomer, perhaps, at this point, took r...;EAP 7;id13515;The surcingle hung in ribands from my body. ;EAP 8;id19322;I knew that you could not say to yourself 'ste...;EAP 9;id00912;I confess that neither the structure of langua...;MWS

Вы можете использовать следующий код:

import pandas as pd
import fastText as ft

# here you load the csv into pandas dataframe
df=pd.read_csv('csv_file.csv',sep=';')

# here you load your fasttext module
model=ft.load_model(MODELPATH)

# line by line, you make the predictions and store them in a list
predictions=[]
for line in df['text']:
    pred_label=model.predict(line, k=-1, threshold=0.5)[0][0]
    predictions.append(pred_label)

# you add the list to the dataframe, then save the datframe to new csv
df['prediction']=predictions
df.to_csv('csv_file_w_pred.csv',sep=';',index=False)
...