ベクトルの内積による相関係数の算出
CSVから$data[列][行]という2次元配列にいれたデータについて、1列目とその他の列とで相関係数を算出する。
<?php
$norm = array();
$innerProduct = array();
for($col = 0; $col < count($data); $col++){
$norm[$col] = 0;
$ave[$col] = average($data[$col]);
for( $row = 0; $row < count($data[$col]); $row++ ){
$data[$col][$row] = $data[$col][$row] - $ave[$col];
$norm[$col] += pow($data[$col][$row],2);
}
$norm[$col] = sqrt($norm[$col]);
}
for($col = 1; $col < count($data); $col++){
$innerProduct[$col] = 0;
foreach( $data[$col] as $key => $val ){
$innerProduct[$col] += $data["0"][$key]*$val;
}
$r[$col] = $innerProduct[$col] / ($norm["0"] * $norm[$col]);
echo $col + 1 . "列目との相関係数は" . $r[$col] . "\n";
}
exit(0);
?>
The following two tabs change content below.
しゃちょー
有限会社こだまシステム社長。18歳の時からIT業界で働く。趣味はモータースポーツ。マイブームはダイエット。
最新記事 by しゃちょー (全て見る)
- 企業が間違う働き方改革 - 2019年6月26日
- ウンベラータに新しい芽が出てきたよ! - 2019年6月25日
- 【お金】と【ありがとう】についての大事な話。 - 2018年3月16日