【发布时间】:2020-03-23 11:55:05
【问题描述】:
假设有以下几行代码
import numpy as np
# The values equal to 1 inside this nested list indicate where the data need to be loaded. a = [7 x 6]
a = [
[0, 1, 0, 1, None, None],
[0, 0, 0, 0, None, 0],
[0, 0, 1, 0, None, 0],
[0, 1, 0, 1, None, 1],
[0, 0, 0, 1, None, 0],
[0, 0, 0, 0, None, 0],
[1, 0, 0, 0, None, None]
]
# The list "a" cannot be modified for a number of reasons, so I create a np.array copy, named "b"
b = np.array(a)
N = int(1E7) # Number of samples
# The loop below retrieves the positions inside "b" in which data need to be loaded
row = []
col = []
for i in range(len(b)):
col.append([])
if any(b[i] == 1):
row.append(i)
for j in range(len(a[i])):
if b[i][j] is 1:
b[i][j] = np.zeros((N, 1))
col[i].append(j)
# Loading the data inside the selected positions of "b". "mydata" is a numpy array, whose shape is (N, 6)
for i in row:
mydata = np.random.randn(N, len(a[0])).reshape(N, len(a[0])) # Generation of dummy data
b[i, col[i]] = mydata[:, col[i]] # This instruction returns a ValueError
但是,我收到以下错误: ValueError:形状不匹配:形状(10000000,2)的值数组无法广播到形状(2,)的索引结果
为什么这种切片不能正常工作?是不是因为"b"里面的数组元素大小可变?
提前谢谢你。
【问题讨论】:
-
我在这里试图实现的主要目标是在数据加载过程中避免在
for i in row之后出现第二个循环for j in col[i],只是为了提高效率
标签: python arrays list numpy numpy-slicing