【发布时间】:2021-02-10 03:10:25
【问题描述】:
我无法将 CountVectorizer 应用于 Excel 导入的数据集。我尝试将数据中的所有整数换成一个字符串,但 CountVectorizer 仍然注册整数。
import numpy as np
import sklearn
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer as cv
from sklearn.linear_model import Perceptron
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
pos = pd.read_excel("/content/drive/My Drive/Polarity_pos.xlsx", header = None, names=None)
neg = pos = pd.read_excel("/content/drive/My Drive/Polarity_neg.xlsx", header = None, names=None)
merged_train = pd.merge(pos,neg)
string = merged_train.astype('str')
train=pd.DataFrame(data=string).replace('\d+','NUM',regex=True)
print(train.loc[19,:])
#analyzer='word',stop_words=None,analyzer = 'word'
vectorizer = cv()
count_vector = vectorizer.fit_transform(train)
出现错误:
AttributeError Traceback (most recent call last)
<ipython-input-116-adcd263d8e89> in <module>()
26 #analyzer='word',stop_words=None,analyzer = 'word'
27 vectorizer = cv()
---> 28 count_vector = vectorizer.fit_transform(train)
29
30
3 frames
/usr/local/lib/python3.6/dist-packages/sklearn/feature_extraction/text.py in _preprocess(doc, accent_function, lower)
66 """
67 if lower:
---> 68 doc = doc.lower()
69 if accent_function is not None:
70 doc = accent_function(doc)
AttributeError: 'int' object has no attribute 'lower'
【问题讨论】:
标签: python pandas scikit-learn countvectorizer