[英]how can i delete an element in numpy array and use a for loop in python and its index
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.这是我所做的,但它不起作用,我做了一个 for 循环,我用它来获取索引并在另一件事中使用它为什么它不起作用或者我可以找到另一种方法来删除元素并使用它的索引。
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)
Your task is something like filtering of an array.您的任务类似于过滤数组。 You want to drop all elements == 1 .
您想删除所有元素== 1 。
Assume that the source array ( arr ) contains:假设源数组 ( arr ) 包含:
array([0, 1, 2, 3, 4, 1, 0, 3, 7, 1])
so it contains 3 elements == 1 (to be dropped).所以它包含 3 个元素== 1 (要删除)。
A much simpler way to do it is to use boolean indexing and save the result back to the original variable:一个更简单的方法是使用boolean 索引并将结果保存回原始变量:
arr = arr[arr != 1]
The result is:结果是:
array([0, 2, 3, 4, 0, 3, 7])
as you wish - with all one s dropped.如你所愿 - 所有的都掉了。
#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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.