gpt4 book ai didi

python - 类型错误 : train_test_split() got an unexpected keyword argument 'test_size'

转载 作者:行者123 更新时间:2023-11-30 09:31:49 26 4
gpt4 key购买 nike

我正在尝试使用随机森林方法找到最佳特征集我需要将数据集分为测试和训练。这是我的代码

from sklearn.model_selection import train_test_split

def train_test_split(x,y):
# split data train 70 % and test 30 %
x_train, x_test, y_train, y_test = train_test_split(x, y,train_size=0.3,random_state=42)
#normalization
x_train_N = (x_train-x_train.mean())/(x_train.max()-x_train.min())
x_test_N = (x_test-x_test.mean())/(x_test.max()-x_test.min())

train_test_split(data,data_y)

参数data,data_y解析正确。但我收到以下错误。我不明白这是为什么。

enter image description here

最佳答案

您在代码中使用的函数名称与 sklearn.preprocessing 中的函数名称相同,更改函数名称即可完成这项工作。像这样的事情,

from sklearn.model_selection import train_test_split

def my_train_test_split(x,y):
# split data train 70 % and test 30 %
x_train, x_test, y_train, y_test = train_test_split(x,y,train_size=0.3,random_state=42)
#normalization
x_train_N = (x_train-x_train.mean())/(x_train.max()-x_train.min())
x_test_N = (x_test-x_test.mean())/(x_test.max()-x_test.min())

my_train_test_split(data,data_y)

解释:- 虽然 python 中存在方法重载(即根据参数类型选择相同的命名函数),但在您的情况下,这两个函数都需要相同类型的参数,因此不同的命名是IMO 唯一可能的解决方案。

关于python - 类型错误 : train_test_split() got an unexpected keyword argument 'test_size' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54732163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com