sklearn.exceptions.NotFittedError如何摆脱这种情况

时间:2019-12-01 18:39:07

标签: python python-3.x scikit-learn

基本上,我正在尝试为多线性回归模型创建GUI,在该模型中,用户将能够添加一些他的值并从中获得电影得分。 这是代码:

import tkinter as tk 
import sklearn as sk

root= tk.Tk() 

canvas1 = tk.Canvas(root, width = 1200, height = 450)
canvas1.pack()

print_model = model.summary()
label_model = tk.Label(root, text=print_model, justify = 'center', relief = 'solid', bg='LightSkyBlue1')
canvas1.create_window(800, 220, window=label_model)

label1 = tk.Label(root, text=' Taper le nombre de vote: ')
label1.pack(side="left")
canvas1.create_window(80, 100, window=label1)


entry1 = tk.Entry (root) 
canvas1.create_window(300, 100, window=entry1)

label2 = tk.Label(root, text=' Taper la durée du film (en minutes): ')
label2.pack(side="left")
canvas1.create_window(80, 120, window=label2)

entry2 = tk.Entry (root) 
canvas1.create_window(300, 120, window=entry2)

label3 = tk.Label(root, text=' Est-ce que le fim est en anglais? (1=oui, 0=non): ')
label3.pack(side="left")
canvas1.create_window(80, 140, window=label3)

entry3 = tk.Entry (root) 
canvas1.create_window(300, 140, window=entry3)

label4 = tk.Label(root, text=' Est-ce que le film est PG-13? (1=oui,0=non): ')
label4.pack(side="left")
canvas1.create_window(80, 160, window=label4)


entry4 = tk.Entry (root) 
canvas1.create_window(300, 160, window=entry4)

label5 = tk.Label(root, text=' Est-ce que le film est PG? (1=oui,0=non): ')
label5.pack(side="left")
canvas1.create_window(80, 180, window=label5)

entry5 = tk.Entry (root) 
canvas1.create_window(300, 180, window=entry5)

label6 = tk.Label(root, text=' Est-ce que le film est G? (1=oui,0=non): ')
label6.pack(side="left")
canvas1.create_window(80, 200, window=label6)

entry6 = tk.Entry (root) 
canvas1.create_window(300, 200, window=entry6)

label7 = tk.Label(root, text=' Est-ce que le film est R? (1=oui,0=non): ')
label7.pack(side="left")
canvas1.create_window(80, 220, window=label7)

entry7 = tk.Entry (root) 
canvas1.create_window(300, 220, window=entry7)

regr = linear_model.LinearRegression()

def values(): 
    global New_Nb_Vote #our 1st input variable
    New_Nb_Vote = float(entry1.get()) 

    global New_Durée 
    New_Durée = float(entry2.get()) 

    global New_Anglais 
    New_Anglais = float(entry3.get())

    global New_PG_13 
    New_PG_13 = float(entry4.get())

    global New_PG 
    New_PG = float(entry5.get())

    global New_G 
    New_G = float(entry6.get())

    global New_R 
    New_R = float(entry7.get())

    Prediction_result  = ('Score IMDb prédit: ', regr.predict([[New_Nb_Vote,New_Durée,New_Anglais,New_PG_13,New_PG,New_G,New_R]]))
    label_Prediction = tk.Label(root, text= Prediction_result, bg='orange')
    canvas1.create_window(80, 250, window=label_Prediction)

button1 = tk.Button (root, text='Prédire score IMDb',command=values, bg='orange')  
canvas1.create_window(270, 250, window=button1)


root.mainloop()

基本上,我使用的是预先导入的CSV,并且已经为上述代码找到了必要的值:

regr = linear_model.LinearRegression()
regr.fit(first, second)

print('Intercept: \n', regr.intercept_)
print('Coefficients: \n', regr.coef_)


**Gives me this**


Intercept: 
 37.26301371428632
Coefficients: 
 [ 0.00000000e+00  3.18885131e-06  8.98583585e-03 -1.55836415e-02
 -7.78758275e-01 -3.63227977e-01 -2.33707300e-01 -7.10371314e-02
 -5.81148889e-02]

GUI看起来不错,但是每当我向其中输入值时,都会出现如下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\owner\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "<ipython-input-199-b62d64111f11>", line 25, in values
    Prediction_result  = ('Score IMDb prédit: ', regr.predict([[New_Nb_Vote,New_Durée,New_Anglais,New_PG_13,New_PG,New_G,New_R]]))
  File "C:\Users\owner\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 221, in predict
    return self._decision_function(X)
  File "C:\Users\owner\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 202, in _decision_function
    check_is_fitted(self, "coef_")
  File "C:\Users\owner\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 914, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This LinearRegression instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

编辑:有效的部分

bd_f = bd_f.dropna()
first = bd_f[['Nombre_de_vote','Durée','Année','Langue_English','Type_de_contenu_PG-13','Type_de_contenu_PG','Type_de_contenu_G','Type_de_contenu_R']] 
second = bd_f['IMDb']

regr = linear_model.LinearRegression()
regr.fit(first, second)

print('Intercept: \n', regr.intercept_)
print('Coefficients: \n', regr.coef_)

它是为学校项目设计的,现在就停滞了,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

在尝试在回归模型上使用predict之前,需要使用训练数据对模型进行拟合/训练。

确定要打吗

regr.fit(first, second)

在调用预测函数之前

Prediction_result  = ('Score IMDb prédit: ', regr.predict([[New_Nb_Vote,New_Durée,New_Anglais,New_PG_13,New_PG,New_G,New_R]]))

编辑:最终的组合代码(由于我没有数据也没有目标,所以我只能作一些假设)

这是组合的代码。

import tkinter as tk 
import sklearn as sk
import pandas as pd

root= tk.Tk() 

canvas1 = tk.Canvas(root, width = 1200, height = 450)
canvas1.pack()

bd_f = pd.read_csv('data.csv')

bd_f = bd_f.dropna()
first = bd_f[['Nombre_de_vote','Durée','Année','Langue_English','Type_de_contenu_PG-13','Type_de_contenu_PG','Type_de_contenu_G','Type_de_contenu_R']] 
second = bd_f['IMDb']

regr = linear_model.LinearRegression()
regr.fit(first, second)

print('Intercept: \n', regr.intercept_)
print('Coefficients: \n', regr.coef_)

# Not sure what model is here, did you mean regr here
print_model = model.summary()

label_model = tk.Label(root, text=print_model, justify = 'center', relief = 'solid', bg='LightSkyBlue1')
canvas1.create_window(800, 220, window=label_model)

label1 = tk.Label(root, text=' Taper le nombre de vote: ')
label1.pack(side="left")
canvas1.create_window(80, 100, window=label1)


entry1 = tk.Entry (root) 
canvas1.create_window(300, 100, window=entry1)

label2 = tk.Label(root, text=' Taper la durée du film (en minutes): ')
label2.pack(side="left")
canvas1.create_window(80, 120, window=label2)

entry2 = tk.Entry (root) 
canvas1.create_window(300, 120, window=entry2)

label3 = tk.Label(root, text=' Est-ce que le fim est en anglais? (1=oui, 0=non): ')
label3.pack(side="left")
canvas1.create_window(80, 140, window=label3)

entry3 = tk.Entry (root) 
canvas1.create_window(300, 140, window=entry3)

label4 = tk.Label(root, text=' Est-ce que le film est PG-13? (1=oui,0=non): ')
label4.pack(side="left")
canvas1.create_window(80, 160, window=label4)


entry4 = tk.Entry (root) 
canvas1.create_window(300, 160, window=entry4)

label5 = tk.Label(root, text=' Est-ce que le film est PG? (1=oui,0=non): ')
label5.pack(side="left")
canvas1.create_window(80, 180, window=label5)

entry5 = tk.Entry (root) 
canvas1.create_window(300, 180, window=entry5)

label6 = tk.Label(root, text=' Est-ce que le film est G? (1=oui,0=non): ')
label6.pack(side="left")
canvas1.create_window(80, 200, window=label6)

entry6 = tk.Entry (root) 
canvas1.create_window(300, 200, window=entry6)

label7 = tk.Label(root, text=' Est-ce que le film est R? (1=oui,0=non): ')
label7.pack(side="left")
canvas1.create_window(80, 220, window=label7)

entry7 = tk.Entry (root) 
canvas1.create_window(300, 220, window=entry7)

def values(): 
    global New_Nb_Vote #our 1st input variable
    New_Nb_Vote = float(entry1.get()) 

    global New_Durée 
    New_Durée = float(entry2.get()) 

    global New_Anglais 
    New_Anglais = float(entry3.get())

    global New_PG_13 
    New_PG_13 = float(entry4.get())

    global New_PG 
    New_PG = float(entry5.get())

    global New_G 
    New_G = float(entry6.get())

    global New_R 
    New_R = float(entry7.get())

    Prediction_result  = ('Score IMDb prédit: ', regr.predict([[New_Nb_Vote,New_Durée,New_Anglais,New_PG_13,New_PG,New_G,New_R]]))
    label_Prediction = tk.Label(root, text= Prediction_result, bg='orange')
    canvas1.create_window(80, 250, window=label_Prediction)

button1 = tk.Button (root, text='Prédire score IMDb',command=values, bg='orange')  
canvas1.create_window(270, 250, window=button1)


root.mainloop()