计算和可视化处于无序序列中的2个变量之间的相关性

问题描述 投票:0回答:1

作为我最后一年研究实施的一部分,我试图计算并可视化两个不在有序系列中的变量之间的相关性。在如下的数据集中,

DateAndTime           Demand    Temperature
2015-01-02 18:00:00    2081         41
2015-01-02 19:00:00    2370         42
2015-01-02 20:00:00    2048         42
2015-01-02 21:00:00    1806         42
2015-01-02 22:00:00    1818         41
2015-01-02 23:00:00    1918         40
2015-01-03 00:00:00    1685         40
2015-01-03 01:00:00    1263         38
2015-01-03 02:00:00     969         38
2015-01-03 03:00:00     763         37
2015-01-03 04:00:00     622         36

计算和可视化日期和需求之间的相关性很简单,因为它们是有序序列,散点图可以用来轻松地显示它们的相关性。但是,如果我要计算温度和需求之间的相关性,则得到的散点图没有多大意义,因为它不是任何数学顺序。应该采用什么方法以更有意义的方式可视化这两个变量之间的相关性。我正在使用基本的python框架,如Matplotlib,Statsmodels和Sklearn。

python statistics correlation statsmodels
1个回答
1
投票

好的,所以想法是绘制两个列,一个在x轴上,另一个在y轴上,并尝试制作一个模拟其行为的线。 Numpy有一个计算线的功能

import numpy as np
import matplotlib.pyplot as plt

x = [4,2,1,5]
y = [2,4,6,3]

fit = np.polyfit(x,y,1)
fit_line = np.poly1d(fit)

plt.figure()
plt.plot(x,y,'rx')
plt.plot(x,fit_line(x),'--b')
plt.show()

enter image description here

如果我们将回归线视为y = a*x + b,则可以获得系数a和b

a = fit[0]
b = fit[1]

返回

a = -0.8000000000000005
b = 6.150000000000002

只需使用你的x和y

© www.soinside.com 2019 - 2024. All rights reserved.