简体   繁体   English

在numpy.array中没有fortran命令

[英]No fortran order in numpy.array

I see no fortran order in: 我看到没有强烈的命令:

import numpy as np
In [143]: np.array([[1,2],[3,4]],order='F')
Out[143]: 
array([[1, 2],
       [3, 4]])

But in the following it works: 但在下面它的工作原理:

In [139]: np.reshape(np.arange(9),newshape=(3,3),order='F')
Out[139]: 
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])

So what am I doing wrong in the first one? 那我在第一个做错了什么?

When you call numpy.array to create an array from an existing Python object, it will give you an object with whatever shape that the original Python object has. 当您调用numpy.array从现有Python对象创建数组时,它将为您提供一个具有原始Python对象所具有的任何形状的对象。 So, 所以,

np.array([[1,2],[3,4]], ...)

Will always give you, 永远都会给你,

np.array([[1, 2],
          [3, 4]])

Which is exactly what you typed in, so it should not come as a surprise. 这正是你输入的内容,所以它不应该让人感到意外。 Fortran order and C order do not describe the shape of the data, they describe the memory layout. Fortran order和C order不描述数据的形状,它们描述了内存布局。 When you print out an object, NumPy doesn't show you what the memory layout is, it only shows you the shape. 当您打印出一个对象时,NumPy不会向您显示内存布局,它只显示您的形状。

You can witness that the array truly is stored in Fortran order when you flatten it with the "K" order, which keeps the original order of the elements: 您可以看到,当您使用"K"顺序将数组展平时,该数组确实以Fortran顺序存储,这保持了元素的原始顺序:

>>> a = np.array([[1,2],[3,4]], order="F")
>>> a.flatten(order="K")
array([1, 3, 2, 4])

This is what truly distinguishes Fortran from C order: the memory layout. 这就是Fortran与C顺序的真正区别:内存布局。 Most NumPy functions do not force you to consider memory layout, instead, different layouts are handled transparently. 大多数NumPy函数不会强制您考虑内存布局,而是透明地处理不同的布局。

It sounds like what you want is to transpose, reversing the axis order. 听起来你想要的是转置,反转轴顺序。 This can be done simply: 这可以简单地完成:

>>> b = numpy.transpose(a)
>>> b
array([[1, 3],
       [2, 4]])

This does not create a new array, but a new view of the same array: 这不会创建新数组,而是创建相同数组的新视图:

>>> b.base is a
True

If you want the data to have the memory layout 1 2 3 4 and have a Fortran order view of that [[1, 3], [2, 4]], the efficient way to do this is to store the existing array with C order and then transpose it, which results in a Fortran-order array with the desired contents and requires no extra copies. 如果您希望数据具有内存布局1 2 3 4并且具有[[1,3],[2,4]]的Fortran顺序视图,则执行此操作的有效方法是使用C存储现有数组顺序然后转置它,这导致具有所需内容的Fortran-order数组,并且不需要额外的副本。

>>> a = np.array([[1, 2], [3, 4]]).transpose()
>>> a.flatten(order="K")
array([1, 2, 3, 4])
>>> a
array([[1, 3],
       [2, 4]])

If you store the original with Fortran order, the transposition will result in C order, so you don't want that (or maybe all you care about is the transposition, and memory order is not important?). 如果您使用Fortran订单存储原始文件,则转置将导致C顺序,因此您不希望这样(或者您可能只关心转置,并且内存顺序不重要?)。 In either case, the array will look the same in NumPy. 在任何一种情况下,数组在NumPy中看起来都是一样的。

>>> a = np.array([[1, 2], [3, 4]], order="F").transpose()
>>> a.flatten(order="K")
array([1, 3, 2, 4])
>>> a
array([[1, 3],
       [2, 4]])

Your two means of constructing the 2D array are not at all equivalent. 你构建二维数组的两种方法完全不相同。 In the first, you specified the structure of the array. 在第一个中,您指定了数组的结构。 In the second, you formed an array and then reshaped to your liking. 在第二个,你形成了一个数组,然后根据自己的喜好重新塑造。

>>> np.reshape([1,2,3,4],newshape=(2,2),order='F')
array([[1, 3],
       [2, 4]])

Again, for comparison, even if you ask for the reshape and format change to FORTRAN, you'll get your specified structure: 同样,为了进行比较,即使您要求重新整形并将格式更改为FORTRAN,您也将获得指定的结构:

>>> np.reshape([[1,2],[3,4]],newshape=(2,2),order='F')
array([[1, 2],
       [3, 4]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM