Suppose I have a = np.arange(16).reshape(4,4), which is
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
And want to slice a as I could with a[0:3,1:4], which results in
array([[ 1, 2, 3],
…
I want to slice the same numpy array (data_arra) multiple times to find each time the values in a different range
data_ar shpe: (203,)
range_ar shape: (1000,)
I implemented it with a for loop, but it takes way to long since I have a lot of…
I am generating a numpy array with 1000000 random numbers between 1 and 6 and i would like to calculate the mean of the first 10, 100, 1000, ... I also want to plot the means on logarithmic scale. I mustn't use anything but Python with numpy and…
I am looking for a way to select multiple rows from a numpy array multiple times given an array of indexes.
Given M and indexes, I would like to get N avoiding for loop, since it is slow for big dimensions.
import numpy as np
M = np.array([[1, 0, 1,…
a = np.array([2,3,1,4])
b = np.array([2,3,7,1])
c = np.zeros((4, 10))
I wanna assign value 1 to some elements in c. a and b define the positions of such elements. a is the starting column indices of value 1 in each row. And b represents how many…
In the script below, I want to apply a rotation matrix to the first two columns of a (Nx3) array.
rotate_mat = lambda theta: np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])
rot_mat = rotate_mat(np.deg2rad(90))
basis1 =…
I was wondering what is the best way to perform a batched slice (torch/numpy)?
I know for constant slicing indices it is possible to perform this operation
batch_size = 2
data = torch.zeros((batch_size,1, 256, 256))
x_start = 10
x_stop = 20
y_start…
let's assume that I have a 2-dimensional NumPy array that looks like that and i want to extract the left bottom square (4x4):
arr_2d = [[ 5,10,15],
[20,25,30],
[35,40,45]]
why there is difference between this…
I have something like this
import numpy as np
array_3D = np.random.rand(3,3,3)
array_2D = np.random.randint(0, 3 , (3,3))
for i in range(3):
for j in range(3):
array_3D[:, i, j][:array_2D[i, j]]=np.nan
Is there a way to do this…
How can I slice a numpy array beyond its shape such that values in the array are repeated without having to store the entire array in memory? Here's what I'd like to do:
x = numpy.array([[1, 2], [3, 4]])
x[0:3, 0:3]
->
[[1, 2, 1, 2],
[3, 4, 3, 4],
…
I want to take a basic 3d array like this:
b = np.arange(1,101).reshape(4,5,5)
b
Then I want to take the first index, and work down like a stairs.
b1 = [b[0:,0,0],b[0:,1,1],b[0:,2,2],b[0:,3,3],b[0:,4,4]]
b1 = np.asarray(b1)
b1 =…
I am trying to vectorize this method that I am using for image augmentation in ML:
def random_erase_from_image(images, random_erasing, image_size):
#could probably be vectorized to speed up
to_return = images
for t in range(images.shape[0]):
if…
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
if we run the following statement
x[1:, [2,0,1]]
we get the following result
array([[ 6, 4, 5],
[10, 8, 9]])
According to numpy's doc:
Advanced indexes always…
a = np.arange(12).reshape(3,4)
b1 = np.array([False,True,True]
b2 = np.array([True,False,True,False])
a[b1,b2]
output:
array([4,10])
I am not getting how it comes 4 and 10 in a[b1,b2]
I have an array like this: A = ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) and would like to extract every 5th "and" 6th element from this row vector, leading to B = ([4,5,9,10,14,15]).
I know how to extract every 5th element: B =…