python - Numpy multiply multiple columns by scalar -


this seems simple question can't find answer anywhere. how might multiply (in place) select columns (perhaps selected list) scalar using numpy?

e.g. multiply columns 0 , 2 4

in:  arr=([(1,2,3,5,6,7), (4,5,6,2,5,3), (7,8,9,2,5,9)]) out: arr=([(4,2,12,5,6,7), (16,5,24,2,5,3), (28,8,36,2,5,9)]) 

currently doing in multiple steps feel there must better way if list gets larger. current way:

arr['f0'] *= 4 arr['f2'] *= 4 

you can use array slicing follows -

in [10]: arr=([(1,2,3,5,6,7), (4,5,6,2,5,3), (7,8,9,2,5,9)])  in [11]: narr = np.array(arr)  in [13]: narr[:,(0,2)] = narr[:,(0,2)]*4  in [14]: narr out[14]: array([[ 4,  2, 12,  5,  6,  7],        [16,  5, 24,  2,  5,  3],        [28,  8, 36,  2,  5,  9]]) 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -