Say I have a numpy array x:
x = array([[ 3, 2, 1],
[ 3, 25, 34],
[ 33, 333, 3],
[ 43, 32, 2]])
I want to carry out the following operations without explicitly writing a for loop i.e. say a method which uses automatic in built looping;
1) Replace the 2nd column by a column of all 1 i.e.
x = array([[ 3, 1, 1],
[ 3, 1, 34],
[ 33, 1, 3],
[ 43, 1, 2]])
2) In the original array , replace 3rd column with the product of 2nd and 3rd i.e.
x = array([[ 3, 2, 1*2],
[ 3, 25, 34*25],
[ 33, 333, 3*333],
[ 43, 32, 2*32]])
3) Finally, I would like to replace the 2nd column in the original array based on a condition i.e.
x[1] = 0 if x[0] > 5 else 4
i.e. the array now looks like:
x = array([[ 3, 4, 1],
[ 3, 4, 34],
[ 33, 0, 3],
[ 43, 0, 2]])
Any suggestions ? Thanks !
1 Answer 1
The documentation on numpy is well worth reading as this is fairly basic stuff...
x[:,1]= 1x[:,2] *= x[:,1]x[:,1] = np.where( x[:,0] > 5, 0, 4 )
answered Jul 23, 2012 at 0:33
Jon Clements
143k34 gold badges254 silver badges288 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
R.Bahl
Jon Clements: Thanks for this ! I was indexing in the wrong way, something like x[:][1] .. Thanks again for this !
Sven Marnach
Number 2 can be improved by using
x[:,2] *= x[:,1] – this saves the creation of a temporary array.Jon Clements
@Sven good point - not sure why I didn't write that - thanks - edited
lang-py
whileloop and an iterator variable that is incremented with every loop. May I ask why you can't use aforloop?forloop is adding a lot of overhead. Therefore, I switched tonumpy arrayin place oflists, hopping that there will be some kind ofvectorizedsolution to this.