Deleting slices from numpy array given a condition - python

I have a numpy array t_i of shape (6, 2000, 2).
I need to delete column slices from the array if all the elements in that slice below that row is -1.
Let's say I am in ith row of t_i. Now for those columns below the ith row where every element in the column is [-1,-1] I need to delete the complete column slice from t_i.
I tried this:
t_iF = t_i[:, t_i[i+1:] != -1]
But I am getting this error:
IndexError: too many indices for array: array is 3-dimensional, but 4
were indexed
Can you all please point me to the right direction? Thanks!
Updated:
Let t_i be:
t1=np.arange(32).reshape(4,4,2)
t1[1:,[1,3]] = -1
In this case the whole 1 and 3 column slices need to be deleted.

np.any can be very helpful for tasks like these:
import numpy as np
t1 = np.arange(32).reshape(4, 4, 2)
t1[1:, [1, 3]] = -1
t_filtered = t1[:, [np.any(t1[i, i:] != -1) for i in range(t1.shape[0])], :]
print(t_filtered.shape) # (4, 3, 2)

Related

Numpy array: Remove and append values

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)

np.where to compute age group index

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

Finding the index of one element randomly chosen from np.array elements that match a condition

Consider the numpy array, arr1, shown below.
arr1 = np.array([-8,-3,-5,-2, 8,-4],
[ 7, 4, 3, 1,-5, 6],
[-6, 1,-2,-6,-4,-8],
[ 5,-2, 7,-5,-3,-1]])
row_idx = 2
val = -6
I want to randomly select the index of one value (ie: -6) from the specified row index (ie: 2). I started by isolating the target values:
arr2 = np.where(arr1[row_idx][:] == val)
print(arr2)
This correctly produces the desired indices: (array([0, 3)], dtype=int64,)
I'm a new enough coder that I'm having trouble randomly selecting either the 0 or the 3. What is an efficient way to do this? (the actual arr1 is much larger).
Thanks

How to index with array and slice numpy array at the same time

I have a vector with indexes and vector with values.
For every index in the indexes vector I need to efficiently produce a row in numpy array of reslts
The row in the results array needs to contain the previous value of the index in values, the indexed value and two next values.
Here is how I do this with a loop:
import numpy as np
indexes = np.array([5,10,12])
values = np.array([ 0.89643977, 0.50794841, 0.75995795, 0.78029348, 0.83609961,
0.47534985, 0.17330516, 0.15152753, 0.15504392, 0.10245308,
0.70428183, 0.36804107, 0.13074141, 0.77377332, 0.11368238,
0.74711745, 0.89431082, 0.35544423, 0.08592396, 0.28762 ])
result = values[indexes[0] -1:index[0] -1 + 4]
for index in indexes[1:]:
result = numpy.vstack([result, values[index -1:index -1 + 4]])
print result
[[ 0.83609961 0.47534985 0.17330516 0.15152753]
[ 0.10245308 0.70428183 0.36804107 0.13074141]
[ 0.36804107 0.13074141 0.77377332 0.11368238]]
My indexes and values arrays are big and the loop takes too much time.
Is there a way to do this with out a loop?
This will work as long as no index falls out of bounds, and should be much faster:
In [4]: idx = indexes[:, None] + np.arange(-1, 3)
In [5]: values[idx]
Out[5]:
array([[ 0.83609961, 0.47534985, 0.17330516, 0.15152753],
[ 0.10245308, 0.70428183, 0.36804107, 0.13074141],
[ 0.36804107, 0.13074141, 0.77377332, 0.11368238]])
Is there any reason why you chose values[index -1:][:4] over values[index -1: index -1 + 4]? Also my guess is you rather want to gather all results in a list and then np.vstack rather that doing it in the loop. Pending better answers, you can thus consider using:
ls_results = [values[index -1:index -1 + 4] for index in indexes]
result = np.vstack(ls_results)

filtering elements of matrix by row in python scipy/numpy

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.

Categories

Resources