如何使用 NumPy.recarray 的两个视图修改它
我是 Python 和 Numpy 的新手,我面临一个问题,即在应用于屏蔽视图时无法修改 numpy.recarray。 我从文件中读取记录,然后创建两个屏蔽视图,然后尝试修改 for 循环中的值。 这是一个示例代码。
import numpy as np
import matplotlib.mlab as mlab
dat = mlab.csv2rec(args[0], delimiter=' ')
m_Obsr = dat.is_observed == 1
m_ZeroScale = dat[m_Obsr].scale_mean < 0.01
for d in dat[m_Obsr][m_ZeroScale]:
d.scale_mean = 1.0
但是当我打印结果时
newFile = args[0] + ".no-zero-scale"
mlab.rec2csv(dat[m_Obsr][m_ZeroScale], newFile, delimiter=' ')
,文件中的所有scale_means仍然为零。
我一定做错了什么。 是否有修改值的正确方法 看法? 是因为我将两种观点一一应用吗?
谢谢。
I am new to Python and Numpy, and I am facing a problem, that I can not modify a numpy.recarray, when applying to masked views. I read recarray from a file, then create two masked views, then try to modify the values in for loop. Here is an example code.
import numpy as np
import matplotlib.mlab as mlab
dat = mlab.csv2rec(args[0], delimiter=' ')
m_Obsr = dat.is_observed == 1
m_ZeroScale = dat[m_Obsr].scale_mean < 0.01
for d in dat[m_Obsr][m_ZeroScale]:
d.scale_mean = 1.0
But when I print the result
newFile = args[0] + ".no-zero-scale"
mlab.rec2csv(dat[m_Obsr][m_ZeroScale], newFile, delimiter=' ')
All the scale_means in the files, are still zero.
I must be doing something wrong. Is there a proper way of modifying values of the
view? Is it because I am applying two views one by one?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您对“屏蔽视图”这个术语有误解,应该(重新)阅读这本书(现在可免费下载)以澄清您的理解。
我引用3.4.2节:
您在这里所做的是高级选择(布尔类型),因此您将获得一个副本,并且永远不会将其绑定到任何地方 - 您在副本上进行更改,然后让它消失,然后编写一个新的副本从原来的。
一旦您了解了问题,解决方案就应该很简单:制作一次副本,对该副本进行更改,然后编写相同的副本。 IE:
I think you have a misconception in this term "masked views" and should (re-)read The Book (now freely downloadable) to clarify your understanding.
I quote from section 3.4.2:
What you're doing here is advanced selection (of the Boolean kind) so you're getting a copy and never binding it anywhere -- you make your changes on the copy and then just let it go away, then write a new fresh copy from the original.
Once you understand the issue the solution should be simple: make your copy once, make your changes on that copy, and write that same copy. I.e.: