Skip to main content

All Questions

373votes
8answers
195kviews

Understanding NumPy's einsum

How does np.einsum work? Given arrays A and B, their matrix multiplication followed by transpose is computed using (A @ B).T, or equivalently, using: np.einsum("ij, jk -> ki", A, B)
-3votes
1answer
46views

How to make percent chance that there will be a * in a 2D array decrease

My result I need to make the number of stars decrease with each line, but I have no idea how. import random import numpy as np import sys size = int(sys.argv[1]) mat = np.full((size, size), '\x1b[35m....
11votes
4answers
55kviews

Create arbitrary multidimensional zeros array

I need to make a multidimensional array of zeros. For two (D=2) or three (D=3) dimensions, this is easy and I'd use: a = numpy.zeros(shape=(n,n)) or a = numpy.zeros(shape=(n,n,n)) How for I for ...
0votes
3answers
4kviews

Separating a string into a 2d array using two different splits

I'm trying to input values into a CSV using a 2d array. I have a string which is separated by ',' to separate the values and ';' to separate each row. string below; text = 536924636,www.microsoft.com,...
-1votes
2answers
4kviews

Python Sort Multidimensional List first element Based on second element that already sorted [duplicate]

I have a multidimensional List like this (the second element already sorted) [[92, 25], [93, 25], [95, 27], [94, 27], [94, 27], [92, 27], [89, 27], [89, 27], [92, 27], [91, 30], [90, 30], [90, 30], [...
60votes
4answers
131kviews

Sort multidimensional array based on 2nd element of the subarray

I have an array like this: [['G', 10], ['A', 22], ['S', 1], ['P', 14], ['V', 13], ['T', 7], ['C', 0], ['I', 219]] I'd like to sort it based on the 2nd element in descending order. An ideal output ...
-3votes
1answer
67views

Why can't I stack 3D arrays with np.concatenate in numpy, while 1D and 2D arrays work?

I'm working with 3D numpy arrays and having trouble stacking two of them. Here’s what I’m trying to do: import numpy as np grid = np.arange(16).reshape((1, 4, 4)) grid2 = grid[:, :-1, ::-1].copy() I ...
38votes
6answers
82kviews

Subsetting a 2D numpy array

I have looked into documentations and also other questions here, but it seems I have not got the hang of subsetting in numpy arrays yet. I have a numpy array, and for the sake of argument, let it ...
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], ...
0votes
0answers
23views

Xarray .where() unable to parse multiple n-dimensional arrays

I'm trying to use the xr.where() function to create a binary based on multiple arrays. The code I am using is the following: binary = xr.where((snd_nc.SNOWDP >= 0.2) & (tas_nc.tas <= 0) &...
1vote
1answer
87views

How do I effectively compute pairwise quantities in numpy?

I want to compute pairwise quantities, e.g. distances between two points. A simple example would be import numpy as np N = 9 x = np.linspace(0,1,N) y = np.abs(x - x[:,None]) # pairwise 1d eucdlidian ...
1vote
1answer
111views

algorithm to detect pools of 0s in a matrix

I'm writing an algorithm that detects regions of contiguous empty cells that are completely surrounded (orthogonally) by filled cells and do not extend to the edge of the grid. Let's call such regions ...
-3votes
1answer
103views

How to Slice Multi Dimensional Array?

Dipping my toes into multi dimensional array slicing in Python and am hitting a wall of confusion with the following code # Example 2D array matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] # ...
1vote
2answers
740views

ValueError: could not broadcast input array from shape A into shape B

I used Python with Numpy to create an array filled with numbers from a tuple, and set it up to be a 4x4 matrix. numbers = (-8, -3, -5, 0, 4, 5, 6, 10, 7,8, 9, 100, 10, 11, 12, 1000) numbers_array = ...

153050per page
close