简体   繁体   中英

Generalize Numpy slicing Python

I am not getting an idea how I can generalize the slicing of the numpy array. See the following slicing:

x_train = data[:train_set_size,:-1,4:-1]    
x_valid = data[train_set_size:train_set_size+valid_set_size,:-1,4:-1]
x_test = data[train_set_size+valid_set_size:,:-1,4:-1]

y_train = data[:train_set_size,-1,-2:]
y_valid = data[train_set_size:train_set_size+valid_set_size,-1,-2:]
y_test = data[train_set_size+valid_set_size:,-1,-2:]

As you can see x set is sliced as :-1,4:-1 and y set as -1,-2: . If I have to make changes then I need to change 3 times whereas if there a way to store the slicing in variable and just change variable and the changes are reflected.

for example: xset_slice = ":-1,4:-1" and yset_slice = "-1,-2:" . Then just replacing in the place as:

x_train = data[:train_set_size,xset_slice ]    
x_valid = data[train_set_size:train_set_size+valid_set_size,xset_slice ]
x_test = data[train_set_size+valid_set_size:,xset_slice ]
y_train = data[:train_set_size,-1,-2:]
y_valid = data[train_set_size:train_set_size+valid_set_size,yset_slice ]
y_test = data[train_set_size+valid_set_size:,yset_slice]

But this sort of statement gives error. Hence, please let me know is there any analogous methodology that can help me.

you can use sklearn.model_selection.train_test_split

 X_train, X_test, y_train, y_test 
    = train_test_split(X, y, test_size=0.2, random_state=1)

 X_train, X_val, y_train, y_val 
    = train_test_split(X_train, y_train, test_size=0.2, random_state=1)

You could construct the indices from slice objects and tuples:

slice1 = slice(-1)
slice2 = slice(4,-1)
slice3 = slice(-2,None)

x_train = data[:train_set_size,slice1, slice2]    
x_valid = data[train_set_size:train_set_size+valid_set_size, slice1, slice2]
x_test = data[train_set_size+valid_set_size:, slice1, slice2]

Make a composite tuple by joining several tuples:

tup1 = (slice1, slice2)
# or with np.s_[:-1, 4:-1]
x_train = data[(slice(train_set_size),+tup1]    
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM