簡體   English   中英

如何解決“AttributeError:'numpy.ndarray'對象沒有屬性'lower'”?

[英]How solved "AttributeError: 'numpy.ndarray' object has no attribute 'lower'"?

我有一個二維數組,每個元素中都有字符串。 我正在嘗試使用使用sklearn.feature_extraction.texttransform()將文本數據轉換為 numeric 。 但是,我收到一條錯誤消息:

Traceback (most recent call last):File "/Users/asma/Desktop/q.py", line 13, in <module>
        Fit = stand.fit(Lower)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/feature_extraction/text.py", line 2053, in fit
        X = super().fit_transform(raw_documents)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/feature_extraction/text.py", line 1330, in fit_transform
        vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary_)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/feature_extraction/text.py", line 1201, in _count_vocab
        for feature in analyze(doc):
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/feature_extraction/text.py", line 113, in _analyze
        doc = preprocessor(doc)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/feature_extraction/text.py", line 71, in _preprocess
        doc = doc.lower()
    AttributeError: 'numpy.ndarray' object has no attribute 'lower'

這是編碼

import pandas as pd
import numpy as np
import tkinter as tk
from tkinter import ttk
from sklearn.feature_extraction.text import TfidfVectorizer

sting_array = ["hEllO","iNteRneT","pEopLe","sKay","sds","gbrhn","rHy"]
Array2D = np.reshape(sting_array, [-1, 1])
Lower = np.char.lower(Array2D)

stand = TfidfVectorizer()
 #fit data
Fit = stand.fit(Lower)
 #transform data
x_scaled =stand.transform(Lower)

print(x_scaled)

雖然我使用的是Lower = np.char.lower (Array2D) ,但我得到了同樣的錯誤!! 任何幫助將不勝感激。

我認為您需要刪除小寫的代碼。 因為stand.fit會自動執行此操作。

...
sting_array = ["hEllO","iNteRneT","pEopLe","sKay","sds","gbrhn","rHy"]

stand = TfidfVectorizer()
 #fit data
Fit = stand.fit(sting_array)
 #transform data
x_scaled =stand.transform(sting_array)

print(x_scaled)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM