0

This is what I made and it doesn't work, I made a for loop and I use it to get the index and use it in another thing why doesn't it work or can I found another method to delete the element and use the index of it.

Here is some of my code

X1_train, X1_test, y1_train, y1_test = train_test_split(EclipseFeautres, EclipseClass, test_size=0.3, random_state=0)
E_critical_class=y1_train.copy()
E_critical_class = E_critical_class[E_critical_class != 1]
for x in range(len(E_critical_class)):
if(E_critical_class[x]==1):
    E=np.delete(E_critical_class,x)
4
  • the website didn't let me to write the code Commented Jun 18, 2020 at 0:17
  • @TeneshVignesan ok thank u very much Commented Jun 18, 2020 at 0:29
  • The delete result is assigned to E, but that variable isn't used anywhere. So changes that you make in one loop are just thrown away. In general though it is a bad idea to delete items one by one in a loop, even when working with lists. The array/list size changes with deletes. It would be better to collect a list of delete locations, and call np.delete just once with that list. Commented Jun 18, 2020 at 0:51
  • @hpaulj i get what u said but i'm asking this question because I need the same index to delete another elements in another numpy array and I use E list to print it but it doesn't work Commented Jun 18, 2020 at 2:17

2 Answers 2

2

Your task is something like filtering of an array. You want to drop all elements == 1.

Assume that the source array (arr) contains:

array([0, 1, 2, 3, 4, 1, 0, 3, 7, 1])

so it contains 3 elements == 1 (to be dropped).

A much simpler way to do it is to use boolean indexing and save the result back to the original variable:

arr = arr[arr != 1]

The result is:

array([0, 2, 3, 4, 0, 3, 7])

as you wish - with all ones dropped.

Sign up to request clarification or add additional context in comments.

Comments

0
#dizi icinde olan ve kendini tekrarlayan sayiyi delete etme!!!!!!
#to delete the repeated element in the numpy array
import numpy as np
a = np.array([10, 0, 0, 20, 0, 30, 40, 50, 0, 60, 70, 80, 90, 100,0])
print("Original array:")
print(a)
index=np.zeros(0)
print("index=",index)
for i in range(len(a)):
    if a[i]==0:
        index=np.append(index, i)
print("index=",index)
new_a=np.delete(a,index)
print("new_a=",new_a)

Comments

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.