Im trying to iterate over a Numpy Array that contains 3d numpy arrays (3d vectors) inside it.
Something like this:
import numpy as np
Matrix = np.zeros(shape=(10, 3))
# => [
[0,0,0],
[0,0,0],
...
[0,0,0]
]
I need to iterate over it, getting each 3d Vector.
In pseudo code:
for vector in Matrix
print vector #=> [0,0,0]
Is there any Numpy native way of doing this?
What is the fastest way of doing this?
Thanks!
Fran
Your pseudocode is only missing a colon:
for vector in matrix:
print vector
That said, you will generally want to avoid explicit iteration over a NumPy array. Take advantage of broadcasted operations and NumPy built-in functions as much as possible; it moves the loops into C instead of interpreted Python, and it tends to produce shorter code, too.
Related
So suppose I have a 2by2 numpy array. I want to create another 2 by 2 numpy array so that the elements will each be the previous 2by2 array, without using an explicit for loop. How can I achieve this? The shape of the new numpy matrix should be (2,2,2,2)
This helps you copy the numpy matrix.
But I really did not understand your point
import numpy as np
a = np.matrix('1,2; 3,2; 3,2')
b = a.copy()
I am very new to learning python and I am trying to scale a matrix using library np. array n x m.
the question : if a matrix with using library np.array is given as input and I don't know how big the range the matrix, how can I initialize the size of m? Are there certain features or tricks in Python that can be used for this?
import numpy as np
def scaleArray(arr: np.array);
arrayB = np.array([[1,2,4],
[3,4,5],
[2,1,0],
[0,1,0]])
scaleArray(b)
This arrayB is just for example.
Expected output :
3
arr.shape is what you are looking for, it gives you the dimensions of the nD array.
In your case, you want arr.shape[1]
Sorry for this simple question, but I can't find how to figure it out :
I have a long 1D numpy array like:
[1,2,3,4,5,6,7,8,9,10,11,12, ... ,n1,n2,n3]
this array is used to store x y z position of points, like [x0,y0,z0,x1,y1,z1 etc.... ]
I would like to convert it to this form :
[ [1,2,3],[4,5,6],[7,8,9],[10,11,12],....,[n1,n2,n3] ]
It it possible with numpy without going through slow for loops ?
Thanks :)
Use the reshape method.
a = np.arange(27) # some 1-D numpy array
a.reshape(-1, 3)
Matlab Code:
AP(queryIdx) = diff([0;recall]')*prec
My python code:
AP[queryIdx] = np.dot(np.diff(np.concatenate(([[0]], recall), axis=0).transpose()),prec)
Variables:(Checked and am quite sure they are equivalent in python and in Matlab)
Recall: 1000x1 np array*
prec: 1000x1 np array
* prints out as [[.],.....,[.]]
Results:
Matlab: .1011
Python: 0.05263158
Only cause I can think of outside of the code is that python uses more
precision, but I doubt that would make such a large difference)
*Edit There was a problem with my prec variable. The above code worked
That code looks a bit messy. Try replacing it with this:
AP[queryIdx] = np.dot(np.diff(np.hstack([0, recall.ravel()])), prec.ravel())
In your post, you mentioned that you have a 1000 x 1 array for both recall and prec. This to me is interpreted as a 2D array with a singleton dimension: the second dimension. As such, you'd need to convert this back to a 1D array using ravel.
Now, np.hstack horizontally stacks 1D arrays together and so this will append a 0 at the front, then apply the diff operator, and the perform the dot product with prec.
One common gotcha that MATLAB coders have with numpy is the representation of 1D arrays in numpy. There is no such thing as the transpose of a 1D array. All numpy 1D arrays are row vectors. If you explicitly want to make the 1D array a column vector, you need to include an additional dimension and make the second dimension 1, then transpose it. Something like this:
r = v[:][None].T
In any case, let's verify the results:
MATLAB
>> recall = (1:1000).';
>> prec = (1000:-1:1).';
>> diff([0; recall].')*prec
ans =
500500
Python (IPython)
In [1]: import numpy as np
In [2]: recall = np.arange(1,1001)
In [3]: prec = np.arange(1000,0,-1)
In [4]: np.dot(np.diff(np.hstack([0, recall.ravel()])), prec.ravel())
Out[4]: 500500
I have a 2-D array of values and need to mask certain elements of that array (with indices taken from a list of ~ 100k tuple-pairs) before drawing random samples from the remaining elements without replacement.
I need something that is both quite fast/efficient (hopefully avoiding for loops) and has a small memory footprint because in practice the master array is ~ 20000 x 20000.
For now I'd be content with something like (for illustration):
xys=[(1,2),(3,4),(6,9),(7,3)]
gxx,gyy=numpy.mgrid[0:100,0:100]
mask = numpy.where((gxx,gyy) not in set(xys)) # The bit I can't get right
# Now sample the masked array
draws=numpy.random.choice(master_array[mask].flatten(),size=40,replace=False)
Fortunately for now I don't need the x,y coordinates of the drawn fluxes - but bonus points if you know an efficient way to do this all in one step (i.e. it would be acceptable for me to identify those coordinates first and then use them to fetch the corresponding master_array values; the illustration above is a shortcut).
Thanks!
Linked questions:
Numpy mask based on if a value is in some other list
Mask numpy array based on index
Implementation of numpy in1d for 2D arrays?
You can do it efficently using sparse coo matrix
from scipy import sparse
xys=[(1,2),(3,4),(6,9),(7,3)]
coords = zip(*xys)
mask = sparse.coo_matrix((numpy.ones(len(coords[0])), coords ), shape= master_array.shape, dtype=bool)
draws=numpy.random.choice( master_array[~mask.toarray()].flatten(), size=10)