I want to apply a transformation to certain columns of a record array and reassign these values. What is the canonical way to do this?
List = [['a',.3,.3],
['b',-.5,.4]]
Arr = np.rec.fromrecords(List,names=['id','var1','var2'])
And I want to apply some scaling to some variables. I’ll let this be an example but in general the scaling may be more complex than subtracting means.
scale = lambda x: x - x.mean(0)
This does not work
Arr[['var1','var2']].mean(0)
TypeError: cannot perform reduce with flexible type
So I have to convert to an unstructured array first, but then how to reassign back to the record array? Do I loop?
Unstr = Arr[['var1','var2']].view('float').reshape(len(Arr),-1)
Arr[['var1','var2']] = scale(Unstr)
IndexError: unsupported iterator index
Why don’t you scale variables one by one?