您的位置:首页 > 编程语言 > Python开发

python计算相关系数

2016-10-25 12:09 351 查看
代码包括以下步骤:

从文件读数据

将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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: