Python中面板数据的相关矩阵

我想为数据面板创建一个相关矩阵。该数据框包含 11 年中每月 15 个数值变量的数据。

我想知道,如果可能的话,如何为此类数据帧的变量生成单个相关矩阵。

我想到的另一种选择是每年生成一个相关矩阵,但我想知道是否可以为整个数据帧只生成一个相关矩阵,以防年数非常大(这将使其每年制作一个矩阵是不可行的)。


慕的地8271018
浏览 101回答 1
1回答

跃然一笑

IIUC,您主要是在寻找corrDataFrame 的方法。考虑这个例子:import pandas as pdimport numpy as npnp.random.seed(0)df = pd.DataFrame(np.random.rand(30, 5)).add_prefix("feature_")df["year"] = np.repeat(["2012", "2013", "2014"], 10)print(df.head()) # first 5 rows. Note that there are 30 rows   feature_0  feature_1  feature_2  feature_3  feature_4  year0   0.548814   0.715189   0.602763   0.544883   0.423655  20121   0.645894   0.437587   0.891773   0.963663   0.383442  20122   0.791725   0.528895   0.568045   0.925597   0.071036  20123   0.087129   0.020218   0.832620   0.778157   0.870012  20124   0.978618   0.799159   0.461479   0.780529   0.118274  2012对您想要在 cormat 中的数字列进行子集化(在本例中,我用于.filter仅获取“feature_X”列)并使用 DataFrame.corr:cormat = df.filter(like="feature").corr()print(cormat)           feature_0  feature_1  feature_2  feature_3  feature_4feature_0   1.000000   0.004582   0.412658   0.269969   0.151162feature_1   0.004582   1.000000  -0.200808   0.140620  -0.138652feature_2   0.412658  -0.200808   1.000000  -0.019439   0.284211feature_3   0.269969   0.140620  -0.019439   1.000000  -0.063653feature_4   0.151162  -0.138652   0.284211  -0.063653   1.000000如果你想得到一些其他变量分组的相关矩阵,你可以.groupby先使用。annual_cormat = df.groupby("year").corr()print(annual_cormat)                feature_0  feature_1  feature_2  feature_3  feature_4year                                                                 2012 feature_0   1.000000   0.359721  -0.266740   0.285998  -0.526528     feature_1   0.359721   1.000000  -0.330484   0.180620  -0.580236     feature_2  -0.266740  -0.330484   1.000000   0.262000   0.428895     feature_3   0.285998   0.180620   0.262000   1.000000  -0.144745     feature_4  -0.526528  -0.580236   0.428895  -0.144745   1.0000002013 feature_0   1.000000   0.135499   0.704653   0.081326   0.453111     feature_1   0.135499   1.000000  -0.385677   0.732700  -0.065941     feature_2   0.704653  -0.385677   1.000000  -0.607016   0.143572     feature_3   0.081326   0.732700  -0.607016   1.000000   0.107971     feature_4   0.453111  -0.065941   0.143572   0.107971   1.0000002014 feature_0   1.000000  -0.624004   0.056185   0.351376  -0.038286     feature_1  -0.624004   1.000000   0.103911  -0.284685   0.266124     feature_2   0.056185   0.103911   1.000000   0.249860   0.145773     feature_3   0.351376  -0.284685   0.249860   1.000000  -0.347361     feature_4  -0.038286   0.266124   0.145773  -0.347361   1.000000
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python