numpy-slicing

How to add a 4X4 matrix values into a 6×6 matrix using numpy

How to add a 4X4 matrix values into a 6×6 matrix using numpy Question: suppose i have multiple 4×4 matrices which i want to add to a final 6×6 zero matrix by adding some of the values in the designated coordination. how would i do this. I throughout of adding slices to np.zero 6×6 matrix …

Total answers: 3

Iterate over inner axes of an array

Iterate over inner axes of an array Question: I want to iterate over some inner dimensions of an array without knowing in advance how many dimensions to iterate over. Furthermore I only know that the last two dimensions should not be iterated over. For example assume the array has dimension 5 and shape (i,j,k,l,m) and …

Total answers: 1

How to "spread" a numpy array (opposite of slice with step size)?

How to "spread" a numpy array (opposite of slice with step size)? Question: Is there a way to spread the values of a numpy array? Like an opposite to slicing with a step size > 1: >>> a = np.array([[1, 0, 2], [0, 0, 0], [3, 0, 4]]) >>> a array([[1, 0, 2], [0, 0, …

Total answers: 1

Printing columns of a list of arrays

Printing columns of a list of arrays Question: I have the following list import numpy as np Y = [np.array([[1, 4, 7], [2, 5, 8]]), np.array([[10, 14, 18], [11, 15, 19], [12, 16, 20], [13, 17, 21]]), np.array([[22, 26, 31], [24, 28, 33], [26, 30, 35]])] I want to loop through and print the columns …

Total answers: 3

Unexpected results from Numpy r_

Unexpected results from Numpy r_ Question: When I use ":n" or "m:" as arguments to np.r_, I get unexpected results that I don’t understand. Here’s my code import numpy as np B = np.arange(180).reshape(6,30) C = B[:, np.r_[10:15, 20:26]] D = C[:, np.r_[0:3,8:11]] Now all of that worked as expected. C prints as: array([[ 10, …

Total answers: 2

Get each item while slicing (:) a numpy matrix

Get each item while slicing (:) a numpy matrix Question: I have been looking for a solution to this for a while. I am trying to use as few loops in my code as possible, so I’ve been trying to set slices of the numpy arrays instead. I have a large array of size (s, …

Total answers: 1

Slicing every second row and column of a 3d numpy array, making a new array

Slicing every second row and column of a 3d numpy array, making a new array Question: I have a NumPy array of dimension 698x64x64 (64rows, 64columns) I would like to slice it into a new array of 698x32x32 by taking every second row and column of the original array. How do I do that? Asked …

Total answers: 1

Downsampling 3D array with numpy

Downsampling 3D array with numpy Question: Given array: A = array([[[1, 2, 3, 1], [4, 5, 6, 2], [7, 8, 9, 3]]]) I obtain the following array in the forward pass with a downsampling factor of k-1: k = 3 B = A[…,::k] #output array([[[1, 1], [4, 2], [7, 3]]]) In the backward pass I …

Total answers: 1

Upsampling using Numpy

Upsampling using Numpy Question: I want to upsample a given 1d array by adding ‘k-1’ zeros between the elements for a given upsampling factor ‘k’. k=2 A = np.array([1,2,3,4,5]) B = np.insert(A,np.arange(1,len(A)), values=np.zeros(k-1)) The Above code works for k=2. Output: [1 0 2 0 3 0 4 0 5] k=3 A = np.array([1,2,3,4,5]) B = …

Total answers: 2