def split_data(): test_size = float(entry.get()) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=0) huafen=(f'X_train: {X_train}, X_test: {X_test}, y_train: {y_train}, y_test: {y_test}') a6=Text(root) a6.place(x=600, y=100,height=100,width=500) a6.insert(END, huafen) label=tk.Label(root, text='请输入测试集比例:') label.place(x=600,y=65) entry=tk.Entry(root) entry.place(x=700,y=65) button = tk.Button(root, text='划分数据集', command=split_data) button.place(x=850,y=65)怎么能把X_train, X_test, y_train, y_test拿到def外面使用
时间: 2023-12-24 17:14:51 浏览: 76
你可以在`split_data()`函数中返回`X_train, X_test, y_train, y_test`,然后在函数调用的地方保存返回值到全局变量中,这样就可以在函数外部使用了。
例如:
```
def split_data():
test_size = float(entry.get())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=0)
huafen=(f'X_train: {X_train}, X_test: {X_test}, y_train: {y_train}, y_test: {y_test}')
a6=Text(root)
a6.place(x=600, y=100,height=100,width=500)
a6.insert(END, huafen)
return X_train, X_test, y_train, y_test
label=tk.Label(root, text='请输入测试集比例:')
label.place(x=600,y=65)
entry=tk.Entry(root)
entry.place(x=700,y=65)
button = tk.Button(root, text='划分数据集', command=split_data)
button.place(x=850,y=65)
X_train, X_test, y_train, y_test = None, None, None, None # 初始化全局变量
# 在需要使用 X_train, X_test, y_train, y_test 的地方调用 split_data 函数并保存返回值到全局变量中
X_train, X_test, y_train, y_test = split_data()
```
阅读全文