関数には, データの次元 x データ数 のサイズのデータ行列を入力します.
>>> import numpy as np
>>> from sklearn.datasets import load_iris
>>> iris = load_iris()
>>> # number of data, dimension
... iris.data.shape
(150, 4)
>>> # Variance-covariance matrix (Transposition of data matrix)
... np.cov(iris.data.T)
array([[ 0.68569351, -0.042434 , 1.27431544, 0.51627069],
[-0.042434 , 0.18997942, -0.32965638, -0.12163937],
[ 1.27431544, -0.32965638, 3.11627785, 1.2956094 ],
[ 0.51627069, -0.12163937, 1.2956094 , 0.58100626]])
>>> # Correlation coefficient matrix (Transposition of data matrix)
... np.corrcoef(iris.data.T)
array([[ 1. , -0.11756978, 0.87175378, 0.81794113],
[-0.11756978, 1. , -0.4284401 , -0.36612593],
[ 0.87175378, -0.4284401 , 1. , 0.96286543],
[ 0.81794113, -0.36612593, 0.96286543, 1. ]])
... iris.data.shape
(150, 4)
>>> # Variance-covariance matrix (Transposition of data matrix)
... np.cov(iris.data.T)
array([[ 0.68569351, -0.042434 , 1.27431544, 0.51627069],
[-0.042434 , 0.18997942, -0.32965638, -0.12163937],
[ 1.27431544, -0.32965638, 3.11627785, 1.2956094 ],
[ 0.51627069, -0.12163937, 1.2956094 , 0.58100626]])
>>> # Correlation coefficient matrix (Transposition of data matrix)
... np.corrcoef(iris.data.T)
array([[ 1. , -0.11756978, 0.87175378, 0.81794113],
[-0.11756978, 1. , -0.4284401 , -0.36612593],
[ 0.87175378, -0.4284401 , 1. , 0.96286543],
[ 0.81794113, -0.36612593, 0.96286543, 1. ]])
上記の例のJupyter Notebookファイルは,GitHubのStatistics.ipynbの"Convariance and Correlation coefficient"で見ることができます.
0 件のコメント :
コメントを投稿