numpy-ndarray

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

Moving Window Calculation Across Multiple Arrays

Moving Window Calculation Across Multiple Arrays Question: I have several two-dimensional data arrays loaded into NumPy arrays, all of which have identical dimensions. These shared dimensions are 2606x and 1228y. I am interested in computing calculations between the first two arrays (a1 & a2) across a moving window, using a window sized 2x by 2y, …

Total answers: 1

Large Numpy array causses Error when trying to load

Large Numpy array causses Error when trying to load Question: Edit : For those looking for answers, my file was corrupted hpauli suggested, knowing the shape the array should have, I opened the file using open(filenam) as append mode and appended 0 until there was a correct amount of data, then I split the files …

Total answers: 1

How do I store objects I created in np.array if a __getattr__ exists?

How do I store objects I created in np.array if a __getattr__ exists? Question: I created a Pixel class for image processing (and learn how to build a class). A full image is then a 2D numpy.array of Pixel but when I added a __getattr__ method , it stopped to work, because numpy wants an …

Total answers: 2

Extract a row from a Python numpy array by condition

Extract a row from a Python numpy array by condition Question: I have an array (called "attractors") which looks like this: [[‘0000000000’ ‘0.0’ ‘0.0’] [‘0000000001’ ‘0.0’ ‘1.0’] [‘0000000010’ ‘0.0’ ‘2.0’] ……………………… I want to create new array which contains all rows where the third column was 0 in the original array. I try the following: …

Total answers: 2

Group elements in ndarray by index

Group elements in ndarray by index Question: I have an image dataset of a 1000 images, which I have created embeddings for. Each embeddings (512 embeddings for each image with a 256-d vector) is an ndarray of shape (512, 256), so the total array shape would be (1000, 512, 256). Now, from each image (1000), …

Total answers: 1

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

Replace values in a dimension of a 2D numpy array, from a map dictionary

Replace values in a dimension of a 2D numpy array, from a map dictionary Question: I need to replace the values of a certain index in all lists inside a list, with a value from a dictionary, which is mapped for a different index of that list. Or, if the first index value of every …

Total answers: 3