LoginSignup
2

More than 5 years have passed since last update.

Python ライブラリ Pandas で相関係数を求める方法

Last updated at Posted at 2019-02-13

image.png

ぶっちゃけ, 雰囲気で Pandas イジっていますが, 相関係数を超カンタンに求められたので, シェアします.

コード


# pandas をインポート
import pandas as pd

# tsv ファイルを読み込み
data = pd.read_csv(
    'example_reviews.tsv',
    delimiter='\t',
    names=[
        'FOOD',
        'SERVICE',
        'AMBIENCE',
        'COST_PERFORMANCE',
        'TOTAL'
    ]
)

# 相関行列を表示
print(data.corr())

結果


$ python correlation_analysis.py 
                      FOOD   SERVICE  AMBIENCE  COST_PERFORMANCE     TOTAL
FOOD              1.000000  0.599945  0.662754          0.811401  0.914692
SERVICE           0.599945  1.000000  0.644448          0.576259  0.771395
AMBIENCE          0.662754  0.644448  1.000000          0.673517  0.752079
COST_PERFORMANCE  0.811401  0.576259  0.673517          1.000000  0.827508
TOTAL             0.914692  0.771395  0.752079          0.827508  1.000000

サンプルデータ

4.5 4.5 5   4   4.5
3.5 2   4   3   3.5
2   2   4   2   3
4   3   3   4   4
3.5 3   4   3.5 3.5
5   3   4.5 4   4.5
4   3   3   3   4
3   2   3   2   3
4   3   3   5   4
1   1   1   1   1
2.5 1.5 3   2   2
1   2   2.5 1   1
2.5 2   2   3   2.5
2   2.5 3   2   2
3   3   3   3   3
2   2   3.5 2   2
1   1   1   1   1
2   1.5 1.5 2   2
4   1   5   5   3
4   2   2   2.5 3
3.5 3.5 3.5 4   3.5
4   1   3   2   3
3.5 3   3   3.5 3.5
1.5 2   3   2   2
3   1   2   2.5 2
3   3   4   3   3
4   4.5 5   4   4.5
4.5 4.5 4.5 4   4.5
2   2   2   1   1
4.5 4.5 4.5 3   4.5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2