使用sortingx排序欧式空间内的高维数据点

采用operator.methodcaller来实现

sortingx是一个正逆序协同的排序算法库,使用如下指令下载它:

pip install sortingx --upgrade
pip install sortingx>=1.2.2

高维数据点集类的设计

以4维空间的数据点为例:

import math
class Point:
	def __init__(self, a, b, c, d):
		self.a = a
		self.b = b
		self.c = c
		self.d = d
	def __repr__(self):
		return 'Point({!r:},{!r:},{!r:},{!r:})'.format(self.a, self.b, self.c, self.d)
	def __lt__(self, other):
		return (self.a < other.a if self.a != other.a else (self.b < other.b if self.b != other.b else (self.c < other.c if self.c != other.c else self.d < other.d)))
	def __le__(self, other):
		return (self.a, self.b, self.c, self.d) <= (other.a, other.b, other.c, other.d)
	def __gt__(self, other):
		return (self.a > other.a if self.a != other.a else (self.b > other.b if self.b != other.b else (self.c > other.c if self.c != other.c else self.d > other.d)))
	def __ge__(self, other):
		return (self.a, self.b, self.c, self.d) >= (other.a, other.b, other.c, other.d)
	def __eq__(self, other):
		return self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d
	def distance(self, a, b, c, d):
		return math.hypot(self.a - a, self.b - b, self.c - c, self.d - d)

构造一组实例点

points = [
	Point(1, 2, 3, 4),
	Point(1, -1, 1, -1),
	Point(3, 0, -2, 1),
	Point(2, -2, 3, 2),
	Point(-1, 2, 0, 1),
	Point(1, 1, 1, 1),
	Point(-2, -1, -3, 0),
	Point(2, 0, 1, 1),
	Point(1, -3, -2, -1)
]

通过到原点距离的排序

import sortingx
from operator import methodcaller
print(sortingx.heap(points, key=methodcaller('distance', 0, 0, 0, 0)))

结果:

[Point(1,1,1,1), Point(1,-1,1,-1), Point(2,0,1,1), Point(-1,2,0,1), Point(3,0,-2,1), Point(-2,-1,-3,0), Point(1,-3,-2,-1), Point(2,-2,3,2), Point(1,2,3,4)]

按__lt__与__gt__的魔法规则排序

print(sortingx.heap(points))

结果:

[Point(-2,-1,-3,0), Point(-1,2,0,1), Point(1,-3,-2,-1), Point(1,-1,1,-1), Point(1,1,1,1), Point(1,2,3,4), Point(2,-2,3,2), Point(2,0,1,1), Point(3,0,-2,1)]
print(sortingx.heap(points, reverse=True)) # print(sortingx.insert(points, reverse=True))

结果:

[Point(3,0,-2,1), Point(2,0,1,1), Point(2,-2,3,2), Point(1,2,3,4), Point(1,1,1,1), Point(1,-1,1,-1), Point(1,-3,-2,-1), Point(-1,2,0,1), Point(-2,-1,-3,0)]

你可能感兴趣的:(数据分析,python,开发语言,operator,methodcaller)