2

I'm attempting to remove each column one at a time from an array and, based on the documentation and this question, thought the following should work:

print(all_input_data.shape)

for n in range(9):
    print(n)
    testArray = all_input_data.copy()
    print(testArray.shape)
    np.delete(testArray,[n],axis=1)
    print(testArray.shape)
    print(testArray[0:1][:])

The original matrix is all_input_data.

This is not causing any columns to be deleted or generating any other change to the array. The initial output for the snippet above is:

(682120, 9)

0

(682120, 9)

(682120, 9)

[[  2.37000000e+02   1.60000000e+01   9.90000000e+01   1.04910000e+03
    9.29000000e-01   9.86000000e-01   8.43000000e-01   4.99290000e+01
    1.97000000e+00]]

The delete command is not changing the shape of the matrix at all.

1
  • According to the documentation numpy.delete returns a copy with the specified column(s) deleted. Also note that n will be invalid for n >= 5 as you delete successively columns from the array. You should rather apply a while loop and always delete the first one. Commented Jul 12, 2017 at 7:29

2 Answers 2

2

np.delete returns a copy of the input array with elements removed.

Return a new array with sub-arrays along an axis deleted.

There is no in place deletion of array elements in numpy.

Because np.delete returns a copy and does not modify the input there is no need to manually make a copy of all_input_data:

import numpy as np
all_input_data = np.random.rand(100, 9)

for n in range(9):
    print(n)
    testArray = np.delete(all_input_data,[n],axis=1)
    print(testArray.shape)
    print(testArray[0:1][:])
Sign up to request clarification or add additional context in comments.

5 Comments

@a_guest I think you are wrong mainly for one reason: all_input_data is never modified. The rest follows from that.
@a_guets You are wrong again. The array is different in each iteration. Maybe you should try to run the code before commenting. As I understood the question, the OP wants to delete columns one at a time, not successively.
The original array is copied in every loop iteration, which strongly suggests an intent not to modify the array. But it's pretty useless to speculate on one's intent. Sooner or later the OP may clarify this point.
Deleted comments make me look like talking to myself :)
Thanks. You are correct in interpreting my intent, both in terms of the language used and the code.
1

From linked question consider this:

In [2]: a = np.arange(12).reshape(3,4)

In [3]: np.delete(a, [1,3], axis=1)
Out[3]:
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])

In [4]: a
Out[4]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In other words, if you want to save changes you should save to a new variable, but considering the size of your matrix this wouldn't be practical. What you could do is use slice notation indexing. It is explained here.

1 Comment

Using slice notation (actually indexing) to remove individual columns requires advanced indexing which also copies the array just like delete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.