Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以Python方式从数组中删除十进制值_Python_Numpy_Numpy Ndarray - Fatal编程技术网

如何以Python方式从数组中删除十进制值

如何以Python方式从数组中删除十进制值,python,numpy,numpy-ndarray,Python,Numpy,Numpy Ndarray,我试图从数组中删除一个元素。当尝试删除整数值(使用numpy.delete)时,它可以工作,但对十进制值不起作用 For integer deletion X = [1. 2. 2.5 5.7 3. 6. ] to_delete_key = [3, 7.3] Y = np.delete(X, to_delete_key, None) Output is [1. 2. 2.5 5.7 6. ] The value 3 got deleted 而在十进制删除的情况下 For decim

我试图从数组中删除一个元素。当尝试删除整数值(使用numpy.delete)时,它可以工作,但对十进制值不起作用

For integer deletion

X = [1.  2.  2.5 5.7 3. 6. ]
to_delete_key = [3, 7.3]
Y = np.delete(X, to_delete_key, None)
Output is [1.  2.  2.5 5.7 6. ]
The value 3 got deleted
而在十进制删除的情况下

 For decimal deletion
X = [6.  7.3 9.1]
to_delete_key = [3, 7.3]
Y = np.delete(X, to_delete_key, None)
Output is [6.  7.3 9.1]
The value 7.3 didn't get deleted.

我知道如何用正常的方法来做,但是有没有什么有效的python方法来做呢

你正在处理无法精确比较的浮点数。谷歌搜索出“每个程序员都应该知道的关于浮点数的知识”。
由于四舍五入错误,1/3+1/3+1/3可能不等于1

因此,解释是找不到您的值7.3。Numpy可能将7.3转换为32位浮点或与数组中的值不完全相等的任何值

正如@elPastor所提到的,你误用了Numpy

In [249]: X = np.array([1.,  2.,  2.5, 5.7, 3., 6. ])
     ...: to_delete_key = [3, 7.3]

In [252]: np.delete(X, to_delete_key)
Traceback (most recent call last):
  File "<ipython-input-252-f9031065a548>", line 1, in <module>
    np.delete(X, to_delete_key)
  File "<__array_function__ internals>", line 5, in delete
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 4406, in delete
    keep[obj,] = False
IndexError: arrays used as indices must be of integer (or boolean) type
删除的是5.7,
X[3]

np.delete
不按值删除!从文档中:

obj : slice, int or array of ints
    Indicate indices of sub-arrays to remove along the specified axis.
我们可以查找值匹配

In [267]: vals = [3, 2.5]
In [268]: X[:,None]==vals
Out[268]: 
array([[False, False],
       [False, False],
       [False,  True],
       [False, False],
       [ True, False],
       [False, False]])
但浮动上的相等匹配可能不可靠<代码>isclose以公差运行:

In [269]: np.isclose(X[:,None],vals)
Out[269]: 
array([[False, False],
       [False, False],
       [False,  True],
       [False, False],
       [ True, False],
       [False, False]])
然后找到匹配的行:

In [270]: _.any(axis=1)
Out[270]: array([False, False,  True, False,  True, False])
In [271]: X[_]
Out[271]: array([2.5, 3. ])
In [272]: X[~__]
Out[272]: array([1. , 2. , 5.7, 6. ])
列表具有“按移除”值:

In [284]: alist=X.tolist()
In [285]: alist.remove(3.0)
In [286]: alist.remove(2.5)
In [287]: alist
Out[287]: [1.0, 2.0, 5.7, 6.0]

你需要使用numpy吗?这么简单的任务似乎很昂贵。你可以只做
y=[z代表x中的z,如果z不在delete_数组中]
其中
delete_数组
只是你想删除的数字列表吗?这回答了你的问题吗?不完全是我需要用NumPy。我将把数组转换成一个列表,然后执行操作。你确定第一个示例是正确的吗?我没有得到那个结果,即使只有
3
。使用
7.3
有什么意义?它不会出现在
X
中。
In [284]: alist=X.tolist()
In [285]: alist.remove(3.0)
In [286]: alist.remove(2.5)
In [287]: alist
Out[287]: [1.0, 2.0, 5.7, 6.0]