如何解决Python文本处理:attributeError:' list'对象没有属性。

发布于 2025-02-01 19:00:56 字数 2882 浏览 4 评论 0原文

我是Python的新手,可以stackoverflow和Gui Python(请保持温柔),并试图批准如何进行KNN分析。我正在使用我自己构建的代码组合的组合:Python -AttributeError:

doc = doc.lower()
AttributeError: 'list' object has no attribute 'lower'

这是我的代码:

selct = StringVar()
categorychoosen = ttk.Combobox(top, width = 27, textvariable = selct)
categorychoosen['values'] = (' Computer Science', 
                          ' computer engineering',
                          ' Information Technology',
                          ' artificial intelligence',
                          ' cyber security',
                          ' computer networks',
                          ' Information Security',
                          ' Management Information Systems',
                          ' Software engineering',
                          ' data analysis',
                          ' Data Science')
  
categorychoosen.grid(row=1, column=2)
categorychoosen.current()

s = StringVar()
choosen = ttk.Combobox(top, width = 27, textvariable = s)
choosen['values'] = (' Computer Science', 
                          ' computer engineering',
                          ' Information Technology',
                          ' artificial intelligence',
                          ' cyber security',
                          ' computer networks',
                          ' Information Security',
                          ' Management Information Systems',
                          ' Software engineering',
                          ' data analysis',
                          ' Data Science')
  
choosen.grid(row=1, column=3)
choosen.current()

def model():
    
    from sklearn.model_selection import train_test_split
    from sklearn.feature_extraction.text import TfidfVectorizer
    from scipy.sparse import hstack
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.neighbors import KNeighborsClassifier

    resume = pd.read_csv(r'/Users/asma/Desktop/UpdatedResumeDataSet.csv')

    #DATA
    x = resume['Resume'].values
    y = resume['Category'].values
    v = [[selct.get(),s.get()]]

    #transform
    word = TfidfVectorizer(sublinear_tf=True, stop_words='english')
    word.fit(x)
    wordFeatures = word.transform(x)
    
    w = TfidfVectorizer(sublinear_tf=True, stop_words='english')
    w.fit(v)
    wx = word.transform(v)

    # to 2D Array
    wx.reshape(-1, 1)
    wordFeatures.reshape(-1, 1)
    x.reshape(-1, 1)

    #KNN 
    model = KNeighborsClassifier(n_neighbors=5, metric= 'euclidean')
    model.fit(wordFeatures,y)
    x_test = wx
    y_pred = model.predict([x_test])
    jobR = Label(top,text=str([y_pred]) ,bg='light gray').grid(row=4,column=2)

but= Button(top,text="Start",bg='gray', command=model).grid(row=3,column=0)

在“转换过程”之前或之后,我可以在哪里添加“较低”,我将使用什么数据? 简历['简历']。值[[SELCT.GEG(),s.get()]]

任何帮助将不胜感激。

I am new to Python to Stackoverflow and GUI Python (please be gentle) and am trying to aplaing how to do a KNN analysis. I am using a combination of codes I built it myself: Python - AttributeError:

doc = doc.lower()
AttributeError: 'list' object has no attribute 'lower'

This is my code:

selct = StringVar()
categorychoosen = ttk.Combobox(top, width = 27, textvariable = selct)
categorychoosen['values'] = (' Computer Science', 
                          ' computer engineering',
                          ' Information Technology',
                          ' artificial intelligence',
                          ' cyber security',
                          ' computer networks',
                          ' Information Security',
                          ' Management Information Systems',
                          ' Software engineering',
                          ' data analysis',
                          ' Data Science')
  
categorychoosen.grid(row=1, column=2)
categorychoosen.current()

s = StringVar()
choosen = ttk.Combobox(top, width = 27, textvariable = s)
choosen['values'] = (' Computer Science', 
                          ' computer engineering',
                          ' Information Technology',
                          ' artificial intelligence',
                          ' cyber security',
                          ' computer networks',
                          ' Information Security',
                          ' Management Information Systems',
                          ' Software engineering',
                          ' data analysis',
                          ' Data Science')
  
choosen.grid(row=1, column=3)
choosen.current()

def model():
    
    from sklearn.model_selection import train_test_split
    from sklearn.feature_extraction.text import TfidfVectorizer
    from scipy.sparse import hstack
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.neighbors import KNeighborsClassifier

    resume = pd.read_csv(r'/Users/asma/Desktop/UpdatedResumeDataSet.csv')

    #DATA
    x = resume['Resume'].values
    y = resume['Category'].values
    v = [[selct.get(),s.get()]]

    #transform
    word = TfidfVectorizer(sublinear_tf=True, stop_words='english')
    word.fit(x)
    wordFeatures = word.transform(x)
    
    w = TfidfVectorizer(sublinear_tf=True, stop_words='english')
    w.fit(v)
    wx = word.transform(v)

    # to 2D Array
    wx.reshape(-1, 1)
    wordFeatures.reshape(-1, 1)
    x.reshape(-1, 1)

    #KNN 
    model = KNeighborsClassifier(n_neighbors=5, metric= 'euclidean')
    model.fit(wordFeatures,y)
    x_test = wx
    y_pred = model.predict([x_test])
    jobR = Label(top,text=str([y_pred]) ,bg='light gray').grid(row=4,column=2)

but= Button(top,text="Start",bg='gray', command=model).grid(row=3,column=0)

Where can I add the 'lower' before or after 'the transform process', and what data will I use for it? resume['Resume'].values or [[selct.get(),s.get()]].

Any help would be massively appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2025-02-08 19:00:56

doclist具有元素的对象。现在,您开始调用其lower()方法,但是它没有这样的方法,而列表的项目可能具有solower方法。

list是包含项目的数据结构,并且不应与其项目混淆。

lower()方法是String的已知方法,这使您的项目在列表中很可能是字符串对象。

您可以使用map()将列表的字符串项转换为Python中的较低情况,请参见更多信息: https://www.delftstack.com/howto/python/python/python/python-lowercase-list/

doc is a list object, which has elements. Now, you start to call its lower() method, but it has no such method, while the list's items may be having a lower method.

The list is a data structure that contains items and it is not to be confounded with its items.

The lower() method is a known method of String, which makes it highly probable that your items in the list are string objects.

You can use map() to convert String items of a list to lower case in Python, see more here: https://www.delftstack.com/howto/python/python-lowercase-list/

冷清清 2025-02-08 19:00:56

请添加定义“ DOC”的行。

据我所知,从错误消息中,您正在尝试将字符串方法应用于列表。我认为您想将.lower()方法应用于DOC中收集的字符串。

因此,请尝试此内线列表理解,以将.lower()方法应用于字符串元素,并同时创建带有新元素的新列表:

doc = [d.lower(d.lower for doc in doc]

希望,这会有所帮助,否则请给我们更多详细信息。

Please add the line where you define "doc".

As far as I can tell from the error message, you are trying to apply a String method to a List. I assume you want to apply the .lower() method to Strings that are gathered in doc.

So try this inline list comprehension to apply the .lower() methods to the String elements and create a new list with the new elements at the same time:

doc = [d.lower() for d in doc]

Hope, this helps, otherwise please give us more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文