## How to find numpy array shape in a larger array?

big_array = np.array(( [0,1,0,0,1,0,0,1], [0,1,0,0,0,0,0,0], [0,1,0,0,1,0,0,0], [0,0,0,0,1,0,0,0], [1,0,0,0,1,0,0,0])) print(big_array) [[0 1 0 0 1 0 0 1] [0 1 0 0 0 0 0 0] [0 1 0 0 1 0 0 0] [0 0 0 0 1 0 0 0] [1 0 0 0 1 0 0 0]]

Is there a way to iterate over this numpy array and for each 2x2 cluster of 0s, set all values within that cluster = 5? This is what the output would look like.

[[0 1 5 5 1 5 5 1] [0 1 5 5 0 5 5 0] [0 1 5 5 1 5 5 0] [0 0 5 5 1 5 5 0] [1 0 5 5 1 5 5 0]]

My thoughts are to use advanced indexing to set the 2x2 shape = to 5, but I think it would be really slow to simply iterate like: 1) check if array[x][y] is 0 2) check if adjacent array elements are 0 3) if all elements are 0, set all those values to 5.

big_array = [1, 7, 0, 0, 3] i = 0 p = 0 while i <= len(big_array) - 1 and p <= len(big_array) - 2: if big_array[i] == big_array[p + 1]: big_array[i] = 5 big_array[p + 1] = 5 print(big_array) i = i + 1 p = p + 1

Output: [1, 7, 5, 5, 3]

It is a example, not whole correct code.

**How to find numpy array shape in a larger array?,** How to find numpy array shape in a larger array? numpy reshape numpy array append numpy array to list numpy 2d array numpy array size numpy ndarray numpy The Python Numpy module has one crucial property called shape. The Python array shape property is to get or find the shape of an array. import numpy as np arr = np.array ([10, 20, 30, 40, 50, 60, 70, 80]) print (arr) print ('Array Shape = ', np.shape (arr))

Here's a solution by viewing the array as blocks.

First you need to define this function `rolling_window`

from here https://gist.github.com/seberg/3866040/revisions

Then break the array `big`

, your starting array, into 2x2 blocks using this function.
Also generate an array which has indices of every element in big and break it similarly into 2x2 blocks.

Then generate a boolean mask where the 2x2 blocks of big are all zero, and use the index array to get those elements.

blks = rolling_window(big,window=(2,2)) # 2x2 blocks of original array inds = np.indices(big.shape).transpose(1,2,0) # array of indices into big blkinds = rolling_window(inds,window=(2,2,0)).transpose(0,1,4,3,2) # 2x2 blocks of indices into big mask = blks == np.zeros((2,2)) # generate a mask of every 2x2 block which is all zero mask = mask.reshape(*mask.shape[:-2],-1).all(-1) # still generating the mask # now blks[mask] is every block which is zero.. # but you actually want the original indices in the array 'big' instead inds = blkinds[mask].reshape(-1,2).T # indices into big where elements need replacing big[inds[0],inds[1]] = 5 #reassign

You need to test this: I did not. But the idea is to break the array into blocks, and an array of indices into blocks, then develop a boolean condition on the blocks, use those to get the indices, and then reassign.

An alternative would be to iterate through `indblks`

as defined here, then test the 2x2 obtained from big at each indblk element and reassign if necessary.

**The Basics of NumPy Arrays,** Data manipulation in Python is nearly synonymous with NumPy array manipulation: even newer tools Get to know them well! and setting smaller subarrays within a larger array; Reshaping of arrays: Changing the shape of a given array Shape of numpy.ndarray: shape. The shape (= size of each dimension) of numpy.ndarray can be obtained as a tuple with attribute shape.. Even in the case of a one-dimensional array, it is a tuple with one element instead of an integer value.

This is my attempt to help you solve your problem. My solution may be subject to fair criticism.

import numpy as np from itertools import product m = np.array(( [0,1,0,0,1,0,0,1], [0,1,0,0,0,0,0,0], [0,1,0,0,1,0,0,0], [0,0,0,0,1,0,0,0], [1,0,0,0,1,0,0,0])) h = 2 w = 2 rr, cc = tuple(d + 1 - q for d, q in zip(m.shape, (h, w))) slices = [(slice(r, r + h), slice(c, c + w)) for r, c in product(range(rr), range(cc)) if not m[r:r + h, c:c + w].any()] for s in slices: m[s] = 5 print(m) [[0 1 5 5 1 5 5 1] [0 1 5 5 0 5 5 5] [0 1 5 5 1 5 5 5] [0 5 5 5 1 5 5 5] [1 5 5 5 1 5 5 5]]

**The N-dimensional array (ndarray),** The number of dimensions and items in an array is defined by its shape You can check whether this option was enabled when your NumPy was built by To avoid overflow, it can be useful to perform the reduction using a larger data type. To get the shape or dimensions of a Numpy Array, use ndarray. shape where ndarray is the name of the numpy array you are interested of. ndarray.shape returns a tuple with dimensions along all the axis of the numpy array. Example 1: Get Shape of Multi-Dimensional Numpy Array

**Array manipulation routines,** self.transpose(), except that self is returned if self.ndim < 2. transpose (a[, axes]), Permute the dimensions of an array. Find the unique elements of an array. If we check the shape of reshaped numpy array, we’ll find tuple (2, 5) which is a new shape of numpy array. Here first element of tuple is number of rows and second is number of columns. Python numpy reshape() Method Reshaping numpy array (vector to matrix)

**numpy: Array shapes and reshaping arrays,** In numpy the shape of an array is described the number of rows, columns, and Now let's get a little more complicated and create a larger array that we will To find the maximum and minimum value in an array you can use numpy argmax and argmin function These two functions (argmax and argmin) returns the indices of the maximum value along an axis However, if you are interested to find out N smallest or largest elements in an array then you can use numpy partition and argpartition functions

**NumPy Array Shape,** Get the Shape of an Array. NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements. The reshape function has two required inputs. First, an array. Second, a shape. Remember numpy array shapes are in the form of tuples. For example, a shape tuple for an array with two rows and three columns would look like this: (2, 3).