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

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

関連記事

WEBアプリでファンクションキーを使う

 

【linuxで画像加工】RAW(CR2)を16bitのPPMファイルに現像

カレントディレクトリにあるCanonRawフォーマットのデータを16bitのPPMファイルに現像する。

シェルスクリプトで標準入力を一文字ずつ読み込む方法

シェルスクリプトで対話式のツールを作っていると、標準入力を一文字ずつ処理したくなることってありますよね。そんなときはこれを使えばOK!

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

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

プリウス以上のエコカー

ちょっと画像で遊んでみました…