numpy 在Python 2.7中查看数组时的输出差异

ycl3bljg  于 2023-02-19  发布在  Python
关注(0)|答案(1)|浏览(315)

下面是我的MATLAB数据的1列:

raw_data
10
-2.77738418373534e+242
3.68340627929942e+23
-1.66594278761299e+94
NaN
-2.77738418373534e+242
3.68340627929942e+23
-1.66594278761299e+94
NaN
2.67810000000000e-319
NaN

下面是将其作为输入的脚本:

nb_msg = int(raw_data[0, col])
 uint8_data = np.array(raw_data[1:1+nb_msg, col]).view(dtype=np.uint8)

结果是:

>>> np.array(raw_data[1:1+nb_msg, col]).view(dtype=np.uint8)

array([181, 211,  68, 242, 127, 211,  68, 242, 182, 127, 211,  68, 242,
127, 211,  68, 183, 242, 127, 211,  68, 242, 127, 211, 184,  68,
242, 127, 211,  68, 250, 127, 185, 211,  68, 242, 127, 211,  68,
242, 186, 127, 211,  68, 242, 127, 211,  68, 187, 242, 127, 211,
68, 242, 127, 211, 188,  68, 242, 127, 211,  68, 250, 127, 189,
211,   0,   0,   0,   0,   0,   0, 210,  68, 242, 127, 132,  68,
250, 127], dtype=uint8)

然而,当我明确地这样指定它时:

np.array([-2.77738418e+242,  3.68340628e+023, -1.66594279e+094,np.nan, -2.77738418e+242,3.68340628e+023,-1.66594279e+094,np.nan,2.67808283e-319,np.nan]).view(dtype=np.uint8)

结果变为:

> > >np.array([-2.77738418e+242,  3.68340628e+023, -1.66594279e+094,np.nan, -2.77738418e+242,3.68340628e+023,-1.66594279e+094,np.nan,2.67808283e-319,np.nan]).view(dtype=np.uint8)

array([ 24, 135, 204, 241, 127, 211,  68, 242, 163, 109, 227,  68, 242,
127, 211,  68, 112, 139,  68, 212,  68, 242, 127, 211,   0,   0,
0,   0,   0,   0, 248, 127,  24, 135, 204, 241, 127, 211,  68,
242, 163, 109, 227,  68, 242, 127, 211,  68, 112, 139,  68, 212,
68, 242, 127, 211,   0,   0,   0,   0,   0,   0, 248, 127, 189,
211,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
48, 127], dtype=uint8)

这种差异归因于什么?
我最初认为MATLAB数据和我的手工输入之间的尾数差就是这样做的,但值得注意的是,Python返回的结果是:

>>> np.array(raw_data[1:1+nb_msg, col])

array([-2.77738418e+242,  3.68340628e+023, -1.66594279e+094,
                    nan, -2.77738418e+242,  3.68340628e+023,
       -1.66594279e+094,              nan,  2.67808283e-319,
                    nan])

它与显式指定的数组相同。

sh7euo9m

sh7euo9m1#

数据和我手动输入的尾数差异导致uint8表示法的差异。

>>> np.array([-2.77738418e+242,  3.68340628e+023, -1.66594279e+094,np.nan, -2.77738418e+242,3.68340628e+023,-1.66594279e+094,np.nan,2.67808283e-319,np.nan]).view(dtype=np.uint8)
array([ 24, 135, 204, 241, 127, 211,  68, 242, 163, 109, 227,  68, 242,
       127, 211,  68, 112, 139,  68, 212,  68, 242, 127, 211,   0,   0,
         0,   0,   0,   0, 248, 127,  24, 135, 204, 241, 127, 211,  68,
       242, 163, 109, 227,  68, 242, 127, 211,  68, 112, 139,  68, 212,
        68, 242, 127, 211,   0,   0,   0,   0,   0,   0, 248, 127, 189,
       211,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
       248, 127], dtype=uint8)

照这样更改数组的第一个元素,将显示预期结果:

>>> np.array([-2.77738418373534e+242,  3.68340628e+023, -1.66594279e+094,np.nan, -2.77738418e+242,3.68340628e+023,-1.66594279e+094,np.nan,2.67808283e-319,np.nan]).view(dtype=np.uint8)
array([190, 211,  68, 242, 127, 211,  68, 242, 163, 109, 227,  68, 242,
       127, 211,  68, 112, 139,  68, 212,  68, 242, 127, 211,   0,   0,
         0,   0,   0,   0, 248, 127,  24, 135, 204, 241, 127, 211,  68,
       242, 163, 109, 227,  68, 242, 127, 211,  68, 112, 139,  68, 212,
        68, 242, 127, 211,   0,   0,   0,   0,   0,   0, 248, 127, 189,
       211,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
       248, 127], dtype=uint8)

相关问题