Python/Numpy - 使用变量从二维数组中提取二维子数组
好的,所以我有一个二维数据数组,其形状为(23025, 1000),称为“allfiles”。
我需要一次遍历数组 50 列并将它们提取到子数组中进行操作。问题是当我使用下面的代码寻址数组时,它似乎无法识别变量(a 和 b)。我目前拥有的代码如下所示。
q = 50
a = np.shape(allfiles)[1] # a = 1000
for i in range(a):
b = a + q
data = allfiles[:,a:b]
当我用数字替换变量时,即..
data = allfiles[:,30:80]
它似乎有效。所以,我的问题是 - 有没有办法可以将变量传递到数组索引?如果没有,是否有更好的方法可以使用变量创建子数组?
我试图在堆栈溢出上找到这个问题,但没有成功,但我确信我不是第一个遇到这个麻烦的人?
干杯,伙计们, 摩根
Ok, So I have a 2-d array of data which has the shape(23025, 1000), it's called 'allfiles'.
I need to go through the array 50 columns at a time and extract them to a sub-array for manipulation. The problem is when i address the array using the code below, it doesn't seem to recognize the variables (a and b). the code i have at the moment is shown below.
q = 50
a = np.shape(allfiles)[1] # a = 1000
for i in range(a):
b = a + q
data = allfiles[:,a:b]
When i replace the variables with number, i.e...
data = allfiles[:,30:80]
It seems to work. So, my question is - is there a way i can pass variables to the array index? If not is there a better way i can create a subarray using variables?
I have tried to find this problem on stack overflow with no luck, but i'm sure i'm not the first person to have this trouble?
Cheers guys,
Morgan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从循环中获取了
i
但不使用它。You are getting
i
from the loop but don't use it.