SciPy凸包错误

4
我是一名有用的助手,可以为您翻译文本。

我正在尝试基于pandas数据帧的两列找到一系列点的凸包。

我的当前代码是:

# Create column of point co-ordinates
df['xy'] = df.apply(lambda x: [x['col_1'], x['col_2']], axis=1)
# Return a numpy array of the point coordinates    
point_list = df.xy.values 
# pass the list to ConvexHull (imported using: from scipy.spatial import ConvexHull)
hull = ConvexHull(point_list) 

当我运行时,出现以下错误:

Traceback (most recent call last):

  File "<ipython-input-41-517201a29182>", line 1, in <module>
    hull = ConvexHull(point_list)

  File "qhull.pyx", line 2220, in scipy.spatial.qhull.ConvexHull.__init__     (scipy\spatial\qhull.c:19058)

  File "C:\Users\****\AppData\Local\Continuum\Anaconda\lib\site-    packages\numpy\core\numeric.py", line 550, in ascontiguousarray
    return array(a, dtype, copy=False, order='C', ndmin=1)

ValueError: setting an array element with a sequence.

对此有何想法?

最好的问候,

1个回答

4

你所做的看起来过于复杂,你可以直接将df列传递给ConvexHull

In [311]:

from scipy.spatial import ConvexHull
df = pd.DataFrame({'col_1':np.random.randn(30), 'col_2':np.random.randn(30), 'col3':0})
df
​
Out[311]:
    col3     col_1     col_2
0      0  0.837349  1.526832
1      0 -0.282778 -0.150751
2      0 -0.331192 -0.382630
3      0 -0.933054 -0.234423
4      0  1.074336 -1.180293
5      0  0.296417  0.626924
6      0  0.806266 -0.501335
7      0 -1.192482 -1.793160
8      0  0.920646  1.377393
9      0 -1.255671  0.428256
10     0 -1.518031  0.888582
11     0  1.231974  0.566314
12     0 -0.717847 -0.236354
13     0  0.758947 -0.286670
14     0 -1.546001  1.774912
15     0 -0.707825 -0.529058
16     0  0.446111  0.406430
17     0  0.711017  0.774281
18     0 -2.616337  0.293725
19     0 -0.370344 -0.471336
20     0 -0.281950 -0.243941
21     0 -1.088772 -1.471154
22     0 -0.422274 -0.266592
23     0  0.423735 -0.341429
24     0  1.166969 -0.329791
25     0  0.689842  1.143460
26     0  0.462430 -0.843409
27     0  3.071030  1.615058
28     0 -0.812258  0.272436
29     0  0.707237 -1.717054

那么我可以直接传递列:

hull = ConvexHull(df[['col_1','col_2']])
import matplotlib.pyplot as plt
plt.plot(df['col_1'], df['col_2'], 'o')
for simplex in hull.simplices:
    plt.plot(df['col_1'].iloc[simplex], df['col_2'].iloc[simplex], 'k-')

这会生成以下图形:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接