用python/numpy固定精度的小数的单速编码

发布于 2025-01-18 10:12:12 字数 1057 浏览 2 评论 0原文

在Python中,我想获得精确的十进制数字的单速编码,直到10^{ - 3}。鉴于我的输入分数是,例如,Interval = [0.433,0.223,0.111],它将为每个数字在间隔中生成一堆单式向量。因此,对于第一个浮点数0.433,我们应该获得4 ---> 0 0 0 0 1 0 0 0 0 03 ---> 0 0 0 1 0 0 0 0 0 0 03 ---> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2025-01-25 10:12:12

如评论中所建议的,您可以乘以1000并转换为整数。此后,您可以从数字i中提取每个单独的数字,最后应用标准单速编码:

i = (np.array(interval) * 1000).astype(int)
digits = i // 10 ** np.arange(len(i))[::-1, None] % 10
np.eye(10, dtype=int)[digits.T]

输出:

array([[[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]],

       [[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]],

       [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]])

如果您希望将其连接到30维数组中,则可以做:

np.eye(10, dtype=int)[digits.T].reshape(3, 3 * 10)

输出:输出:

array([[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 1, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        0, 0, 0, 0, 0, 0, 0, 0]])

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:

i = (np.array(interval) * 1000).astype(int)
digits = i // 10 ** np.arange(len(i))[::-1, None] % 10
np.eye(10, dtype=int)[digits.T]

output:

array([[[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]],

       [[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]],

       [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]])

If you prefer to concatenate it into a 30 dimensional array you could do:

np.eye(10, dtype=int)[digits.T].reshape(3, 3 * 10)

output:

array([[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 1, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
        0, 0, 0, 0, 0, 0, 0, 0]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文