I have built a model to see it's predictions on unseen data and this is the ending chunk. I would like to convert the results from the print function into a dataframe. How can I achieve this?
text_features = tfidf.transform(texts)
predictions = model.predict(text_features)
for text, predicted in zip(texts, predictions):
print('"{}"'.format(text))
print(" - Predicted as: '{}'".format(id_to_category[predicted]))
print("")
This is the output
"Accessibility by phone !!!"
- Predicted as: 'Communication'
"very nice"
- Predicted as: 'Irrelevant - other'
"NO"
- Predicted as: 'Irrelevant - other'
"nothing"
- Predicted as: 'Irrelevant - other'
"RAS"
- Predicted as: 'Irrelevant - other'
"pbm at the share level"
- Predicted as: 'Irrelevant - other'
"no, not that I know"
- Predicted as: 'Irrelevant - other'
I would like to convert this output into a dataframe.
An example would be
Text Predicted as
Accessability by phone Communication
No Irrelevant other
Try this,You can use append method in pandas .
text_features = tfidf.transform(texts)
predictions = model.predict(text_features)
df = pd.DataFrame(columns=["Text","Predicted as"])
for text, predicted in zip(texts, predictions):
df = df.append({"Text":text,"Predicted as":id_to_category[predicted]})
df = pd.DataFrame(columns=['Text', 'PredictedAs'])
text_features = tfidf.transform(texts)
predictions = model.predict(text_features)
for ind, (text, predicted) in enumerate(zip(texts, predictions)):
print('"{}"'.format(text))
print(" - Predicted as: '{}'".format(id_to_category[predicted]))
print("")
df.loc[ind, 'Text'] = text
df.loc[ind, 'PredictedAs'] = id_to_category[predicted]
import pandas as pd
texts = [
"Accessibility by phone !!!",
"very nice",
"NO"
]
predictions = [0, 1, 1]
id_to_category = {0: "Communication", 1: "Irrelevant - other"}
predictions_as_text = [id_to_category[i] for i in predictions]
df = pd.DataFrame({"text": texts, "prediction": predictions_as_text})
I mocked some of the data, all you need is the first line and the last 2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.