How to modify NumPy.recarray using two views

I am new to Python and Numpy, and I ran into the problem that I cannot change numpy.recarray when applied to masked views. I read recarray from a file, then create two masked views, and then try to change the values ​​in a for loop. Here is a sample 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 scale_means in the files are still null.

I have to do something wrong. Is there a correct way to change View values? Is it because I apply two views one after another?

Thank.

+1
source share
1 answer

, " " () "" ( ) .

3.4.2:

, , obj, , ndarray ( integer bool) ndarray ( bool). : Boolean. ( , ).

, , - ( Boolean), - , , .

, : , . :.

dat = mlab.csv2rec(args[0], delimiter=' ')
m_Obsr = dat.is_observed == 1
m_ZeroScale = dat[m_Obsr].scale_mean < 0.01
the_copy = dat[m_Obsr][m_ZeroScale]

for d in the_copy:
    d.scale_mean = 1.0

newFile = args[0] + ".no-zero-scale"
mlab.rec2csv(the_copy, newFile, delimiter=' ')
+3

Source: https://habr.com/ru/post/1773354/


All Articles