UnboundLocalError: 在使用tkinter时,在赋值前引用了局部变量'X'。

问题描述 投票:-1回答:1

完全错误:-

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Kapil Sharma\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\Kapil Sharma\Desktop\RexD ML Tool\KRexD.py", line 112, in ok_clicked

  imputer.fit(X[:, listOfMissingValuesIndexes])

UnboundLocalError: local variable 'X' referenced before assignment**

我试图执行这段代码:-

def yes_clicked():
                #Here we have to do data preprocessing
                dataPreprocessingWindow = Toplevel()
                dataPreprocessingWindow.title("Data Preprocessing Window")
                dataPreprocessingWindow.geometry("700x700")
                    #Importing the dataset
                dataset = pd.read_csv('combined.csv')
                X = dataset.iloc[:, :-1].values
                y = dataset.iloc[:, -1].values
                Label(dataPreprocessingWindow,text = "Data Imported",font=50).grid()

                missing_values_indexes = StringVar()
                icategorical_values_indexes = StringVar()
                dcategorical_values_indexes = StringVar()
                def ok_clicked():
                    listOfMissingValuesIndexes = []
                    listOfICategoricalValuesIndexes = []
                    listOfDCategoricalValuesIndexes = []
                    index = missing_values_indexes.get()
                    iindex = icategorical_values_indexes.get()
                    dindex = dcategorical_values_indexes.get()
                    for i in index:
                        if(i==' '):
                            pass
                        else:
                            ind = int(i)
                            listOfMissingValuesIndexes.append(ind)
                    for i in iindex:
                        if(i==' '):
                            pass
                        else:
                            ind = int(i)
                            listOfICategoricalValuesIndexes.append(ind)
                    for i in dindex:
                        if(i==' '):
                            pass
                        else:
                            ind = int(i)
                            listOfDCategoricalValuesIndexes.append(ind)
                    from sklearn.impute import SimpleImputer
                    imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
                    imputer.fit(X[:, listOfMissingValuesIndexes])
                    X[:, listOfMissingValuesIndexes] = imputer.transform(X[:, listOfMissingValuesIndexes])
                    print(X)
                    from sklearn.compose import ColumnTransformer
                    from sklearn.preprocessing import OneHotEncoder
                    ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), listOfICategoricalValuesIndexes)], remainder='passthrough')
                    X = np.array(ct.fit_transform(X))
                    print(X)
                    from sklearn.preprocessing import LabelEncoder
                    le = LabelEncoder()
                    y = le.fit_transform(y)
                    print(y)
                Label(dataPreprocessingWindow,text = "Enter indexes(space separated) that can contain missing data",font=70).grid(row=0,column=0)
                Entry(dataPreprocessingWindow,textvariable = missing_values_indexes,width=50).grid()
                Label(dataPreprocessingWindow,text = "Enter indexes(space separated) of independent variables that can contain categorical data",font=70).grid()
                Entry(dataPreprocessingWindow,textvariable = icategorical_values_indexes,width=50).grid()
                Label(dataPreprocessingWindow,text = "Enter indexes(space separated) of dependent variables that can contain missing data",font=70).grid()
                Entry(dataPreprocessingWindow,textvariable = dcategorical_values_indexes,width=50).grid()
                Button(dataPreprocessingWindow,text = "OK",command = ok_clicked,cursor="hand2").grid()
python tkinter
1个回答
0
投票

替换 def yes_clicked():def yes_clicked(X):yes_clicked():yes_clicked(X):. 或者你可以将X定义为一个 全局变量.

© www.soinside.com 2019 - 2024. All rights reserved.