Skip to main content

All 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, ...
HonzaB's user avatar
  • 7,375
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 ...
cryptomanic's user avatar
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?
flxb's user avatar
  • 4,545
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....
Yue Harriet Huang's user avatar
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'>" ...
Pumpkin C's user avatar
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 ...
CodyBugstein's user avatar
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?
M.Y. Babt's user avatar
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], ...
poeticcapybara's user avatar
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 ...
user1871528's user avatar
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 ...
Zanam's user avatar
  • 4,827
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,...
Håkon Hægland's user avatar
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 ...
user2256085's user avatar
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], # -> -&...
kmario23's user avatar
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]])
G F's user avatar
  • 341
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?
aretor's user avatar
  • 2,569

153050per page
close