我正在尝试将零添加到所需索引的数组中。
newerror = []
for i in range(40):
temp = []
for j in range(20):
output = numpy.zeros((196)).astype(numpy.float64) # 196*1
x=index2[i][j].astype(int) # 40*20*49
# filling 49 elements into 196 elements
# at desired indices
y=error[i][j] # 40*20*49
for (ind, rep) in zip(x, y):
output[ind] = rep
temp.append(output)
newerror.append(temp) #40*20*196
我使用上面的代码实现了它。 对于每个输入数组:输出数组中的零将替换为所需索引处的输入值。
但我的结果如下所示:在没有要求的情况下,随机数组显示科学记数法。
(输出的一部分;总输出尺寸:40 * 20 * 196)
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
7.69098301e-02 0.00000000e+00 0.00000000e+00 1.51987125e-01
0.00000000e+00 8.44965872e-02 0.00000000e+00 -1.59399264e-04
1.44000233e-01 0.00000000e+00 1.07718190e-01 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 -3.70872988e-02
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
9.31264783e-02 0.00000000e+00 0.00000000e+00 0.00000000e+00
-5.27716619e-02 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 -8.66752459e-02 0.00000000e+00
1.60625907e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00
-5.99051582e-02 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 4.35048696e-02 0.00000000e+00 -4.90880002e-02
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 -9.75583410e-03 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 -5.85866895e-02
-4.12372907e-02 0.00000000e+00 3.39738431e-02 0.00000000e+00
0.00000000e+00 -3.28913870e-02 0.00000000e+00 0.00000000e+00
0.00000000e+00 -6.56889122e-02 0.00000000e+00 1.12313472e-01
0.00000000e+00 0.00000000e+00 -1.28921454e-02 0.00000000e+00
2.64238752e-02 0.00000000e+00 0.00000000e+00 -3.83728496e-02
0.00000000e+00 0.00000000e+00 0.00000000e+00 5.22824327e-03
0.00000000e+00 0.00000000e+00 1.60543359e-01 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 7.20355685e-02 0.00000000e+00
我认为'这会影响我后期的代码,我无法分享。有什么想法吗?
答案 0 :(得分:1)
如果数组中的值范围足够宽,特别是如果某些值足够小,numpy
会以科学记数法显示整个事物:
In [410]: input = [0.03302, 0.232323, 0.22222, 0.232344]
In [411]: np.array(input)
Out[411]: array([ 0.03302 , 0.232323, 0.22222 , 0.232344])
In [412]: input[2] *= 1e-6
In [413]: input
Out[413]: [0.03302, 0.232323, 2.2222e-07, 0.232344]
此列表中的一项小到足以使用科学记数法
In [414]: np.array(input)
Out[414]:
array([ 3.30200000e-02, 2.32323000e-01, 2.22220000e-07,
2.32344000e-01])
等效数组的所有元素以相同的方式显示。
zip
步骤与此无关(您不需要迭代进行分配):
In [415]: arr = np.zeros(10)
In [416]: arr.dtype
Out[416]: dtype('float64')
In [417]: index= [0, 2, 6, 7]
In [418]: arr[index] = input
In [419]: arr
Out[419]:
array([ 3.30200000e-02, 0.00000000e+00, 2.32323000e-01,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
2.22220000e-07, 2.32344000e-01, 0.00000000e+00,
0.00000000e+00])
显示屏中的科学记数法不应该在以后的计算中产生问题。它只是一个显示问题,并不会影响数值。
来自科学记数法行的复制粘贴:
In [420]: alist =[ 0.00000000e+00, -1.88939223e-01, -1.99170532e-01, 0.0000
...: 0000e+00, 0.00000000e+00, 3.66637802e-02, 0.00000000e+00, 8.0379
...: 8680e-02, -1.46634700e-01, 0.00000000e+00]
In [421]: alist
Out[421]:
[0.0,
-0.188939223,
-0.199170532,
0.0,
0.0,
0.0366637802,
0.0,
0.080379868,
-0.1466347,
0.0]
In [422]: np.array(alist)
Out[422]:
array([ 0. , -0.18893922, -0.19917053, 0. , 0. ,
0.03666378, 0. , 0.08037987, -0.1466347 , 0. ])
在您的最新展示中,-1.59399264e-04
足够小。