# 【集体智慧编程 学习笔记】 Euclidean距离和Pearson相关系数

- URL: https://www.yidoo.xyz/the-euclidean-distance-and-the-pearson-correlation-coefficient
- 作者: 核动力蜗牛
- 发布时间: 2012-06-23
- 更新时间: 2012-06-23
- 分类: 技术笔记
- 标签: machine_learning

_Euclidean 距离_

定义:欧几里得空间中点 x = (x1,...,xn) 和 y = (y1,...,yn) 之间的距离为

![image](https://www.yidoo.xyz/images/d4b4f7e89c3bf12bf1757c84def3a44a.png)

两个变量之间的相关系数越高，从一个变量去预测另一个变量的精确度就越高，这是因为相关系数越高，就意味着这两个变量的共变部分越多，所以从其中一个变量的变化就可越多地获知另一个变量的变化。如果两个变量之间的相关系数为 1 或-1，那么你完全可由变量 X 去获知变量 Y 的值。

· 当相关系数为 0 时，X 和 Y 两变量无关系。

· 当 X 的值增大，Y 也增大，正相关关系，相关系数在 0.00 与 1.00 之间

· 当 X 的值减小，Y 也减小，正相关关系，相关系数在 0.00 与 1.00 之间

· 当 X 的值增大，Y 减小，负相关关系，相关系数在-1.00 与 0.00 之间

当 X 的值减小，Y 增大，负相关关系，相关系数在-1.00 与 0.00 之间

相关系数的绝对值越大，相关性越强，相关系数越接近于 1 和-1，相关度越强，相关系数越接近于 0，相关度越弱。

![image](https://www.yidoo.xyz/images/e039d7a6bc6ccc4786a2e6ff3e2fdb8e.gif)

实现代码：

```python
from math import sqrt

# A dictionary of movie critics and their ratings of a small
# set of movies
movies = {'Lisa Rose': {'Lady in the Water': 2.5,
                       'Snakes on a Plane': 3.5,
                       'Just My Luck': 3.0,
                       'Superman Returns': 3.5,
                       'You, Me and Dupree': 2.5,
                       'The Night Listener': 3.0},
         'Gene Seymour': {'Lady in the Water': 3.0,
                          'Snakes on a Plane': 3.5,
                          'Just My Luck': 1.5,
                          'Superman Returns': 5.0,
                          'The Night Listener': 3.0,
                          'You, Me and Dupree': 3.5},
         'Michael Phillips': {'Lady in the Water': 2.5,
                              'Snakes on a Plane': 3.0,
                              'Superman Returns': 3.5,
                              'The Night Listener': 4.0},
         'Claudia Puig': {'Snakes on a Plane': 3.5,
                          'Just My Luck': 3.0,
                          'The Night Listener': 4.5,
                          'Superman Returns': 4.0,
                          'You, Me and Dupree': 2.5},
         'Mick LaSalle': {'Lady in the Water': 3.0,
                          'Snakes on a Plane': 4.0,
                          'Just My Luck': 2.0,
                          'Superman Returns': 3.0,
                          'The Night Listener': 3.0,
                          'You, Me and Dupree': 2.0},
         'Jack Matthews': {'Lady in the Water': 3.0,
                           'Snakes on a Plane': 4.0,
                           'The Night Listener': 3.0,
                           'Superman Returns': 5.0,
                           'You, Me and Dupree': 3.5},
         'Toby': {'Snakes on a Plane':4.5,
                  'You, Me and Dupree':1.0,
                  'Superman Returns':4.0}}

def euclidean(data, p1, p2):
    "Calculate Euclidean distance"
    distance = sum([pow(data[p1][item]-data[p2][item],2)
                      for item in data[p1] if item in data[p2]])

    return distance

def pearson(data, p1, p2):
    "Calculate Pearson correlation coefficient"
    corrItems = [item for item in data[p1] if item in data[p2]]

    n = len(corrItems)
    if n == 0:
        return 0;

    sumX = sum([data[p1][item] for item in corrItems])
    sumY = sum([data[p2][item] for item in corrItems])
    sumXY = sum([data[p1][item] * data[p2][item] for item in corrItems])
    sumXsq = sum([pow(data[p1][item], 2) for item in corrItems])
    sumYsq = sum([pow(data[p2][item],2) for item in corrItems])

    pearson = (sumXY - sumX * sumY / n) / sqrt((sumXsq - pow(sumX, 2) / n) * (sumYsq - pow(sumY, 2) / n))
    return pearson
```
