I am trying to trace to what extent is listA, listB, listC… similar to the original list. How do I print the number of elements that occur in the same sequence in listA as they occur in the original list? Answer I wrote a function that does the job I think. It might be a bit too complex, but I
Tag: numpy
How to create a numpy 2-D array whose elements depend on elements of two lists already fixed
Example: Let and I would like to create a matrix C of size (5,5) whose elements are where f is a fixed function for example f(x,y) = x*y + x + y which means In the case where c_ij depends on i and j only and does not depend on the lists A and B, we can use np.fromfunction(lambda i,j:
Slicing a 3D image to create a 2D image
I have several 3D images of shape (32,32,32) and I want to create 2D images from them. I want to do that by getting each slice in the z-axis and putting each of them in a square array in order, something like this: Because I want the 2D image to be square I need to fill the missing slices with
Problems adding strings to 2D array
So i’m simply trying to add certain strings to a NxN array(matrix) using a for loop. First I create an empty 2D array using np.empty to later fill up with my values: When I run this code I get a correct NxN array matrixK that is however only filled with ‘a’ at all indexes instead of ‘abcd’. So basically always
Finding location of subarray in numpy array
I want to find the location of a subarray in an numpy array. For example, if the large array is of shape (10000, 10, 10, 10), namely 10000 arrays of shape (10,10,10) and I want to find the location of the subarray of shape (3,3,3) as follows: So I will know that at the location of n, i, j, k
How to efficiently loop over an image pixel by pixel in python OpenCV?
What I want to do is to loop over an image pixel by pixel using each pixel value to draw a circle in another corresponding image. My approach is as follows: Looping this way is somewhat slow. I tried adding the @njit decorator of numba, but apparently it has problems with opencv. Input images are 32×32 pixels They map to
How to get standard deviation across multiple 2d arrays by cell?
I have 16 2d-arrays, each in a shape of [16000, 16000], which means one array has 256000000 cells. I want to have a std_array that is the standard deviation of each cell in the 16 arrays. I tried something but failed, and my questions are in bold. Here’s my attempt. For example (simplified 3*3 arrays): However, the np.std function only
How to use the value of a 2d array as an index to a 3d array in numpy?
I have a 2d array, A, with shape (n x m), where each element of the array at position (i,j) holds a third value k. I want to increment a 3d array with dimensions nxmxl at position (k,i,j) based on the 2d array value and position. So for example if How do you do this in numpy efficiently? Answer The
How to find maximums’ indexes by row with Numpy?
I have np 2d array and want to get indexes of max by row. For example: Maximum by row is [3, 4, 2]. Indexes are [(0,0) (0,3) (1,1) (1,2) (2,4)] I tried smth like this buf = np.apply_along_axis(lambda x: zip(np.where(x == np.max(x))), axis=1, arr=B) or this buf = np.where(B == np.max(B,axis = 1)) But it doesnt work. Also I can’t
dictionary inside column of a dataframe
I have a pandas dataframe that has a column like this : I want to make a condition on the whole dataframe based on the id value. I did many attempts but failed. it says key error, it cannot access ‘id’ which is inside the column ‘platform’. Any help is welcome, and thank you in advance. Answer Use Series.str.get and