Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want to delete any column in my python array that has the number 1 in it. How

ID: 3553328 • Letter: I

Question

I want to delete any column in my python array that has the number 1 in it. How do I go about doing this?

I have created an array using python like this:


import numpy as np

vecs=np.zeros((20,3))


for i in range(20):

for j in range(3):

vecs[i][0]=i+2

vecs[j][0]=j+2

vecs[i][1]= i+1

vecs[j][1]=j+1

vecs[i][2]=i+5

vecs[j][2]=j+5

print vecs


output

:[[ 2. 1. 5.]

[ 3. 2. 6.]

[ 4. 3. 7.]

[ 5. 4. 8.]

[ 6. 5. 9.]

[ 7. 6. 10.]

[ 8. 7. 11.]

[ 9. 8. 12.]

[ 10. 9. 13.]

[ 11. 10. 14.]

[ 12. 11. 15.]

[ 13. 12. 16.]

[ 14. 13. 17.]

[ 15. 14. 18.]

[ 16. 15. 19.]

[ 17. 16. 20.]

[ 18. 17. 21.]

[ 19. 18. 22.]

[ 20. 19. 23.]

[ 21. 20. 24.]]


so the entire second column should be deleted/. do i do this?

Explanation / Answer

import numpy as np
vecs=np.zeros((20,3))
for i in range(20):
    for j in range(3):
        vecs[i][0]=i+2
        vecs[j][0]=j+2
        vecs[i][1]= i+1
        vecs[j][1]=j+1
        vecs[i][2]=i+5
        vecs[j][2]=j+5
print vecs
columnsDelete=[]
for i in range(3):
    cond=0
    for j in range(20):
        if vecs[j][i]==1:
            cond=1
    if cond==1:
        columnsDelete.append(i);
for i in columnsDelete:
    vecs=np.delete(vecs,i,1)
print vecs

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote