这是我的数组:
array([-0.1142 , 0.11127 , 0.0374 , 0.02007 , -0.05737 , -0.02058 ,
-0.1595 , -0.1288 , 0.1436 , -0.05212 , 0.2437 , 0.0046 ,
-0.1456 , -0.09485 , -0.0788 , 0.1755 , -0.2429 , -0.1204 ,
-0.01064 , 0.01154 , 0.06058 , -0.02666 , 0.01773 , 0.03436 ,
-0.1262 , -0.3428 , -0.068 , -0.10645 , 0.0669 , -0.02094 ,
-0.0751 , 0.001348, -0.1737 , 0.01146 , 0.01648 , 0.03613 ,
0.03384 , -0.063 , 0.1617 , -0.03023 , -0.258 , 0.0385 ,
0.0382 , 0.1821 , 0.2104 , -0.01604 , 0.05945 , -0.1809 ,
0.1847 , -0.1569 , 0.02007 , 0.1757 , 0.08514 , 0.07886 ,
-0.00872 , -0.1108 , -0.01473 , 0.1075 , -0.1221 , 0.0163 ,
0.03275 , -0.01775 , 0.01232 , -0.0705 ], dtype=float16)
我需要将其转换为二进制,应该识别小数和负号,然后每个元素应该输出16位,但我不知道如何请帮助我。我正在使用Jupyter Notebook来运行我的python程序。
使用struct
模块将一半(16位浮点数)解释为整数,然后将该整数转换为16个字符长的二进制字符串。
根据文档,!
表示使用计算机系统(大端或小端)对两个字节进行排序,H
表示unsigned short
(16位无符号整数),e
表示half
(16位浮点数)
import numpy as np
import struct
arr = np.array([-0.1142 , 0.11127 , 0.0374 , 0.02007 , -0.05737 , -0.02058 ,
-0.1595 , -0.1288 , 0.1436 , -0.05212 , 0.2437 , 0.0046 ,
-0.1456 , -0.09485 , -0.0788 , 0.1755 , -0.2429 , -0.1204 ,
-0.01064 , 0.01154 , 0.06058 , -0.02666 , 0.01773 , 0.03436 ,
-0.1262 , -0.3428 , -0.068 , -0.10645 , 0.0669 , -0.02094 ,
-0.0751 , 0.001348, -0.1737 , 0.01146 , 0.01648 , 0.03613 ,
0.03384 , -0.063 , 0.1617 , -0.03023 , -0.258 , 0.0385 ,
0.0382 , 0.1821 , 0.2104 , -0.01604 , 0.05945 , -0.1809 ,
0.1847 , -0.1569 , 0.02007 , 0.1757 , 0.08514 , 0.07886 ,
-0.00872 , -0.1108 , -0.01473 , 0.1075 , -0.1221 , 0.0163 ,
0.03275 , -0.01775 , 0.01232 , -0.0705 ], dtype=np.float16)
def half_to_binstr(half):
bits, = struct.unpack('!H', struct.pack('!e', half))
return "{:016b}".format(bits)
res = np.vectorize(half_to_binstr)(arr)
输出:
array(['1010111101001111', '0010111100011111', '0010100011001010',
'0010010100100011', '1010101101011000', '1010010101000101',
'1011000100011011', '1011000000011111', '0011000010011000',
'1010101010101100', '0011001111001100', '0001110010110110',
'1011000010101001', '1010111000010010', '1010110100001011',
'0011000110011110', '1011001111000110', '1010111110110101',
'1010000101110011', '0010000111101001', '0010101111000001',
'1010011011010011', '0010010010001010', '0010100001100110',
'1011000000001010', '1011010101111100', '1010110001011010',
'1010111011010000', '0010110001001000', '1010010101011100',
'1010110011001110', '0001010110000101', '1011000110001111',
'0010000111011110', '0010010000111000', '0010100010100000',
'0010100001010101', '1010110000001000', '0011000100101101',
'1010011110111101', '1011010000100001', '0010100011101110',
'0010100011100100', '0011000111010100', '0011001010111100',
'1010010000011011', '0010101110011100', '1011000111001010',
'0011000111101001', '1011000100000101', '0010010100100011',
'0011000110011111', '0010110101110011', '0010110100001100',
'1010000001110111', '1010111100010111', '1010001110001011',
'0010111011100001', '1010111111010000', '0010010000101100',
'0010100000110001', '1010010010001011', '0010001001001111',
'1010110010000011'], dtype='<U16')