All Questions
167 questions
502votes
10answers
375kviews
What is the purpose of meshgrid in NumPy?
What is the purpose of np.meshgrid? I know it creates some kind of grid of coordinates for plotting, but I can't see the direct benefit of it. The official documentation gives the following example, ...
459votes
3answers
143kviews
What is the difference between flatten and ravel functions in numpy?
import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the ...
401votes
6answers
223kviews
What is the difference between ndarray and array in NumPy?
What is the difference between ndarray and array in NumPy? Where is their implementation in the NumPy source code?
393votes
5answers
516kviews
How do I use np.newaxis?
What is numpy.newaxis and when should I use it? Using it on a 1-D array x produces: >>> x array([0, 1, 2, 3]) >>> x[np.newaxis, :] array([[0, 1, 2, 3]]) >>> x[:, np....
59votes
4answers
115kviews
what does numpy ndarray shape do?
I have a simple question about the .shape function, which confused me a lot. a = np.array([1, 2, 3]) # Create a rank 1 array print(type(a)) # Prints "<class 'numpy.ndarray'>" ...
55votes
6answers
26kviews
How does the axis parameter from NumPy work?
Can someone explain exactly what the axis parameter in NumPy does? I am terribly confused. I'm trying to use the function myArray.sum(axis=num) At first I thought if the array is itself 3 ...
54votes
6answers
211kviews
Transforming a row vector into a column vector in Numpy
Let's say I have a row vector of the shape (1, 256). I want to transform it into a column vector of the shape (256, 1) instead. How would you do it in Numpy?
34votes
3answers
41kviews
How to convert a 3d numpy array to 2d
I have a 3d matrix like this np.arange(16).reshape((4,2,2)) array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]], [[12, 13], ...
23votes
7answers
11kviews
how to copy numpy array value into higher dimensions
I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is ...
11votes
4answers
9kviews
Compare a matrix against a column vector
Arrays 'A' and vector 'B' below are part of pandas dataframe. I have a large array A of form: 28 39 52 77 80 66 7 18 24 9 97 68 I have a vector B of form: 32 5 42 17 How do I compare ...
9votes
3answers
17kviews
Numpy zip function
If I have two numpy 1D arrays, for example x=np.array([1,2,3]) y=np.array([11,22,33]) How can I zip these into Numpy 2D coordinates arrays? If I do: x1,x2,x3=zip(*(x,y)) The results are of type list,...
8votes
5answers
590views
Intersect multiple 2D np arrays for determining zones
Using this small reproducible example, I've so far been unable to generate a new integer array from 3 arrays that contains unique groupings across all three input arrays. The arrays are related to ...
8votes
1answer
2kviews
Snake traversal of 2D NumPy array
I have the following 2D array: In [173]: arr Out[173]: array([[ 1, 2, 3, 4], # -> -> -> -> [ 5, 6, 7, 8], # <- <- <- <- [ 9, 10, 11, 12], # -> -&...
7votes
3answers
8kviews
How to delete a row based on a condition from a numpy array?
From the following array : test = np.array([[1,2,'a'],[4,5,6],[7,'a',9],[10,11,12]]) How can I delete the rows that contain 'a' ? Expected result : array([[ 4, 5, 6], [10, 11, 12]])
6votes
2answers
2kviews
Perform numpy exp function in-place
As in title, I need to perform numpy.exp on a very large ndarray, let's say ar, and store the result in ar itself. Can this operation be performed in-place?