TypeError:|TypeError: only size-1 arrays can be converted to Python scalars解决办法

def ori_point(points): if len(points) == 1: origin = points[0] else: xyzmean = np.mean(np.array(points), axis=0) distances = np.sqrt(np.sum(np.asarray(xyzmean - points) ** 2, axis=1)) origin = points[int(np.where(distances == np.min(distances))[0])] return origin

【TypeError:|TypeError: only size-1 arrays can be converted to Python scalars解决办法】选择所有点中距离xyzmean最近的点,由于有多个点与xzymean的距离相同,导致无法正确选择距离最近,导致np.where(distances == np.min(distances)的返回值是具有多个索引值得元祖,故报错
TypeError:|TypeError: only size-1 arrays can be converted to Python scalars解决办法
文章图片

改正方法改为:int(np.where(distances == np.min(distances))[0][0])永远取元祖中的第一个数字

    推荐阅读