numpy写入数组的排列版本

时间:2018-06-27 21:10:34

标签: python numpy

我必须将numpy ndarray的内容转储到二进制文件,该文件将由第三方程序读取。但是,我要写的是排列轴的内容。例如,我有类似的东西:

import numpy as np
x = np.random.rand(3, 3, 3)

a = np.transpose(x, (1, 0, 2))
a.tofile("a.bin")
x.tofile("x.bin")

在两种情况下,输出的文件都是相同的。是否有类似transpose的操作,它会随意地移动数组的内容,而不是仅仅交换步幅和维数?这样,原始内容将按照我需要的顺序进行序列化。

2 个答案:

答案 0 :(得分:2)

我无法重现您的发现:我得到了不同的数组:

In [11]: np.fromfile('a.bin').reshape((3,3,3))
Out[11]: 
array([[[0.95499073, 0.53044188, 0.31122484],
        [0.44293225, 0.23932913, 0.13954034],
        [0.08992127, 0.59397388, 0.72471928]],

       [[0.43503453, 0.15910105, 0.10589887],
        [0.39610877, 0.68784233, 0.87956587],
        [0.89785046, 0.64688383, 0.40787343]],

       [[0.91490793, 0.31428658, 0.85234109],
        [0.36403572, 0.99601086, 0.46086401],
        [0.43524914, 0.85182394, 0.01254642]]])

In [12]: np.fromfile('x.bin').reshape((3,3,3))
Out[12]: 
array([[[0.95499073, 0.53044188, 0.31122484],
        [0.43503453, 0.15910105, 0.10589887],
        [0.91490793, 0.31428658, 0.85234109]],

       [[0.44293225, 0.23932913, 0.13954034],
        [0.39610877, 0.68784233, 0.87956587],
        [0.36403572, 0.99601086, 0.46086401]],

       [[0.08992127, 0.59397388, 0.72471928],
        [0.89785046, 0.64688383, 0.40787343],
        [0.43524914, 0.85182394, 0.01254642]]])

无需重塑:

In [22]: np.fromfile('a.bin')
Out[22]: 
array([0.95499073, 0.53044188, 0.31122484, 0.44293225, 0.23932913,
       0.13954034, 0.08992127, 0.59397388, 0.72471928, 0.43503453,
       0.15910105, 0.10589887, 0.39610877, 0.68784233, 0.87956587,
       0.89785046, 0.64688383, 0.40787343, 0.91490793, 0.31428658,
       0.85234109, 0.36403572, 0.99601086, 0.46086401, 0.43524914,
       0.85182394, 0.01254642])

In [23]: np.fromfile('x.bin')
Out[23]: 
array([0.95499073, 0.53044188, 0.31122484, 0.43503453, 0.15910105,
       0.10589887, 0.91490793, 0.31428658, 0.85234109, 0.44293225,
       0.23932913, 0.13954034, 0.39610877, 0.68784233, 0.87956587,
       0.36403572, 0.99601086, 0.46086401, 0.08992127, 0.59397388,
       0.72471928, 0.89785046, 0.64688383, 0.40787343, 0.43524914,
       0.85182394, 0.01254642])

我的numpy和Python版本是:

In [21]: import sys
    ...: print(sys.version)
    ...: print("\nNumpy version: " + np.__version__)
    ...: 
2.7.15 |Anaconda, Inc.| (default, May  1 2018, 18:37:05) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

Numpy version: 1.14.5

我还尝试了具有相同结果的不同环境:

In [1]: import sys
   ...: import numpy as np
   ...: print(sys.version)
   ...: print(np.__version__)
   ...: 
3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
1.13.3

In [2]: x = np.random.rand(3, 3, 3)
   ...: a = np.transpose(x, (1, 0, 2))
   ...: a.tofile("a.bin")
   ...: x.tofile("x.bin")
   ...: 

In [3]: np.fromfile('a.bin').reshape((3,3,3))
Out[3]: 
array([[[ 0.7628757 ,  0.5117887 ,  0.85286206],
        [ 0.27096479,  0.5056376 ,  0.14519906],
        [ 0.9517039 ,  0.92225717,  0.85885034]],

       [[ 0.57380259,  0.74694459,  0.19207375],
        [ 0.50738877,  0.33581015,  0.57100872],
        [ 0.54989565,  0.35004858,  0.9527302 ]],

       [[ 0.94359803,  0.6223541 ,  0.57774136],
        [ 0.92983442,  0.98074324,  0.62467311],
        [ 0.49712549,  0.73399765,  0.56790972]]])

In [4]: np.fromfile('x.bin').reshape((3,3,3))
Out[4]: 
array([[[ 0.7628757 ,  0.5117887 ,  0.85286206],
        [ 0.57380259,  0.74694459,  0.19207375],
        [ 0.94359803,  0.6223541 ,  0.57774136]],

       [[ 0.27096479,  0.5056376 ,  0.14519906],
        [ 0.50738877,  0.33581015,  0.57100872],
        [ 0.92983442,  0.98074324,  0.62467311]],

       [[ 0.9517039 ,  0.92225717,  0.85885034],
        [ 0.54989565,  0.35004858,  0.9527302 ],
        [ 0.49712549,  0.73399765,  0.56790972]]])

答案 1 :(得分:1)

我认为您只需要使用numpy的交换轴即可。

import numpy as np
x = np.random.rand(3, 3)
a = np.swapaxes(x,0,1)
a.tofile("a.bin")
x.tofile("x.bin")

希望这会有所帮助!