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.
Related
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.
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)
I have a homework assignment to extract a 2-dimensional numpy array out of another 2-dimensional np array by choosing specific columns by condition (not by range).
So I have an array A with shape (3, 50000). I am trying to get a new array with shape (3, x) for some x < 50000 with the original columns ofAthat satisfy the third cell in the column is-0.4 < z < 0.1`.
For example if:
A = [[1,2,3],[2,0.5,0],[9,-2,-0.2],[0,0,0.5]]
I wish to have back:
B = [[2,0.5,0],[9,-2,-0.2]
I have tried to make a bool 1 rank array that holds true on the columns I want, and to some how combine between the two. The problem it's output is 1 rank array which is not what I am looking for. And I got some ValueErrors..
bool_idx = (-0.4 < x_y_z[2] < 0.1)
This code made some troubles:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I can do it with some loops but NumPy got so many beautiful function I am sure I am missing something here..
In Python, the expression -0.4 < x_y_z[2] < 0.1 is roughly equivalent to -0.4 < x_y_z[2] and x_y_z[2] < 0.1. The and operator decides the truth value of each part of the expression by converting it into a bool. Unlike Python lists and tuples, numpy arrays do not support the conversion.
The correct way to specify the condition is with bitwise & (which is unambiguous and non-short-circuiting), rather than the implicit and (which short circuits and is ambiguous in this case):
condition = ((x_y_z[2, :] > - 0.4) & (x_y_z[2, :] < 0.1))
condition is a boolean mask that selects the columns you want. You can select the rows with a simple slice:
selection = x_y_z[:, condition]
I have a specific requirement for this problem. I need it to be simple and fast.
My problem:
I have two 2D arrays and I need to replace values in 1. array by values in 2. array according to condition. That is if element in x,y position in 1. array is smaller than element in x,y position in 2. array, then replace element in 1. array by element in 2. array.
what I tried and is not working:
import numpy as np
arr = np.random.randint(3,size=(2, 2))
arr2 = np.random.randint(3,size=(2, 2))
print(arr)
print(arr2)
arr[arr<arr2]=arr2 # Doesnt work.
This raises TypeError:
TypeError: NumPy boolean array indexing assignment requires a 0 or 1-dimensional input, input has 2 dimensions.
I can see, that it would be possible to iterate through columns or rows, but I believe it can be done without iteration.
Thanks in advance
I have two numpy arrays:
a = np.arange(100*100).reshape(100,100)
b = np.random.rand(100, 100)
I also have a tuple of slices to extract a certain piece of the array:
slice_ = (slice(5, 10), slice(5, 10))
I then have a set of boolean indices to select a certain part of this slice:
indices = b[slice_] > 0.5
If I want to set these indices to a different value I can do it easily:
a[slice_][indices] = 42
However, if I create another set of boolean indices to select a specific part of the indexed array:
high_indices = a[slice_][indices] > 700
and then try and set the value of the array at these indices:
a[slice_][indices][high_indices] = 42 # Doesn't do anything!
I thought maybe I needed to AND the two index arrays together, but they are different shape: indices has a shape of (5, 5) and high_indices has a shape of (12,).
I think I've got myself into a terrible muddle here trying to do something relatively simple. How can I index using these two boolean arrays in such a way that I can set the values of the array?
Slicing a numpy array returns a view, but boolean indexing returns a copy of an array. So when you indexed it first time with boolean index in a[slice_][indices][high_indices], you got back a copy, and the value 42 is assigned to a copy and not to the array a. You can solve the problem by chaining the boolean index:
a[slice_][(a[slice_] > 700) & (b[slice_] > 0.5)] = 42