There is a part of the following code that I don't quite understand.
Here is the code:
import numpy as np
medalNames = np.array(['none', 'bronze', 'silver', 'gold'])
ageGroupCategories = np.array(['B','P','G','T'])
allLowerThresholds = np.array([[-1,0,5,10], [0,5,10,15], [0,11,14,17], [0,15,17,19]])
ageGroupIndex = np.where(ageGroup[0] == ageGroupCategories)[0][0]
In the last line, what does the [0][0] do, why doesn't the code work without it?
A few things:
Use embedded Code boxes
Your code isn't working at all because the variable ageGroup doesn't exist
Now to your question:
since it is an array the [0][0] calls on the first row and first column of the result of the array np.where().
Your question is general and related to the numpy.where function.
Let's take a simple example as follows:
A=np.array([[3,2,1],[4,5,1]])
# array([[3, 2, 1],
# [4, 5, 1]])
print(np.where(A==1))
# (array([0, 1]), array([2, 2]))
As you can see the np.where function returns a tuple. The first element (it's a numpy array) of the tuple is the row/line index, and the second element (it's again a numpy array), is the column index.
np.where(A==1)[0] # this is the first element of the tuple thus,
# the numpy array containing all the row/line
# indices where the value is = 1.
#array([0, 1])
The above tells you that there is a value = 1 in the first (0) and second (1) row of the matrix A.
Next:
np.where(A==1)[0][0]
0
returns the index of the first line that contains a value = 1. 0 here is the first line of matrix A
Related
I'm trying to do a sorting algorithm and I need to do something like this:
arr = numpy.array([2,4,5,6])
#some function here or something
array([5,2,4,6])
#element "5" moved from position 3 to 1, and all of the others moved up one position
What I mean is I want to change an element's position (index) and move all the other elements up one position. Is this possible?
You could use numpy.roll() with a subset assignment:
arr = numpy.array([2,4,5,6])
arr[:3] = numpy.roll(arr[:3],1)
print(arr)
[5 2 4 6]
If you know the position/index of the element to be shifted, then you could do:
indx = 2
np.r_[arr[indx], np.delete(arr, indx)]
Out[]: array([5, 2, 4, 6])
You could do this in-place without the need of creating first an intermediate array of n-1 elements and then creating another final array by concatenation. Instead, you could try this:
idx = 2
tmp = arr[idx]
arr[1:idx+1] = arr[0:idx]
arr[0] = tmp
So there are more than one ways of doing this, and the choice depends upon your algorithm's constraints.
I have a 3D numpy array of the shape (1, 60, 1). Now I need to remove the first value of the second dimension and instead append a new value at the end.
If it was a list, the code would look somewhat like this:
x = [1, 2, 3, 4]
x = x[1:]
x.append(5)
resulting in this list: [2, 3, 4, 5]
What would be the easiest way to do this with numpy?
I have basically never really worked with numpy before, so that's probably a pretty trivial problem, but thanks for your help!
import numpy as np
arr = np.arange(60) #creating a nd array with 60 values
arr = arr.reshape(1,60,1) # shaping it as mentiond in question
arr = np.roll(arr, -1) # use np.roll to circulate the array left or right (-1 is 1 step to the left)
#Now your last value is in the second last position, the second last value in the third last pos and so on (Your first value moves to the last position)
arr[:,-1,:] = 1000 # index the last location and add the values you want
print(arr)
Say I have a Numpy nd array like below.
arr = np.array([[2, 3, 9], [1,6,7], [4, 5, 8]])
The numpy array is a 3 x 3 matrix.
What I want to do is the find row index which contains the overall maximum value.
For instance in the above example, the maximum value for the whole ndarray is 9. Since it is located in the first row, I want to return index 0.
What would be the most optimal way to do this?
I know that
np.argmax(arr)
Can return the index of the maximum for the whole array, per row, or per column. However is there method that can return the index of the row which contains the overall maximum value?
It would also be nice if this could also be changed to column wise easily or find the minimum, etc.
I am unsure on how to go about this without using a loop and finding the overall max while keeping a variable for the index, however I feel that this is inefficient for numpy and that there must be a better method.
Any help or thoughts are appreciated.
Thank you for reading.
Use where with amax, amin.
import numpy as np
arr = np.array([[2, 3, 9], [1,6,7], [4, 5, 8]])
max_value_index = np.where(arr == np.amax(arr))
min_value_index = np.where(arr == np.amin(arr))
Output:
(array([0]), array([2]))
(array([1]), array([0]))
Consider the following NumPy array:
a = np.array([[1,4], [2,1],(3,10),(4,8)])
This gives an array that looks like the following:
array([[ 1, 4],
[ 2, 1],
[ 3, 10],
[ 4, 8]])
What I'm trying to do is find the minimum value of the second column (which in this case is 1), and then report the other value of that pair (in this case 2). I've tried using something like argmin, but that gets tripped up by the 1 in the first column.
Is there a way to do this easily? I've also considered sorting the array, but I can't seem to get that to work in a way that keeps the pairs together. The data is being generated by a loop like the following, so if there's a easier way to do this that isn't a numpy array, I'd take that as an answer too:
results = np.zeros((100,2))
# Loop over search range, change kappa each time
for i in range(100):
results[i,0] = function1(x)
results[i,1] = function2(y)
How about
a[np.argmin(a[:, 1]), 0]
Break-down
a. Grab the second column
>>> a[:, 1]
array([ 4, 1, 10, 8])
b. Get the index of the minimum element in the second column
>>> np.argmin(a[:, 1])
1
c. Index a with that to get the corresponding row
>>> a[np.argmin(a[:, 1])]
array([2, 1])
d. And take the first element
>>> a[np.argmin(a[:, 1]), 0]
2
Using np.argmin is probably the best way to tackle this. To do it in pure python, you could use:
min(tuple(r[::-1]) for r in a)[::-1]
How can I filter elements of an NxM matrix in scipy/numpy in Python by some condition on the rows?
For example, just you can do where(my_matrix != 3) which treats the matrix "element-wise", I want to do this by row, so that you can ask things like where (my_matrix != some_other_row), to filter out all rows that are not equal to some_other_row. How can this be done?
Assume you have a matrix
a = numpy.array([[0, 1, 2],
[3, 4, 5],
[0, 1, 2]])
and you want to get the indices of the rows tha are not equal to
row = numpy.array([0, 1, 2])
You can get these indices by
indices, = (a != row).any(1).nonzero()
a != row compares each row of a to row element-wise, returning a Boolean array of the same shape as a. Then, we use any() along the first axis to find rows in which any element differs from the corresponding element in row. Last, nonzero() gives us the indices of those rows.