Skip to content

Tag: numpy

frequency of unique values for 2d numpy array

I have a 2-dimensional numpy array of following format: now how to print the frequency of unique elements in this 2d numpy array, so that it returns count([1. 0.]) = 1 and count([0. 1.]) = 1? I know how to do this using loops, but is there any better pythonic way to do this. Answer You can use numpy.unique(),…

Variable is not divisible by an unspecific set of Numbers

I just started to code and try to build my first function in Python/NumPy. My first goal was to get a list for all multiplies of 31 which are not divisible by numbers 1>31 and I figured it out Here is the code: The next step is to make the function more flexible, but easy ideas like this just don’t

How to efficiently create multidimensional arrays?

assuming I have any function such as f(x, y, z) = xyz what’s the fastest way of calculating every value for f given three linear input arrays x, y, and z? Of course I can do something along the lines of, but this is probably not the best way to do it, especially if the input arrays become larger. Is

Supplying varying number of input arguments for np.meshgrid

I have a function that uses np.meshgrid to get the matrix form of supplied co-ordinates. I have a parameter dim that determines what dimension I am working with and needs to return an array with dim dimension along axis 1. I have attached an MWE below. However my expected output is , which is obtained by repl…

Getting a single array containing several sub-arrays iteratively

I have a little question about python Numpy. What I want to do is the following: having two numpy arrays arr1 = [1,2,3] and arr2 = [3,4,5] I would like to obtain a new array arr3 = [[1,2,3],[3,4,5]], but in an iterative way. For a single instance, this is just obtained by typing arr3 = np.array([arr1,arr2]). …