您似乎对术语“列专业”和“行专业”感到困惑。 Row-major 是 order='c' 和 Column-major 是 order='f'。这是一个显示aht的示例
shape = (2000, 10)
buffer = "".join("{:5d}".format(i) for i in range(np.prod(shape)))
x = np.ndarray(shape, 'S5', order='f', buffer=buffer)
print x[0, 1]
# ' 2000'
print x.flags
# C_CONTIGUOUS : False
# F_CONTIGUOUS : True
# OWNDATA : False
# WRITEABLE : False
# ALIGNED : False
# UPDATEIFCOPY : False
x = np.ndarray((2000, 10), 'S5', order='c', buffer=buffer)
print x[0, 1]
# ' 1'
print x.flags
# C_CONTIGUOUS : True
# F_CONTIGUOUS : False
# OWNDATA : False
# WRITEABLE : False
# ALIGNED : False
# UPDATEIFCOPY : False
有几种方法可以在 numpy 中强制进行内存布局,这里有一些:
# When you create the array:
x = np.empty((2000, 10), order='f')
# Check the order and copy if needed:
x = np.asfortranarray(x)
# Force a copy:
x_fortran = x.copy(order='f')
请注意,所有这 3 种方法都会生成一个“相同”的 numpy 数组,即所有 python 代码看起来都一样:
print np.all(x.copy('c') == x.copy('f'))
# True