Get array with maximum value for each array index - python

I am using Python 3 on CentOS 7. I would like to find the quickest way to get the index of the array with the maximum, for each index with the arrays, over multiple arrays without using loops. For example, if I input
array[0] = [1,3,9,4,6,8,9]
array[1] = [2,6,3,8,7,4,5]
array[2] = [6,3,7,9,1,3,6]
I would like the output to be
[2,1,0,2,1,0,0]
I tried
np.maximum.reduce(array)
and got the maximum values, across the arrays, for the indices across arrays. However, when I tried
array.index(np.maximum.reduce(array))
I get
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Try with argmax from numpy
np.argmax(array, axis = 1)
output
[2 3 3]

Try this:
import numpy as np
import pandas as pd
arr = np.array([[1,3,9,4,6,8,9],
[2,6,3,8,7,4,5],
[6,3,7,9,1,3,6]])
df = pd.DataFrame(arr)
print(df)
result = [df[x].idxmax() for x in df]
print(result)

Related

Number of values within a certain range in Python

I have an array A. I want to print total number of values in the range [1e-11,1e-7]. But I am getting an error. I present the expected output.
import numpy as np
A=np.array([ 4.21922009e+002, 4.02356746e+002, 3.96553289e-09,
3.91811967e-010, 3.88467908e-08, 3.86636300e-010])
B=1e-11<A<1e-07
print(B)
The error is
in <module>
B=1e-11<A<1e-07
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The expected output is
4
The numpy-way is to refactor the interval condition into two subconditions using the & operator:
a = np.array([ 4.21922009e+002, 4.02356746e+002, 3.96553289e-09,
3.91811967e-010, 3.88467908e-08, 3.86636300e-010])
mask = (1e-11<a) & (a<1e-07)
# if you care about the values of the filtered array
print(a[mask].size)
# or just
print(np.count_nonzero(mask))
You can't use your code with numpy array:
B = sum((1e-11<A) & (A<1e-07))
print(B)
# Output
4
It doesn't make sense for Python (and not numpy) to compare 2 scalar values to an array.

Splitting numpy multidimensional array based on indices stored in another array or list

I have a numpy multidimensional array with shape = (12,2,3,3)
import numpy as np
arr = np.arange(12*2*3*3).reshape((12,2,3,3))
I need to select those elements based on the 2nd dimension where the dindices are stored in another list
indices = [0,1,0,0,1,1,0,1,1,0,1,1]
in one array, and the rest in another array.
the output in either case should be in another array of shape (12,3,3)
arr2 = np.empty((arr.shape[0],*arr.shape[-2:]))
I could do it using a for loop
for i, ii in enumerate(indices):
arr2[i] = arr[i, indices[ii],...]
However, I am searching for a one liner.
When I try indexing using the list as indices
test = arr[:,indices,...]
I get test of shape (12,12,3,3) instead of (12,3,3). Could you help me please?
You can use np.arange for indexing the first dimension:
test = arr[np.arange(arr.shape[0]),indices,...]
or just the python range function:
test = arr[range(arr.shape[0]),indices,...]

Multidimensional numpy array appending with Python

In Python, I can concatenate two arrays like below,
myArray = []
myArray += [["Value1", "Value2"]]
I can then access the data by indexing the 2 dimensions of this array like so
print(myArray[0][0])
which will output:
Value1
How would I go about achieving the same thing with Numpy?
I tried the numpy append function but that only ever results in single dimensional arrays for me, no matter how many square brackets I put around the value I'm trying to append.
If you know the dimension of the final array then you could use np.vstack
>>> import numpy as np
>>> a = np.array([]).reshape(0,2)
>>> b = np.array([['Value1', 'Value2']])
>>> np.vstack([a,b])
array([['Value1', 'Value2']], dtype='<U32')

Find minimum values of numpy columns

Looking to print the minimum values of numpy array columns.
I am using a loop in order to do this.
The array is shaped (20, 3) and I want to find the min values of columns, starting with the first (i.e. col_value=0)
I have coded
col_value=0
for col_value in X:
print(X[:, col_value].min)
col_value += 1
However, it is coming up with an error
"arrays used as indices must be of integer (or boolean) type"
How do I fix this?
Let me suggest an alternative approach that you might find useful. numpy min() has axis argument that you can use to find min values along various
dimensions.
Example:
X = np.random.randn(20, 3)
print(X.min(axis=0))
prints numpy array with minimum values of X columns.
You don't need col_value=0 nor do you need col_value+=1.
x = numpy.array([1,23,4,6,0])
print(x.min())
EDIT:
Sorry didn't see that you wanted to iterate through columns.
import numpy as np
X = np.array([[1,2], [3,4]])
for col in X.T:
print(col.min())
Transposing the axis of the matrix is one the best solution.
X=np.array([[11,2,14],
[5,15, 7],
[8,9,20]])
X=X.T #Transposing the array
for i in X:
print(min(i))

Loop through 2d array, if value is true, append in an empty array

I'm trying to loop through the second column of an array and pick out every value that == 1, then store the corresponding value from the first column in a new array.
array1 = []
array2 = ([10,0], [11,0], [12,1], [13,1], [14,0])
What I'm trying to do is,
for i in array2:
if array2[:,1] == 1:
array1[:,1] = array2[:,1]
But this gives me an error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
So, in theory, I should get something like:
array1 = [12, 13]
I managed to get the matching index values with the following, but can't figure it out for the columns.
array1 = np.array([i for i, row in enumerate(array2[:,1]) if row ==1])
First, your question is extremely misleading because nowhere do you mention NumPy, but that is what you are using based on the error messages you have.
So let's start with sample input of the type you're actually using, which is a NumPy array:
array2 = np.array([[10,0], [11,0], [12,1], [13,1], [14,0]])
Then, the solution is:
array2[array2[:,1] == 1, 0]
This gives:
array([12, 13])
You should always try to avoid for loops when working with NumPy arrays. Looping over data is slow, doing it in vector form is fast.

Categories