[英]Getting error "AttributeError: 'numpy.ndarray' object has no attribute 'lower' " in word tokenizer
I am trying to train a model to classify multi-label data set by referring this article.我正在尝试通过参考这篇文章来训练一个模型来对多标签数据集进行分类。 I am entirely new to this field and I am getting this error "AttributeError: 'numpy.ndarray' object has no attribute 'lower'"
我对这个领域完全陌生,我收到了这个错误“AttributeError: 'numpy.ndarray' object has no attribute 'lower'”
Here is my code这是我的代码
reviews = pd.read_csv("/content/drive/My Drive/Interim Project Data/score.csv")
Review_labels = reviews[["natral score", "man-made score", "sport score", "special event score"]]
Review_labels.head()
def preprocess_text(sen):
# Remove punctuations and numbers
sentence = re.sub('[^a-zA-Z]', ' ', sen)
sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)
sentence = re.sub(r'\s+', ' ', sentence)
return sentence
X = []
sentences = list(reviews["User Review"])
for sen in sentences:
X.append(preprocess_text(sen))
y = Review_labels.values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=123)
print(len(X_train))
print(len(X_test))
from numpy import array
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(X)
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
The error is here错误在这里
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-128-ff374d8c5eb4> in <module>()
7 tokenizer.fit_on_texts(X)
8
----> 9 X_train = tokenizer.texts_to_sequences(X_train)
10 X_test = tokenizer.texts_to_sequences(X_test)
11
2 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/text.py in text_to_word_sequence(text, filters, lower, split)
41 """
42 if lower:
---> 43 text = text.lower()
44
45 if sys.version_info < (3,):
AttributeError: 'numpy.ndarray' object has no attribute 'lower'
here is the data set that I used in the code 这是我在代码中使用的数据集
It will be really helpful if someone can help with this issue.如果有人可以帮助解决这个问题,那将非常有帮助。
Is text a str variable? text是 str 变量吗? If it's not maybe you could do
如果不是,也许你可以做
text = str(text).lower()
as long as it's value is something that can be turned into a string只要它的值是可以变成字符串的东西
How about trying试试怎么样
tokenizer = Tokenizer(num_words=5000, lower = False) tokenizer = Tokenizer(num_words=5000,lower = False)
instead of代替
tokenizer = Tokenizer(num_words=5000) tokenizer = Tokenizer(num_words=5000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.