如何解决“ ValueRor:Y应该是1D数组,相反。

发布于 2025-01-31 09:23:59 字数 3221 浏览 0 评论 0原文

from tkinter import *
from tkinter import ttk
from tkmacosx import Button
top = Tk()
top.title("Jobs")
top.geometry("1000x800")

line1 = LabelFrame(top, text='')
line1.pack(expand = 'yes', fill = 'both')

n = StringVar()

categorychoosen = ttk.Combobox(line1, width = 27, textvariable = n)


# Adding combobox drop down list
categorychoosen['values'] = ('Advocate','Arts','Automation Testing','Blockchain','Business Analyst',
                             'Web Designing')
  
categorychoosen.place(x=50, y=150)
categorychoosen.current()



    name=Label(line3,text="Welcom to ... company",font =("Arial", 10))
name.place(x=0, y=0)

n1 = StringVar()
sectionchoosen = ttk.Combobox(line3, width = 27, textvariable = n1)
  
# Adding combobox drop down list
sectionchoosen['values'] = (' Computer Science', 
                          ' computer engineering',
                          ' Information Technology',
                          ' artificial intelligence',
                          ' cyber security',
                          ' computer networks',
                          ' Information Security',
                          ' Management Information Systems',
                          ' Software engineering',
                          ' data analysis',
                          ' Data Science')
  
sectionchoosen.place(x=20, y=20)
sectionchoosen.current()

def model():
    import pandas as pd
    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
    from sklearn import preprocessing
    import numpy as np



    Category= ['Advocate','Arts','Automation Testing','Web Designing']
    job = categorychoosen.get()
    section = sectionchoosen.get()
    expert = expertchoosen.get()
    language = languagechoosen.get()

    # transform 
    p = preprocessing.LabelEncoder()

    target= p.fit_transform(Category)
    target.shape
    wordOfjob = p.fit_transform(job)
    wordOfsection = p.fit_transform(section)
    
    #zip
    features = list(zip(wordOfjob,wordOfsection))

   # train_test_split
   x_train, x_test, y_train, y_test = train_test_split(features,target ,random_state = 42, test_size = 0.2)

   # Knn Classifier                       
   model = OneVsRestClassifier(KNeighborsClassifier(n_neighbors=5, metric= 'euclidean'  ))
   model.fit(x_train,y_train)
   prediction = model.predict(x_test)

   # Result in GUI_Lable 
   jobResult = Label(line4,text=str(prediction) ,bg='light gray')
   jobResult.place(x=0, y=80)


but= Button(line4,text="Start",bg='gray', command=model)

but.place(x=0, y=120)



top.mainloop()
 

我对错误有问题,当我执行此代码时,它似乎是这样的:提高valueError(valueError:y应该是1D数组,而是Shape()代替。.


注意:注意: 类别选择sectionChoosenexpertchooshosylanguageChoososen它在Gui Python中的Combobox

我进行了很多搜索,并得出结论,此错误是始终从model.fit(x_train,y_train)中,但我尝试了Internet上提供的解决方案,但我没有进行更正。 即使将我添加到target.shape之后。我遇到同样的错误。在接口中按下按钮时,错误始于启动函数def model()

from tkinter import *
from tkinter import ttk
from tkmacosx import Button
top = Tk()
top.title("Jobs")
top.geometry("1000x800")

line1 = LabelFrame(top, text='')
line1.pack(expand = 'yes', fill = 'both')

n = StringVar()

categorychoosen = ttk.Combobox(line1, width = 27, textvariable = n)


# Adding combobox drop down list
categorychoosen['values'] = ('Advocate','Arts','Automation Testing','Blockchain','Business Analyst',
                             'Web Designing')
  
categorychoosen.place(x=50, y=150)
categorychoosen.current()



    name=Label(line3,text="Welcom to ... company",font =("Arial", 10))
name.place(x=0, y=0)

n1 = StringVar()
sectionchoosen = ttk.Combobox(line3, width = 27, textvariable = n1)
  
# Adding combobox drop down list
sectionchoosen['values'] = (' Computer Science', 
                          ' computer engineering',
                          ' Information Technology',
                          ' artificial intelligence',
                          ' cyber security',
                          ' computer networks',
                          ' Information Security',
                          ' Management Information Systems',
                          ' Software engineering',
                          ' data analysis',
                          ' Data Science')
  
sectionchoosen.place(x=20, y=20)
sectionchoosen.current()

def model():
    import pandas as pd
    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
    from sklearn import preprocessing
    import numpy as np



    Category= ['Advocate','Arts','Automation Testing','Web Designing']
    job = categorychoosen.get()
    section = sectionchoosen.get()
    expert = expertchoosen.get()
    language = languagechoosen.get()

    # transform 
    p = preprocessing.LabelEncoder()

    target= p.fit_transform(Category)
    target.shape
    wordOfjob = p.fit_transform(job)
    wordOfsection = p.fit_transform(section)
    
    #zip
    features = list(zip(wordOfjob,wordOfsection))

   # train_test_split
   x_train, x_test, y_train, y_test = train_test_split(features,target ,random_state = 42, test_size = 0.2)

   # Knn Classifier                       
   model = OneVsRestClassifier(KNeighborsClassifier(n_neighbors=5, metric= 'euclidean'  ))
   model.fit(x_train,y_train)
   prediction = model.predict(x_test)

   # Result in GUI_Lable 
   jobResult = Label(line4,text=str(prediction) ,bg='light gray')
   jobResult.place(x=0, y=80)


but= Button(line4,text="Start",bg='gray', command=model)

but.place(x=0, y=120)



top.mainloop()
 

I have a problem with the error, when I execute this code, it appears like this:raise ValueError( ValueError: y should be a 1d array, got an array of shape () instead..


note:categorychoosen and sectionchoosen and expertchoosen and languagechoosen its Combobox in GUI python

I searched a lot and concluded that this error is always from the line model.fit(x_train,y_train), but I tried the solutions offered on the Internet, but no correction occurred to me. please help me.
Even after adding me to target.shape. I get the same error. The error starts when the button is pressed in the interface, i.s. when the function def model() is started

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文