Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算中的不一致性-Python&;努比_Python_Python 3.x_Numpy - Fatal编程技术网

计算中的不一致性-Python&;努比

计算中的不一致性-Python&;努比,python,python-3.x,numpy,Python,Python 3.x,Numpy,我尝试运行的代码是: import numpy as np x = [np.array([[ 0, 0.66111, 0.325, 0.061111, 0.070833]], dtype=np.float32), np.array([ 2.6026], dtype=np.float32), np.array([ -84.806], dtype=np.float32)] ratio, w, h, pad = (1.0,

我尝试运行的代码是:

import numpy as np
x = [np.array([[          0,     0.66111,       0.325,    0.061111,    0.070833]],        dtype=np.float32), np.array([     2.6026], dtype=np.float32), np.array([    -84.806], dtype=np.float32)]
ratio, w, h, pad  = (1.0, 1.0) ,640 ,426 ,(0.0, 107.0)

labels = x.copy()
print('labels before computation', labels[0])
print(ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0])
print(ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1])
labels[0][:, 1] = ratio[0] * w * (x[0][:, 1] - x[0][:, 3] / 2) + pad[0]  
labels[0][:, 2] = ratio[1] * h * (x[0][:, 2] - x[0][:, 4] / 2) + pad[1]  
labels[0][:, 3] = ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0]
labels[0][:, 4] = ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1]
print('labels after computation', labels[0])
输出为:

labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.6659]
[260.5374]
labels after computation [[0.0000000e+00 4.0355487e+02 2.3036258e+02 2.5829467e+05 9.8256547e+04]]
第2条和第3条语句打印的值是正确的。但是,当我试图将同一计算输出的值分别分配给标签[0][:,3]和标签[0][:,4]时,会分配一些垃圾值

如果我删除第9行和第10行中的赋值操作并运行以下代码,则输出正常

import numpy as np

x = [np.array([[          0,     0.66111,       0.325,    0.061111,    0.070833]], dtype=np.float32), np.array([     2.6026], dtype=np.float32), np.array([    -84.806], dtype=np.float32)]
ratio, w, h, pad  = (1.0, 1.0) ,640 ,426 ,(0.0, 107.0)

labels = x.copy()
print('labels before computation', labels[0])
print(ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0])
print(ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1])
labels[0][:, 3] = ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0]
labels[0][:, 4] = ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1]
print('labels after computation', labels[0])
输出:

labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.6659]
[260.5374]
labels after computation [[0.000000e+00 6.611100e-01 3.250000e-01 4.426659e+02 2.605374e+02]]
labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.66592]
[260.537429]
labels after computation [[  0.       403.55488  230.362571 442.66592  260.537429]]
有人能解释不一致背后的原因吗

我的环境:
Python版本:3.6.9
Numpy版本:1.19.4

谢谢

labels = x.copy()
您只需创建
x
的浅层副本,该副本的元素为数组,因此更改
标签[0][:,1]
标签[0][:,2]
会更改
x
中的相同字段

同时检查
复制功能的功能:

请注意,np.copy是一个浅复制,不会复制数组中的对象元素。这对于包含Python对象的数组非常重要。新数组将包含相同的对象,如果该对象可以修改(是可变的),则可能会导致意外:

解决方案是:

要确保复制对象数组中的所有元素,请使用copy.deepcopy:

就你而言:

import numpy as np
x = [np.array([[          0,     0.66111,       0.325,    0.061111,    0.070833]],        dtype=np.float32), np.array([     2.6026], dtype=np.float32), np.array([    -84.806], dtype=np.float32)]
ratio, w, h, pad  = (1.0, 1.0) ,640 ,426 ,(0.0, 107.0)
import copy
labels = copy.deepcopy(x)
print('labels before computation', labels[0])
print(ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0])
print(ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1])
labels[0][:, 1] = ratio[0] * w * (x[0][:, 1] - x[0][:, 3] / 2) + pad[0]
labels[0][:, 2] = ratio[1] * h * (x[0][:, 2] - x[0][:, 4] / 2) + pad[1]
labels[0][:, 3] = ratio[0] * w * (x[0][:, 1] + x[0][:, 3] / 2) + pad[0]
labels[0][:, 4] = ratio[1] * h * (x[0][:, 2] + x[0][:, 4] / 2) + pad[1]
print('labels after computation', labels[0])
输出:

labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.6659]
[260.5374]
labels after computation [[0.000000e+00 6.611100e-01 3.250000e-01 4.426659e+02 2.605374e+02]]
labels before computation [[0.       0.66111  0.325    0.061111 0.070833]]
[442.66592]
[260.537429]
labels after computation [[  0.       403.55488  230.362571 442.66592  260.537429]]