简体   繁体   English

Python 二维 numpy.array 之和的错误行为

[英]Python wrong behavior for the SUM OF 2D numpy.array

So, I have a list of numbers所以,我有一个数字列表

weird_list = [800153196,
             946067665,
             827629917,
             868941741,
             875745873,
             926109150,
             1353347195,
             1003235074,
             1053666891,
             1442194993,
             1924716858,
             1060724069,
             1182240731,
             1646547575,
             1215762661,
             1520107722,
             1512568609,
             1534064291,
             1549459216,
             1773697582,
             1853820087,
             1696910852,
             1563415785,
             1692314635,
             1627783113]

My goal is to obtain a 2d np.array of sum of each pair of this list.我的目标是获得此列表中每对总和的 2d np.array

Example:例子:

weird_list = [1, 2, 3]
resulting_array = [[0, 1, 2],
                   [1, 2, 3],
                   [2, 3, 4]]

I wrote this function which works just fine for smaller numbers, but not necessary, because I tested on arrays with bigger numbers.我写了这个 function,它适用于较小的数字,但不是必需的,因为我在 arrays 上用更大的数字进行了测试。 The problem for this array it's that I am obtaining somehow negative numbers .这个数组的问题是我以某种方式获得了负数

def array_sum(i_list):
    i_array = np.array(i_list) # converts list to np.array
    i_array_rs1 = i_array.reshape(1, -1) 
    i_array_rs2 = i_array.reshape(-1, 1)
    i_array_rs_SUM = i_array_rs1 + i_array_rs2
    print(i_array_rs_SUM) # prints the sum of reshaped arrays, which doesn't work for my input list

I also wrote tried for some examples with lists of smaller/bigger numbers我还尝试了一些带有较小/较大数字列表的示例

low_list = [0, 1, 2, 3]
ex_list = []
weird_list_div_10 = []
weird_list_mult_10 = []

for i in range(len(weird_list)):
    ex_list.append(i)
    weird_list_div_10.append(weird_list[i] // 10)
    weird_list_mult_10.append(weird_list[i] * 10)

Source for full code: https://pastebin.com/pKpgEtUm完整代码来源: https://pastebin.com/pKpgEtUm

import numpy as np

weird_list = [ 926109150, 1353347195, 1003235074 ]             

arr = np.array( weird_list, dtype = np.int32 )   # int32 forced here

arr.reshape( 1, -1 ) + arr.reshape( -1, 1 ) 

# Out[12]:      # int32 leads to some negative answers.
# array([[ 1852218300, -2015510951,  1929344224],
#        [-2015510951, -1588272906, -1938385027],
#        [ 1929344224, -1938385027,  2006470148]], dtype=int32)

2**31-1
# Out[14]: 2147483647  # Any number greater than this in int32 will be truncated to it's 
# 32 lowest bits.  if bit 31 ( counting bits 0 to 31 ) is 1 it's treated as a negative number.

arr = np.array( weird_list, dtype = np.int64 )   # int64  forced.

arr.reshape( 1, -1 ) + arr.reshape( -1, 1 ) 

# Out[16]:   # int64 doesn't overflow therefore all results are positive with these values
# array([[1852218300, 2279456345, 1929344224],
#        [2279456345, 2706694390, 2356582269],
#        [1929344224, 2356582269, 2006470148]])

With the values in the original weird_list uint32 would work to.使用原始怪异列表中的值 uint32 可以。 The top bit doesn't indicate the sign for unsigned integers.最高位不表示无符号整数的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM