[英]Issue replacing values in numpy array
I am trying to copy an array, replace all values in the copy below a threshold but keep the original array in tact. 我正在尝试复制数组,将副本中的所有值替换为低于阈值,但保持原始数组完好无损。
Here is a simplified example of what I need to do. 这是我需要做的简化示例。
import numpy as np
A = np.arange(0,1,.1)
B = A
B[B<.3] = np.nan
print ('A =', A)
print ('B =', B)
Which yields 哪个产量
A = [ nan nan nan 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
B = [ nan nan nan 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
I can't understand why the values in A <= .3 are also overwritten? 我不明白为什么A <= .3中的值也会被覆盖?
Can someone explain this to me and suggest a work around? 有人可以向我解释一下并提出解决方法吗?
Change B = A
to B = A.copy()
and this should work as expected. 将B = A
更改为B = A.copy()
,这应该可以正常工作。 As written, B
and A
refer to the same object in memory. 如所写, B
和A
引用内存中的同一对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.