我有一個尺寸為 698x64x64(64 行,64 列)的 NumPy 陣列
我想通過獲取原始陣列的每隔一行和一列將它切成一個 698x32x32 的新陣列。我怎么做?
uj5u.com熱心網友回復:
IIUC,你可以slicing像下面這樣使用。(你可以閱讀這個:理解切片)
import numpy as np
np.random.seed(123)
# this array is a sample array for showing result
a = np.random.rand(2,4,4)
print(a)
b = a[:, 1::2, 1::2] # <- you can use this for (698, 64, 64)
print(b)
輸出:
# a =>
array([[[0.69646919, 0.28613933, 0.22685145, 0.55131477],
[0.71946897, 0.42310646, 0.9807642 , 0.68482974],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
[0.4809319 , 0.39211752, 0.34317802, 0.72904971],
[0.43857224, 0.0596779 , 0.39804426, 0.73799541]],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
[[0.18249173, 0.17545176, 0.53155137, 0.53182759],
[0.63440096, 0.84943179, 0.72445532, 0.61102351],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
[0.72244338, 0.32295891, 0.36178866, 0.22826323],
[0.29371405, 0.63097612, 0.09210494, 0.43370117]]])
# -------------------^^^^^^^^^^,-----------,, ^^^^^^^^^^ <- you want this
# b =>
array([[[0.42310646, 0.68482974],
[0.0596779 , 0.73799541]],
[[0.84943179, 0.61102351],
[0.63097612, 0.43370117]]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/519642.html
上一篇:從作業日、月份和年份獲取日期
下一篇:Numpy轉換資料
