Prediction done on tf-idf array, how to merge with original data frame

48 views Asked by At

I have text data, pandas data frame, which, i converted in to tf-idf vectors than using ML algorithm prediction done.so, how to merge results of xtest on indexes with xtest pandas data frame. I have performed train_test_split after on word embedding, not on pandas data frame. when i check:

array.index()
Numpy array has no attribute index

Shape of array is (3480,5000),when i try below code:

len(array.indices)
10480
1

There are 1 answers

2
Anna Andreeva Rogotulka On

assuming that pred is your predictions

it's numpy array, then you can df.merge preds as Series to main df

import pandas as pd
import numpy as np

data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}
df = pd.DataFrame(data, index=[1, 2, 3, 4])


pred = np.array([0.1, 0.9, 0.5, 0.11])

series = pd.Series(pred, index=df.index, name='C')
result = df.merge(series, left_index=True, right_index=True)

print(result)