Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove one column for a numpy array

I have a numpy array of dimension (48, 366, 3) and I want to remove the last column from the array to make it (48, 365, 3). What is the best way to do that? (All the entries are integers. I'm using Python v2.6)

like image 908
Double AA Avatar asked Jul 15 '11 17:07

Double AA


People also ask

How do I remove a column from a NumPy array?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.

How do you delete a column from a 2D array in Python?

Delete a column in 2D Numpy Array by column number To delete a column from a 2D numpy array using np. delete() we need to pass the axis=1 along with numpy array and index of column i.e.

How do you delete an element from an array in Python?

Removing Python Array Elements We can delete one or more items from an array using Python's del statement. We can use the remove() method to remove the given item, and pop() method to remove an item at the given index.

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.


2 Answers

You could try numpy.delete:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html

or just get the slice of the array you want and write it to a new array.

For example:

a = np.random.randint(0,2, size=(48,366,3))
b = np.delete(a, np.s_[-1:], axis=1)
print b.shape # <--- (48,365,3)

or equivalently:

b = np.delete(a, -1, axis=1)

or:

b = a[:,:-1,:]
like image 90
JoshAdel Avatar answered Oct 18 '22 23:10

JoshAdel


Along the lines:

In []: A= rand(48, 366, 3)
In []: A.shape
Out[]: (48, 366, 3)

In []: A= A[:, :-1, :]
In []: A.shape
Out[]: (48, 365, 3)
like image 5
eat Avatar answered Oct 19 '22 01:10

eat