Home  >  Article  >  Backend Development  >  Python numpy point array deduplication

Python numpy point array deduplication

不言
不言Original
2018-04-18 11:04:131974browse

The following is an example of Python numpy point array deduplication. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

No more nonsense, just go to the code with detailed comments

# coding = utf-8
import numpy as np
from IPython import embed
# xy 输入,可支持浮点数操作 速度很快哦
# return xy 去重后结果
def duplicate_removal(xy):
  if xy.shape[0] < 2:
    return xy
  _tmp = (xy*4000).astype(&#39;i4&#39;)          # 转换成 i4 处理
  _tmp = _tmp[:,0] + _tmp[:,1]*1j         # 转换成复数处理 
  keep = np.unique(_tmp, return_index=True)[1]  # 去重 得到索引                  
  return xy[keep]                 # 得到数据并返回  
# _tmp[:,0] 切片操作,因为时二维数组,_tmp[a:b, c:d]为通用表达式,
# 表示取第一维的索引 a 到索引 b,和第二维的索引 c 到索引 d
# 当取所有时可以直接省略,但要加&#39;:&#39;冒号 、当 a == b 时可只写 a ,同时不用&#39;:&#39;冒号
if __name__ == &#39;__main__&#39;:
  if 1: # test
    xy = np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 0.0, 0.0], [1.0, 1.0, 1.00]])
    print(xy)
    new_xy = duplicate_removal(xy)
    print(new_xy)
  embed()

Related recommendations:

Find out the maximum value of the numpy array array and its indexing method

Numpy implements ndarray array returns consistent with Indexing methods for specific conditions


The above is the detailed content of Python numpy point array deduplication. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn