英文:
Fit a shape enclosing data points
问题
我想拟合一个包围所有这些数据点的曲线,例如椭圆或圆。我应该如何做?
英文:
I have some data points as shown in the image:
I want to fit a curve that encloses all these data points, e.g. an ellipse or a circle. How do I do this?
答案1
得分: 1
你寻找的是一个凸包。你可以使用scipy
模块中的ConvexHull
。
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
hull = ConvexHull(points)
plt.scatter(points[:, 0], points[:, 1])
for simplex in hull.simplices:
plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
>>> points
# x y
array([[ 1.85804999, -0.05672655],
[-1.26377587, -0.71670609],
[ 1.20926884, -0.90475264],
...,
[-0.90567066, -0.84883138],
[-0.43970639, 1.84684422],
[ 0.19636511, 1.93878809]])
>>> hull.vertices
array([413, 129, 655, 254, 841, 793, 321, 526, 570, 695, 668, 601, 824,
97, 137, 711, 764, 880, 645, 78, 170, 767, 242, 400, 847, 279,
693], dtype=int32)
>>> points[hull.vertices]
array([[-1.41519105, 1.40697935],
[-1.82809455, 0.76082381],
[-1.96558532, 0.33289305],
[-1.98560143, -0.01191735],
[-1.94680891, -0.44858819],
[-1.92640504, -0.50545949],
[-1.82801696, -0.72696817],
[-1.62541354, -1.10389154],
[-0.81276609, -1.81667984],
[-0.5230336 , -1.91088401],
[-0.16741267, -1.9816704 ],
[ 0.60681346, -1.85569449],
[ 0.97014715, -1.73968032],
[ 1.34722226, -1.46198943],
[ 1.80922099, -0.83972869],
[ 1.86933042, -0.62334995],
[ 1.94523674, -0.29988429],
[ 1.88851458, 0.51313387],
[ 1.78142205, 0.85649355],
[ 1.71284948, 0.99370767],
[ 1.2998368 , 1.51082726],
[ 0.88852111, 1.74722282],
[ 0.50725689, 1.8652817 ],
[ 0.17148352, 1.95374732],
[ 0.04487085, 1.98223443],
[-0.74854329, 1.8364107 ],
[-1.05242057, 1.69470583]])
英文:
What you are looking for is a convex hull. You can use ConvexHull
from scipy
module.
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
hull = ConvexHull(points)
plt.scatter(points[:, 0], points[:, 1])
for simplex in hull.simplices:
plt.plot(points[simplex, 0], points[simplex, 1], 'k-')
>>> points
# x y
array([[ 1.85804999, -0.05672655],
[-1.26377587, -0.71670609],
[ 1.20926884, -0.90475264],
...,
[-0.90567066, -0.84883138],
[-0.43970639, 1.84684422],
[ 0.19636511, 1.93878809]])
>>> hull.vertices
array([413, 129, 655, 254, 841, 793, 321, 526, 570, 695, 668, 601, 824,
97, 137, 711, 764, 880, 645, 78, 170, 767, 242, 400, 847, 279,
693], dtype=int32)
>>> points[hull.vertices]
array([[-1.41519105, 1.40697935],
[-1.82809455, 0.76082381],
[-1.96558532, 0.33289305],
[-1.98560143, -0.01191735],
[-1.94680891, -0.44858819],
[-1.92640504, -0.50545949],
[-1.82801696, -0.72696817],
[-1.62541354, -1.10389154],
[-0.81276609, -1.81667984],
[-0.5230336 , -1.91088401],
[-0.16741267, -1.9816704 ],
[ 0.60681346, -1.85569449],
[ 0.97014715, -1.73968032],
[ 1.34722226, -1.46198943],
[ 1.80922099, -0.83972869],
[ 1.86933042, -0.62334995],
[ 1.94523674, -0.29988429],
[ 1.88851458, 0.51313387],
[ 1.78142205, 0.85649355],
[ 1.71284948, 0.99370767],
[ 1.2998368 , 1.51082726],
[ 0.88852111, 1.74722282],
[ 0.50725689, 1.8652817 ],
[ 0.17148352, 1.95374732],
[ 0.04487085, 1.98223443],
[-0.74854329, 1.8364107 ],
[-1.05242057, 1.69470583]])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论