Поскольку векторизатор tf-idf будет просто аварийно завершать работу всякий раз, когда он встречает новые метки, я пытаюсь удалить новые метки из моего нового ввода. Как я могу обновить значение столбца данных? Я делаю:
def clean_unseen(dfcol, vectorizer):
cleanedstring = ""
for entry in dfcol:
for word in entry.split():
if word in vectorizer.vocabulary_:
cleanedstring = cleanedstring + " " + word
print(cleanedstring)
entry = cleanedstring
cleanedstring = ""
return dfcol
Пример:
tfifgbdf_vect= TfidfVectorizer()
s2 = pd.Series(['the cat', 'awesome xyz', 'f_g_h lol asd'])
tfifgbdf_vect.fit_transform(s2)
s3 = pd.Series(['the dog the awesome xyz', 'xyz lol asd', 'f_g_h lol aha'])
clean_unseen(s3, tfifgbdf_vect)
Это, однако, вернет исходный столбец без изменений:
Output:
0 the dog the awesome xyz
1 xyz lol asd
2 f_g_h lol aha
dtype: object