CountVectorizer():AttributeError:“ numpy.float64”对象没有属性“ lower”

时间:2018-07-03 17:24:06

标签: countvectorizer

我正在尝试拟合一个具有event_type和notes(自由文本)列的数据集。在调用MultinomialNB模型之前,我处理了文本并将其转换为数组以向量化并在提供的代码下方计算tfidf:

将事件类型从字符串转换为整数以便于处理

ACLED['category_id'] = ACLED['event_type'].factorize()[0]
category_id_ACLED = ACLED[['event_type', 'category_id']].drop_d

uplicates().sort_values('category_id')
category_to_id = dict(category_id_ACLED.values)
id_to_category = dict(category_id_ACLED[['category_id', 'event_type']].values)

文本表示

我还将注释和category_id转换为功能和标签,如下所示:

tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
features = tfidf.fit_transform(ACLED.notes).toarray()
labels = ACLED.category_id
print(features.shape)

然后我使用功能部件和标签将数据集分为训练集和测试集:

X_train, X_test, y_train, y_test = train_test_split(features ,labels, random_state=0)
print('Original dataset shape {}'.format(Counter(y_train)))

输出

Original dataset shape Counter({1: 1280, 2: 819, 0: 676, 3: 593, 4: 138, 5: 53, 7: 50, 6: 21, 8: 10})

由于类不平衡,我使用SMOTE解决了少数派问题并创建了合成副本

应用随机过采样来克服不平衡的类

sm = SMOTE(random_state=42)
X_resampled, y_resampled = sm.fit_sample(X_train, y_train)
print('Resampled dataset shape {}'.format(Counter(y_resampled)))

过采样后的输出

Resampled dataset shape Counter({3: 1280, 1: 1280, 2: 1280, 0: 1280, 7: 1280, 6: 1280, 4: 1280, 5: 1280, 8: 1280})

到目前为止,一切工作正常,直到我尝试使用CountVectorizer()如下计算术语频率:

count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_resampled)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

输出错误:

'numpy.ndarray' object has no attribute 'lower'

我试图使用ravel()函数来使数组变平,但是错误仍然存​​在,任何想法,谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的方法,而不是直接使用数据集上的子集来使用特征和标签:

X_train, X_test, y_train, y_test = train_test_split(ACLED['notes'] ,ACLED['event_type'], random_state=0)

然后我将SMOTE移到了counVectorizer之后,因为SMOTE具有它自己的管道:

完善训练集的“注释”列

count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

应用随机过采样来克服不平衡的类

sm = SMOTE(random_state=42)
X_resampled, y_resampled = sm.fit_sample(X_train_tfidf, y_train)
print('Resampled dataset shape {}'.format(Counter(y_resampled)))

输出

Original dataset shape Counter({'Riots/Protests': 1280, 'Battle-No change of territory': 819, 'Remote violence': 676, 'Violence against civilians': 593, 'Strategic development': 138, 'Battle-Government regains territory': 53, 'Battle-Non-state actor overtakes territory': 50, 'Non-violent transfer of territory': 21, 'Headquarters or base established': 10})
Resampled dataset shape Counter({'Violence against civilians': 1280, 'Riots/Protests': 1280, 'Battle-No change of territory': 1280, 'Remote violence': 1280, 'Battle-Non-state actor overtakes territory': 1280, 'Non-violent transfer of territory': 1280, 'Strategic development': 1280, 'Battle-Government regains territory': 1280, 'Headquarters or base established': 1280})