python计算相关系数

代码包括以下步骤:

从文件读数据

将list转化为ndarray

计算相关系数

import numpy as np

path = '/home/fhqplzj/IdeaProjects/DocumentClustering/FirstPython/bishe/data.txt'
file = open(path)


def fun(s):
    return float(s.strip())


data = list(map(fun, file.readlines()))
x = np.array(data)
x = x.reshape(2, -1).T
y = np.corrcoef(x[:, 0], x[:, 1])
print(y)
数据为:

12.5
15.3
23.2
26.4
33.5
34.4
39.4
45.2
55.4
60.9
21.2
23.9
32.9
34.1
42.5
43.2
49.0
52.8
59.4
63.5



你可能感兴趣的:(python)