用python/numpy固定精度的小数的单速编码
在Python中,我想获得精确的十进制数字的单速编码,直到10^{ - 3}
。鉴于我的输入分数是,例如,Interval = [0.433,0.223,0.111]
,它将为每个数字在间隔中生成一堆单式向量。因此,对于第一个浮点数0.433
,我们应该获得4 ---> 0 0 0 0 1 0 0 0 0 0
,3 ---> 0 0 0 1 0 0 0 0 0 0 0
,3 ---> 0 0 0 1 0 0 0 0 0 0 0
,然后将三个维度单速阵列的三个加成。
另外,我想知道这一点:即使输入编号是[0.4,0.2,0.1]
,是否有一种方法可以应用与以前相同的技术?就像考虑数学上等效的数字[0.400,0.200,0.100]
?
编辑:
这是我提出的尝试。我不确定这是实现结果的最佳方法,ADN此外,这不是解决我们得到的情况,例如[0.4,0.2]
,但我们希望将其解释为> [0.400,0.200]
。
def encode_digits(floats: list):
decimals = []
for f in floats:
decimals.append(str(f).split('0.',1)[1])
one_hot = []
for i in range(2):
for j in range(3):
temp =np.zeros(10)
temp[int(decimals[i][j])] += 1
one_hot.append(temp)
return np.array(one_hot).reshape(-1)
In Python, I would like to achieve a one-hot encoding of decimal numbers with precision until 10^{-3}
. Given that my input fractions are, e.g., interval = [0.433,0.223,0.111]
, it would produce a stack of one-hot vectors for each number in interval. So for the first float number 0.433
we should get 4 ---> 0 0 0 0 1 0 0 0 0 0
, 3 ---> 0 0 0 1 0 0 0 0 0 0
, 3 ---> 0 0 0 1 0 0 0 0 0 0
and then concatenate the three obtaining a 30 dimensional one-hot array.
Also, I'm wondering about this: even if the input numbers are, let's say, [0.4,0.2,0.1]
, is there a way to apply the same technique as before? Like considering mathematically equivalent numbers [0.400,0.200,0.100]
?
EDIT:
This is my proposed attempt. I'm not sure this is the best way to accomplish the result, adn additionally this not solves the case where we are given for example [0.4, 0.2]
but we want to interpret it as [0.400, 0.200]
.
def encode_digits(floats: list):
decimals = []
for f in floats:
decimals.append(str(f).split('0.',1)[1])
one_hot = []
for i in range(2):
for j in range(3):
temp =np.zeros(10)
temp[int(decimals[i][j])] += 1
one_hot.append(temp)
return np.array(one_hot).reshape(-1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论中所建议的,您可以乘以1000并转换为整数。此后,您可以从数字
i
中提取每个单独的数字,最后应用标准单速编码:输出:
如果您希望将其连接到30维数组中,则可以做:
输出:输出:
As suggested in the comment you can multiply by 1000 and convert to integer. Thereafter you can extract each individual digit from the numbers
i
and finally apply standard one-hot encoding:output:
If you prefer to concatenate it into a 30 dimensional array you could do:
output: