简体   繁体   English

计算线性回归斜率矩阵(与相关矩阵相同) - Python/Pandas

[英]Calculate linear regression slope matrix (Same to correlation matrix) - Python/Pandas

在此处输入图像描述 How do I calculate slope of each columns below?如何计算下面每列的斜率? The.corr method scans all columns and find the correlation coeffficient with each column. .corr 方法扫描所有列并找到每列的相关系数。 I want to do the same but get slope of the datasets with each other.我想做同样的事情,但得到数据集的斜率。 The slope line of code below does not return expected values.下面的代码斜线不返回预期值。 I think I am not using newaxis correctly.我想我没有正确使用 newaxis。 I need a 5x5 symmetric matrix with 1 as diagonal which means slope is for y = x.我需要一个 1 作为对角线的 5x5 对称矩阵,这意味着斜率为 y = x。 I did it on excel picture attached with the reference being 717024 column.我是在 excel 图片上完成的,参考为 717024 列。 I need to iterate each column and make it as reference.我需要迭代每一列并将其作为参考。

allowableCorr = df2_norm.corr(method = 'pearson') 

slope = allowableCorr * (df2_norm.std().values /  df2_norm.std().values[:, np.newaxis])

df2_norm is: df2_norm 是:

 count          716865                 716873                  716884                 716943                 716944

  0   -0.16029615828413712    -0.07630309240006158    0.11220663712532133    -0.2726775504078691    -0.23279127015045065  
  1    -0.6687265363491811     -0.6135022705188075   -0.49097425130988914     -0.736020384028633      -0.705286321388766  
  2    0.06735205699309535     0.07948417451634422    0.09240256047258057     0.0617964313591086     0.06344003100365293  
  3      0.372935701728449     0.44324822316416074     0.5625073287879649     0.3199599294007491      0.3420770859108217  
  4    0.39439310866886124     0.45960496068147993     0.5591549439131621    0.34928093849248304     0.36951024291102974  
  5   -0.08007381002566456   -0.021313801077641505    0.11996141286735541   -0.15572679401876433    -0.12936514230689095  
  6    0.20853071107951396     0.26561990841073535     0.3661990387594055    0.15720649076873264       0.177890807311781  
  7    -0.0488049712326824     0.02909288268076153    0.18643283476719688    -0.1438092892727158    -0.10871022227142838  
  8   0.017648470149950992     0.10136455179350337     0.2722686729095633   -0.07928001803992157   -0.043102822045971705  

enter image description here在此处输入图像描述

why not为什么不

slope =  np.dot(np.dot(np.diagflat(df2_norm.std().values) , allowableCorr.values), np.diagflat(1/df2_norm.std().values))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM