基于numpy数组的空间对象和计算。
scikit-spatial的Python项目详细描述
简介
这个包提供基于numpy数组的空间对象,以及使用这些对象的计算。该软件包包括二维、三维和高维空间的计算。
提供了以下空间对象:
- Point
- Points
- Vector
- Line
- Plane
- Circle
- Sphere
计算可分为以下主要类别:
- Measurement
- Comparison
- Projection
- Intersection
- Fitting
- Transformation
所有空间对象都配备了基于matplotlib的绘图方法。支持二维和三维打印。通过一次绘制多个对象,可以方便地可视化空间计算。
为什么是这个而不是scipy.spatial或sympy.geometry?
这个包与scipy.spatial的功能几乎没有重叠。它可以看作是一个面向对象的扩展。
<^ >在{TT3} $模块中存在类似的空间对象和计算,^ {TT6}$是基于NUMPY而不是符号数学。scikit-spatial(Point、Points和Vector)的主要对象实际上是numpyndarray的子类。这给了他们ndarray的所有常规功能,以及这个包中的其他方法。>>> from skspatial.objects import Vector
>>> vector = Vector([2, 0, 0])
从numpy继承的行为:
>>> vector.size 3 >>> vector.mean().round(3) 0.667
来自scikit-spatial:
的其他方法>>> vector.norm() 2.0 >>> vector.unit() Vector([1., 0., 0.])
Point和Vector基于1d numpy数组,Points基于2d numpy数组,其中每一行表示空间中的一个点。Line和Plane对象具有Point和Vector对象作为属性。
因为scikit-spatial的计算也基于numpy,所以关键字参数可以传递给numpy函数。例如,可以在测试共线性时指定公差。tol关键字传递给numpy.linalg.matrix_rank。
>>> from skspatial.objects import Points
>>> points = Points([[1, 2, 3], [4, 5, 6], [7, 8, 8]])
>>> points.are_collinear() False >>> points.are_collinear(tol=1) True
安装
该软件包可以通过pip安装。
$ pip install scikit-spatial
示例用法
测量值
测量两个向量之间的余弦相似性。
>>> from skspatial.objects import Vector
>>> Vector([1, 0]).cosine_similarity([1, 1]).round(3) 0.707
比较
检查多个点是否共线。
>>> from skspatial.objects import Points
>>> points = Points([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> points.are_collinear() True
投影
把一个点投射到一条线上。
>>> from skspatial.objects import Line
>>> line = Line(point=[0, 0, 0], direction=[1, 1, 0])
>>> line.project_point([5, 6, 7]) Point([5.5, 5.5, 0. ])
如果计算未定义,则会引发错误。
>>> line_a = Line([0, 0], [1, 0]) >>> line_b = Line([1, 0], [1, 0])
>>> line_a.intersect_line(line_b) Traceback (most recent call last): ... ValueError: The lines must not be parallel.
交叉口
找出两个平面的交点。
>>> from skspatial.objects import Plane
>>> plane_a = Plane([0, 0, 0], [0, 0, 1]) >>> plane_b = Plane([5, 16, -94], [1, 0, 0])
>>> plane_a.intersect_plane(plane_b) Line(point=Point([5., 0., 0.]), direction=Vector([0, 1, 0]))
安装
找到最适合多个点的平面。
>>> points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]]
>>> Plane.best_fit(points) Plane(point=Point([0.5, 0.5, 0. ]), normal=Vector([0., 0., 1.]))
转换
沿一条直线将多个点转换为1D坐标。
>>> line = Line(point=[0, 0, 0], direction=[1, 2, 0]) >>> points = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> line.transform_points(points).round(3) array([ 2.236, 6.261, 10.286])
确认
这个包是用Cookiecutter和audreyr/cookiecutter-pypackage项目模板创建的。