我试着用tfidf来准备我的数据,但是我也有同样的错误。
X = df['Description'], df['Type']
y =df['Description'], df['Type']
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.33, random_state=42)
df['Description']=[" ".join(Description) for Description in df['Description'].values]
tfidf = TfidfVectorizer(stop_words='english')
t_x_train = tfidf.fit_transform(X_train)
t_x_test = tfidf.transform(y_test)当我运行它时,会发生AttributeError: 'Series' object has no attribute 'lower'
发布于 2022-03-30 20:51:55
Sklearn尝试将str.lower()应用于y_test中的元素。但是,数据类型似乎不兼容。
请核对:
使用tfidf或转换为字符串的数据类型,如下面所示,在传递给tfidf时,y_test是否应该替换为X_test
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
corpus = [
('This is the first document.',4),
('This document is the second document.',3),
('And this is the third one.',2),
('Is this the first document?',1)
]
df= pd.DataFrame(corpus, columns = ['Description', 'Type'])
X = df['Description']
# make sure your target is also a series of strings if not already
y = df['Type'].astype('str')
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.33, random_state=42)
# df['Description']=[" ".join(Description) for Description in df['Description'].values]
tfidf = TfidfVectorizer(stop_words='english')
t_x_train = tfidf.fit_transform(X_train)
t_x_test = tfidf.transform(y_test)https://stackoverflow.com/questions/71678256
复制相似问题