推广 Numpy 切片 Python
Generalize Numpy slicing Python
我不知道如何概括 numpy 数组的切片。
请参阅以下切片:
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:]
如您所见,x
集被分割为 :-1,4:-1
,y
被分割为 -1,-2:
。如果我必须进行更改,那么我需要更改 3 次,而如果有一种方法可以将切片存储在变量中,并且只需更改变量并反映更改。
例如:xset_slice = ":-1,4:-1"
和 yset_slice = "-1,-2:"
。然后只需将位置替换为:
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]
但是这种说法会报错。因此,请让我知道是否有任何类似的方法可以帮助我。
你可以使用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)
您可以从 slice
个对象和元组构建索引:
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]
通过连接多个元组来创建复合元组:
tup1 = (slice1, slice2)
# or with np.s_[:-1, 4:-1]
x_train = data[(slice(train_set_size),+tup1]
...
我不知道如何概括 numpy 数组的切片。 请参阅以下切片:
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:]
如您所见,x
集被分割为 :-1,4:-1
,y
被分割为 -1,-2:
。如果我必须进行更改,那么我需要更改 3 次,而如果有一种方法可以将切片存储在变量中,并且只需更改变量并反映更改。
例如:xset_slice = ":-1,4:-1"
和 yset_slice = "-1,-2:"
。然后只需将位置替换为:
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]
但是这种说法会报错。因此,请让我知道是否有任何类似的方法可以帮助我。
你可以使用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)
您可以从 slice
个对象和元组构建索引:
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]
通过连接多个元组来创建复合元组:
tup1 = (slice1, slice2)
# or with np.s_[:-1, 4:-1]
x_train = data[(slice(train_set_size),+tup1]
...