gpt4 book ai didi

python - numpy 从二维矩阵获取子矩阵

转载 作者:行者123 更新时间:2023-12-02 17:04:49 27 4
gpt4 key购买 nike

我有一个 400 列 1000 行的矩阵

从第 10 行第 30 列开始到第 390 行第 960 列从中提取子矩阵的最佳方法是什么?

嗡嗡声

最佳答案

如果您的矩阵名为 arr,请使用:

arr[10:391,30:961]

记住 python 是零索引的!

例如,在这个 10 行 5 列的矩阵上:

>>> arr
array([[0.41296756, 0.14399754, 0.12606098, 0.61114244, 0.83243229],
[0.91409281, 0.28715535, 0.63134933, 0.49837152, 0.15927328],
[0.89021618, 0.16226017, 0.75576869, 0.4949617 , 0.86590637],
[0.7684741 , 0.82399817, 0.48887077, 0.12121645, 0.64524496],
[0.37356536, 0.4548252 , 0.09608423, 0.49361169, 0.12475545],
[0.74875009, 0.16289433, 0.84103655, 0.09670591, 0.34296308],
[0.0697272 , 0.87959546, 0.2928768 , 0.53756533, 0.96180757],
[0.3495039 , 0.09705972, 0.05581994, 0.95367967, 0.94351724],
[0.03739738, 0.65043277, 0.94139692, 0.82945502, 0.51765616],
[0.07702866, 0.57164611, 0.55674663, 0.21082212, 0.76406612]])

我可以使用以下方法选择第 5-7 行、第 1-3 列:

>>> arr[5:8,1:4]
array([[0.16289433, 0.84103655, 0.09670591],
[0.87959546, 0.2928768 , 0.53756533],
[0.09705972, 0.05581994, 0.95367967]])

关于python - numpy 从二维矩阵获取子矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213628/

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