Skip to main content

All Questions

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....
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?
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], ...
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, ...
4votes
2answers
121views

Optimal multiplication of two 3D arrays having a variable dimension

I would like to multiply tensors R = {R_1, R_2, ..., R_M} and X = {X_1, X_2, ..., X_M} where R_i and X_i are 3×3 and 3×N_i matrices, respectively. How can I make maximum use of NumPy functionalities ...
0votes
1answer
521views

How to understand transpose operation on a 3D or Higher Dimensional Array

Consider a 3D array array_3d with shape (3, 2, 2): array_3d = np.array([ [[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]] ]) array_3d.shape = (3, 2, 2) ...
2votes
3answers
86views

Numpy slicing on a zero-padded 2D array

Given a 2D Numpy array, I'd like to be able to pad it on the left, right, top, bottom side, like the following pseudo-code. Is there anything like this already built into Numpy? import numpy as np a =...
0votes
1answer
64views

NumPy ndarray non contiguous memory layout

I need some help in understanding the equations involved in finding the index of an element in an ndarray. I have been reading the Book "Guide to NumPy" by Travis A Oliphant. Link to the ...
0votes
1answer
74views

Fix this numpy-function so it accepts multidimensional inputs

I am trying to compute the following function: geo-decay-function: in numpy, has to be vectorized. The catch in this case is that it should work for multidimensional inputs. E.g., x having dimensions ...
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 ...
0votes
2answers
84views

Finding the maximum value in a specific axis of a 3D array for each group of values on the axis

I have a 3D numpy array of shape (8, x, y) where x and y can be any integer value from 0 to ~500. These 8 "layers" of shape (x, y) each represent a possible z value at a specific point on a ...
1vote
1answer
130views

How does ndarray.__new__ know from where it is being called?

In the numpy documentation of subclassing ndarray here. It is said that ndarray.__new__, passes __array_finalize__ the new object, of our own class (self) as well as the object from which the view ...
0votes
1answer
32views

Rewriting numpy function to handle 2d and 3d inputs

I am trying to rewrite an numpy function such that it can deal with 2d and 3d inputs. Consider the following code: import numpy as np def adstock_geometric(x: np.array, theta: np.array): x_decayed ...
0votes
1answer
126views

Mean absolute value along all columns of a 3D Numpy array

I always get find myself in state of confusion when dealing with a multi-dimensional array. Imaging having the following array of arrays, where each array contains feature importance scores (3-...
0votes
1answer
97views

Numpy comparing nested arrays

I am considering arrays of shape (n,2) for arbitrary n. I want to check whether the two-element sub-arrays match. As an example: import numpy as np a=np.array([[1,0],[2,0]]) b=np.array([[1,0],[2,0]]) ...

153050per page
close