ベクトルの内積による相関係数の算出

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業界で働く。趣味はモータースポーツ。マイブームはダイエット。

関連記事

【Linuxで画像加工】RAW形式のファイルから順次ダーク減算して比較明合成をする方法(メモレベルの覚書)

  for file in IMG_*.CR2 do dcraw -w -j -W -4 $file […]

【linuxで画像加工】CinePaintのインストール

とりあえずは入ったけど、もう少し新しいバージョンがいいな。

Mastodonで出来ること、出来ないこと。

Mastodonのインスタンスを立ち上げて色々と試してみたので、情報共有します!

PHPでcsvを2次元配列に読み込む

<?php $file_name = “data.csv”; $fp = fopen( $file_na […]

[メモ]CentOS5.6のPHPをソース版に入れ替える

CentOS5.6にデフォルトでインストールされるPHPではなく、必要最低限(?)のモジュールのみでコンパイルしたPHPに入れ替える必要があったので、その手順をメモ。