計(jì)算Python Numpy向量之間的歐氏距離實(shí)例
計(jì)算Python Numpy向量之間的歐氏距離,已知vec1和vec2是兩個(gè)Numpy向量,歐氏距離計(jì)算如下:
import numpydist = numpy.sqrt(numpy.sum(numpy.square(vec1 - vec2)))
或者直接:
dist = numpy.linalg.norm(vec1 - vec2)
補(bǔ)充知識(shí):Python中計(jì)算兩個(gè)數(shù)據(jù)點(diǎn)之間的歐式距離,一個(gè)點(diǎn)到數(shù)據(jù)集中其他點(diǎn)的距離之和
如下所示:
計(jì)算數(shù)兩個(gè)數(shù)據(jù)點(diǎn)之間的歐式距離
import numpy as npdef ed(m, n): return np.sqrt(np.sum((m - n) ** 2))i = np.array([1, 1])j = np.array([3, 3])distance = ed(i, j)print(distance)
在jupyter 中運(yùn)輸代碼輸出結(jié)果如下:
計(jì)算一個(gè)點(diǎn)到數(shù)據(jù)集中其他點(diǎn)的距離之和
from scipy import *import pylab as pl all_points = rand(500, 2)pl.plot(all_points[:, 0], all_points[:, 1], ’b.’)pl.show()
在jupyter 中運(yùn)輸代碼輸出結(jié)果如下:
from scipy import *import pylab as pl all_points = rand(500, 2)pl.plot(all_points[:, 0], all_points[:, 1], ’b.’)pl.show()
定義函數(shù)計(jì)算距離
def cost(c, all_points): #指定點(diǎn),all_points:為集合類的所有點(diǎn)return sum(sum((c - all_points) ** 2, axis=1) ** 0.5)
以上這篇計(jì)算Python Numpy向量之間的歐氏距離實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何通過vscode運(yùn)行調(diào)試javascript代碼2. python b站視頻下載的五種版本3. JavaScript設(shè)計(jì)模式之策略模式實(shí)現(xiàn)原理詳解4. 測(cè)試模式 - XSL教程 - 55. Python結(jié)合百度語音識(shí)別實(shí)現(xiàn)實(shí)時(shí)翻譯軟件的實(shí)現(xiàn)6. JAVA抽象類及接口使用方法解析7. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)8. python如何寫個(gè)俄羅斯方塊9. 本站用的rss輸出10. vue之elementUi的el-select同時(shí)獲取value和label的三種方式
