Python:* args解压缩,如何从test_train_split重新打包? [英] Python: *args unpacks, how to repack from test_train_split?
问题描述
我在列表x
中放置了多个(类型)输入,并且正在使用
x0_train, x0_test, x1_train, x1_test, ... , y_train, y_test =
train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
# But I have to manually repack
x_train = [x0_train, x1_train]
x_test = [x0_test, x1_test]
进行操作:
x = [some_matrix, scalar_value, something_else, ...]
x0_train, x0_test, x1_train, x1_test, ... , y_train, y_test =
train_test_split(x[0],x[1],... , y, test_size=0.2, random_state=np.random, shuffle=True)
我设法将输入参数x[0], x[1], ...
更改为*x
:
x0_train, x0_test, x1_train, x1_test, ... , y_train, y_test =
train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
# But I have to manually repack
x_train = [x0_train, x1_train]
x_test = [x0_test, x1_test]
但是有一种无需手动重新包装即可接收它的方法吗?等价于什么:
*x_train, *x_test, y_train, y_test =
train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
还是有其他方法可以做到这一点?例如:构造一个字典并使用**解包,但是我仍然遇到同样的问题.反正有什么约定(如果有的话)?
这解决了锯齿形分割问题:
recv = [None for i in range(2*len(x))]
*recv, y_train, y_test = train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
# Edit: credits hpaulj
x_train = recv[::2]
x_test = recv[1::2]
此外,如果有一种方法可以复制引用,那么它也会起作用
x_train = [ None for _ in range(len(x))]
x_test = [ None for _ in range(len(x))]
recv = [item for sublist in zip(x_train, x_test) for item in sublist]
# But unfortunately the above line gives only the values and not references
# Hence doesn't work
*recv, y_train, y_test = train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
I have multiple (type) inputs put inside a list x
and I'm doing the test train split
using:
x = [some_matrix, scalar_value, something_else, ...]
x0_train, x0_test, x1_train, x1_test, ... , y_train, y_test =
train_test_split(x[0],x[1],... , y, test_size=0.2, random_state=np.random, shuffle=True)
I managed to change the input parameters x[0], x[1], ...
to *x
:
x0_train, x0_test, x1_train, x1_test, ... , y_train, y_test =
train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
# But I have to manually repack
x_train = [x0_train, x1_train]
x_test = [x0_test, x1_test]
But is there a way to receive it without having to manually repack? What is the equivalent of:
*x_train, *x_test, y_train, y_test =
train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
Or is there any other way to do this? For eg: constructing a dictionary and using ** to unpack, but I still have the same problem. What is the convention anyway (if one exists)?
This solves the zigzag split problem:
recv = [None for i in range(2*len(x))]
*recv, y_train, y_test = train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
# Edit: credits hpaulj
x_train = recv[::2]
x_test = recv[1::2]
Also, if there's a way to copy references this will work too
x_train = [ None for _ in range(len(x))]
x_test = [ None for _ in range(len(x))]
recv = [item for sublist in zip(x_train, x_test) for item in sublist]
# But unfortunately the above line gives only the values and not references
# Hence doesn't work
*recv, y_train, y_test = train_test_split(*x, y, test_size=0.2, random_state=np.random, shuffle=True)
这篇关于Python:* args解压缩,如何从test_train_split重新打包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!