numpy-slicing

np.sum gives a value that is higher than possible

np.sum gives a value that is higher than possible Question: I am trying to find the average red value of pixels in an image. The code I have written is as follows: path = "C:\Users\Ariana\Pictures\img.jpg" img = PIL.Image.open(path) imgarray = np.array(img) reds = imgarray[:, 0] avered = np.sum(reds)/len(reds) print(avered) The output should be at most …

Total answers: 1

Slice a multidimensional pytorch tensor based on values in other tensors

Slice a multidimensional pytorch tensor based on values in other tensors Question: I have 4 PyTorch tensors: data of shape (l, m, n) a of shape (k,) and datatype long b of shape (k,) and datatype long c of shape (k,) and datatype long I want to slice the tensor data such that it picks …

Total answers: 2

Assigning Values to Specific Rows and Columns of Numpy Array at the Same Time

Assigning Values to Specific Rows and Columns of Numpy Array at the Same Time Question: My Expected Output for Code below: array([[ 0, 1, 2, 3], [ 4, 999, 999, 7], [ 8, 999, 999, 11], [12, 13, 14, 15]]) Code: import numpy as np A = np.array(range(16)).reshape((4,4)) A[[1,3],:][:, [1,3]] = [[999,999],[999,999]] print(A) However, the …

Total answers: 2

Slicing numpy arrays in a for loop

Slicing numpy arrays in a for loop Question: I have a multidimentional numpy array of elasticities with one of the dimensions being "age-groups" (below/above 18 years) and the other "income-groups" (low/high income). I would like to create a table with the mean elasticities for each combination of subgroups using a for loop. My code is …

Total answers: 2

What is a vectorized way to perform a sliding window

What is a vectorized way to perform a sliding window Question: I have a nested for loop function. For each index i and j of a 2D matrix, it sums all the elements of a 2D slice of a 2D array, as in sum(data[i-1:i+1,j-1+i+1])). import numpy as np data=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) # This is to specify at …

Total answers: 1

Combine by-integer and by-boolean numpy slicing

Combine by-integer and by-boolean numpy slicing Question: I’m looking for a way to combine two index arrays b and i (one of type boolean, one of type integer) to slice another array x. x = np.array([5.5, 6.6, 3.3, 7.7, 8.8]) i = np.array([1, 4]) b = np.array([True, True, False, False, False]) The resulting array should …

Total answers: 1

How can I get a sublist with wraparound in Python

How can I get a sublist with wraparound in Python Question: Simple 1D case I would like to get a substring with wraparound. str = "=Hello community of Python=" # ^^^^^ ^^^^^^^ I want this wrapped substring str[-7] > ‘P’ str[5] > ‘o’ str[-7:5] > ” Why does this slice of a sequence starting at …

Total answers: 3

Adding block-constant matrices in numpy

Adding block-constant matrices in numpy Question: Let’s say we have a n*n matrix A m*m matrix B a vector b=[b_1,…,b_m] of block sizes with b_1 + … + b_m = n and b_i >= 1. Then I define a n*n block matrix B_block whose (i,j)-th block is a b_i*b_j matrix of constant value B_{ij}. How …

Total answers: 1

Numpy Array: Slice several values at every step

Numpy Array: Slice several values at every step Question: I am trying to extract several values at once from an array but I can’t seem to find a way to do it in a one-liner in Numpy. Simply put, considering an array: a = numpy.arange(10) > array([0, 1, 2, 3, 4, 5, 6, 7, 8, …

Total answers: 2

Indexing ndarray with unknown number of dimensions with range dynamically

Indexing ndarray with unknown number of dimensions with range dynamically Question: I have data array with unknown shape and array bounds of bounds for slicing data. This code is for 3D data, but is there any way of generalizing this to N-dim? for b in bounds: l0, u0 = b[0] l1, u1 = b[1] l2, …

Total answers: 1