correlation
numpy
numpy.corrcoef: Pearson product-moment correlation coefficients
https://docs.scipy.org/doc/numpy/reference/generated/numpy.corrcoef.html#numpy.corrcoef
code:python
from numpy import np
T = np.array(1.3, 4.5, 2.8, 3.9)
P = np.array(2.7, 8.7, 4.7, 8.2)
print(np.corrcoef(T,P))
[ 1. 0.98062258
0.98062258 1. ]
code:python
rho = np.array(8.5, 5.2, 6.9, 6.5)
data = np.column_stack(T,P,rho)
print(np.corrcoef(T,P,rho)) # correlation matrix of T,P and rho
[ 1. 0.98062258 -0.97090288
0.98062258 1. -0.91538464
-0.97090288 -0.91538464 1. ]
Use numpy.ma.corrcoef for masked array
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.corrcoef.html
The Glowing Python: Visualizing correlation matrices
https://glowingpython.blogspot.com/2012/10/visualizing-correlation-matrices.html
Pandas
pandas.Series.corr
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.corr.html#pandas.Series.corr
pandas.DataFrame.corr
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.corr.html
pandas.DataFrame.corrwith
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.corrwith.html#pandas.DataFrame.corrwith
Seaborn
heatmap
Plotting a diagonal correlation matrix
https://seaborn.pydata.org/examples/many_pairwise_correlations.html
Seabornで相関をヒートマップにする(行・列を並び替えながら) / Heatmap using Seaborn (order rows and columns as you like) - Qiita
https://qiita.com/Ken-Kuroki/items/81a09c956118e04cd00f
clustermap
https://seaborn.pydata.org/generated/seaborn.clustermap.html
seaborn の clustermap をちゃんと理解する - nykergoto’s blog https://nykergoto.hatenablog.jp/entry/2018/11/19/seaborn_の_clustermap_をちゃんと理解する
Multi-dimension
code:python
def calculate_correlation_nd(data1, data2, axis = 0):
nt = data1.shapeaxis
assert data1.shape == data2.shape
view1 = data1
view2 = data2
if axis:
view1 = np.rollaxis(data1, axis)
view2 = np.rollaxis(data2, axis)
data1_norm = (view1 - data1.mean(axis=axis)) / data1.std(axis=axis)
data2_norm = (view2 - data2.mean(axis=axis)) / data2.std(axis=axis)
return np.sum(data1_norm * data2_norm / float(nt), axis=0)
from numpy.corrcoef returns 1 when applied on 2 matrices. · Issue '#4819 · numpy/numpy · GitHub
https://github.com/numpy/numpy/issues/4819
python - Computing the correlation coefficient between two multi-dimensional arrays - Stack Overflow
https://stackoverflow.com/questions/30143417/computing-the-correlation-coefficient-between-two-multi-dimensional-arrays
See also
regression
autocorrelation