Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude rows/columns from numpy.ndarray data

Tags:

python

numpy

Assume we have a numpy.ndarray data, let say with the shape (100,200), and you also have a list of indices which you want to exclude from the data. How would you do that? Something like this:

a = numpy.random.rand(100,200)
indices = numpy.random.randint(100,size=20)
b = a[-indices,:] # imaginary code, what to replace here?

Thanks.

like image 498
adrin Avatar asked Jan 09 '14 14:01

adrin


People also ask

How do I select specific rows and columns in NumPy?

We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.

How can I remove columns in NumPy array that contains non numeric values?

Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np. isnan() function.

How do I remove nan values from NumPy Ndarray?

How to drop all missing values from a numpy array? Droping the missing values or nan values can be done by using the function "numpy. isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy. logical_not()" where the boolean values will be reversed.


Video Answer


1 Answers

You can use b = numpy.delete(a, indices, axis=0)

Source: NumPy docs.

like image 107
Bang Avatar answered Sep 28 '22 09:09

Bang