Python 是否可以转换以字节为单位的要素映射?
Python 3.6 Unity 2019 我正试图找到将要素地图数据传输到unity的最佳解决方案 我想用字节发送数据。然而,我并没有找到如何将其编码为字节,然后在统一中对其进行解码 基本上是一个4d数组,需要根据我对它的理解转换成字节 蟒蛇片Python 是否可以转换以字节为单位的要素映射?,python,arrays,unity3d,deep-learning,Python,Arrays,Unity3d,Deep Learning,Python 3.6 Unity 2019 我正试图找到将要素地图数据传输到unity的最佳解决方案 我想用字节发送数据。然而,我并没有找到如何将其编码为字节,然后在统一中对其进行解码 基本上是一个4d数组,需要根据我对它的理解转换成字节 蟒蛇片 for fmap in feature_maps: bytes = [] bytes.append(fmap) arrays_of_features.append(bytes)
for fmap in feature_maps:
bytes = []
bytes.append(fmap)
arrays_of_features.append(bytes)
data = np.array(arrays_of_features, dtype=float) # this is not working because of the fact is multidimensional array apparently.
print(fmap)
c.sendall(data.tobytes())
统一件:
字节[]字节=新字节[4000];
int idxUsedBytes=client.Receive(字节)
灵感:
要素地图如下所示:
[[[[ 0. 0. 0. ... 0. 12.569366
0. ]
[ 0. 0. 0. ... 0. 4.421044
0. ]
[ 0. 0. 0. ... 0. 0.19193476
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
[[ 0. 0. 0. ... 0. 12.910363
0. ]
[ 0. 0. 0. ... 0. 3.987629
0. ]
[ 0. 0. 0. ... 0. 1.6041028
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
...
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
49.52598 ]
[ 0. 0. 0. ... 0. 0.
10.050183 ]
[ 0. 0. 0. ... 0. 9.6911745
0. ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
29.483086 ]
[ 0. 0. 0. ... 0. 0.
24.422682 ]
[ 0. 0. 2.253025 ... 0. 0.
15.935954 ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 18.458588 ... 15.824303 0.
0. ]
...
[ 0. 0. 0. ... 25.163502 56.87079
42.9939 ]
[ 0. 0. 11.397255 ... 36.644962 17.04247
44.108196 ]
[ 0. 0. 33.134758 ... 30.220499 8.817273
36.6427 ]]]]
你的问题很不清楚,我相信你对numpy的工作原理感到困惑。如果是这样,让我们解释一些事情。numpy中的数组只不过是内存中的一个字节字符串。特别是,当为您显示这些字节时,它们将由数据类型进行解释。数据类型不用于存储基础数据,而仅用于显示它。因此,更改数据类型只会更改数据的外观,而不会更改数据本身。维度也是一样。数据的维度只会更改数据的显示和访问方式,python实际上不会移动数据或更改数据本身。比如说,
import numpy as np
x = np.array([[1,2,3],[4,5,6]],dtype='int64') #48 bytes, each int takes up 8 bytes.
print(x)
x.dtype = 'int32'
print(x)
x.dtype = 'float'
print(x)
x.dtype = 'int16'
print(x)
请注意,我们可以更改数据类型,绝对零计算由数组完成(因为底层数据已经是字节数组)。同样地,我们可以改变形状,也可以进行绝对零计算
x.shape = (2,2,6)
print(x)
形状和数据类型与内存中存储的数据无关。希望这能清楚地说明我们现在如何使用字节数组
x = np.array([[1,2,3],[4,5,6]],dtype='int64')
print(x)
y = x.tobytes()
# Send y somewhere. Save to a file. Etc.
z = np.frombuffer(y)
z.dtype = 'int64'
z.shape = (2,3)
print(z)
x = np.array([[1,2,3],[4,5,6]],dtype='int64')
print(x)
y = x.tobytes()
# Send y somewhere. Save to a file. Etc.
z = np.frombuffer(y)
z.dtype = 'int64'
z.shape = (2,3)
print(z)