Help us understand the problem. What is going on with this article?

# numpy.linalg.normとscipy.spatial.distanceの計算時間比較：pythonで複数のベクトル間のユークリッド距離を求めたいとき

More than 1 year has passed since last update.

# numpy.linalg.normとscipy.spatial.distanceを使う

scipy.spatial.distanceは以下のように使える：

scipyDistance.py
```>>>import scipy.spatial.distance as distance
>>>distance.cdist([[0,1]],[[1,0]], metric='euclidean') #inputs must be 2d array.
array([[1.41421356]])
>>>distance.cdist([[0,1] ],[[1,0],[2,0]], metric='euclidean')
array([[1.41421356, 2.23606798]]) #root 2 and root 3

```

# numpy.linalg.normとscipy.spatial.distanceの速度比較

```import numpy as np
from scipy.spatial import distance
a, b =  np.array([[0,1]]), np.array([[1,0]])
c, d =  [[0,1]], [[1,0]]
```

### jupyter notebookの`%timeit`時間を計測してみる

```%timeit np.linalg.norm(a-b) #takes an array of difference between a and b
%timeit distance.cdist(a,b) #takes two arrays
%timeit distance.cdist(c,d) #takes lists
```

### 時間計測結果

```13.3 µs ± 71.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
29.9 µs ± 1.24 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
39.2 µs ± 7.03 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```

# 結論

`numpy.lingalg.norm` > `scipy.spatial.distance`(配列渡し) > `scipy.spatial.distance`　（リスト渡し）
の順に早いようです。しかしscipyの方が便利で早いケースもあるかもしれません。

# 参考文献

https://docs.scipy.org/doc/scipy/reference/spatial.distance.html

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away