2

我想我在下面的代码中遗漏了一些东西。

from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE


# Split into training and test sets

# Testing Count Vectorizer

X = df[['Spam']]
y = df['Value']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=40)
X_resample, y_resampled = SMOTE().fit_resample(X_train, y_train)


sm =  pd.concat([X_resampled, y_resampled], axis=1)

当我收到错误消息时

ValueError:无法将字符串转换为浮点数:---> 19 X_resampled, y_resampled = SMOTE().fit_resample(X_train, y_train)

数据示例是

Spam                                             Value
Your microsoft account was compromised             1
Manchester United lost against PSG                 0
I like cooking                                     0

我会考虑同时转换训练集和测试集以解决导致错误的问题,但我不知道如何同时应用于两者。我在谷歌上试过一些例子,但它并没有解决这个问题。

4

1 回答 1

3

在应用 SMOTE 之前将文本数据转换为数字,如下所示。

from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer()
vectorizer.fit(X_train.values.ravel())
X_train=vectorizer.transform(X_train.values.ravel())
X_test=vectorizer.transform(X_test.values.ravel())
X_train=X_train.toarray()
X_test=X_test.toarray()

然后添加您的 SMOTE 代码

x_train = pd.DataFrame(X_train)
X_resample, y_resampled = SMOTE().fit_resample(X_train, y_train)
于 2020-12-13T22:01:34.267 回答