Pythonでデータ解析[Pandas] -その2- 重回帰① [データマイニング]
Pythonのマイニングパッケージをいじってみています。
とりあえず、PandasやScikit-learnを使って重回帰をやってみる。Pandas部分については↑などを参考にしています。Scikit-learnについてはもっぱらGoogle先生だのみ。専門に書かれている本はないのかな?
◆先ずは環境設定
####環境設定 import os import sys import pandas as pd from sklearn import linear_model ####ディレクトリ設定 os.chdir('C:/*******(ワーキングディレクトリ)*****')
◆データ読み込みと状況確認
prs = pd.read_csv('prostate.csv') ##↓データは以下を使用 ##http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/prostate.data ####データ状況確認 prs.info() # #Int64Index: 97 entries, 0 to 96 #Data columns: #lcavol 97 non-null values #lweight 97 non-null values #age 97 non-null values #lbph 97 non-null values #svi 97 non-null values #lcp 97 non-null values #gleason 97 non-null values #pgg45 97 non-null values #lpsa 97 non-null values #train 97 non-null values #dtypes: float64(5), int64(4), object(1) prs.describe() #Out[1]: # lcavol lweight age lbph svi lcp gleason \ #count 97.000000 97.000000 97.000000 97.000000 97.000000 97.000000 97.000000 #mean 1.350010 3.628943 63.865979 0.100356 0.216495 -0.179366 6.752577 #std 1.178625 0.428411 7.445117 1.450807 0.413995 1.398250 0.722134 #min -1.347074 2.374906 41.000000 -1.386294 0.000000 -1.386294 6.000000 #25% 0.512824 3.375880 60.000000 -1.386294 0.000000 -1.386294 6.000000 #50% 1.446919 3.623007 65.000000 0.300105 0.000000 -0.798508 7.000000 #75% 2.127041 3.876396 68.000000 1.558145 0.000000 1.178655 7.000000 #max 3.821004 4.780383 79.000000 2.326302 1.000000 2.904165 9.000000 # # pgg45 lpsa #count 97.000000 97.000000 #mean 24.381443 2.478387 #std 28.204035 1.154329 #min 0.000000 -0.430783 #25% 0.000000 1.731656 #50% 15.000000 2.591516 #75% 40.000000 3.056357 #max 100.000000 5.582932
★".describe()"でRのsummaryと類似出力
◆データ作成
###学習データ train = prs[prs['train'] == 'T'] ###テストデータ test = prs[prs['train'] == 'F'] ###学習データ準備 Y = train.pop('lpsa') #目的変数をセット X = train.ix[:,:'pgg45'] #説明変数をセット
★列の選択がRとは異なる構文。どうやるのか分からずハマった。google先生に聞いてもそれらしいものを見つけるのに苦労した(^^;
★scikit-learnでは目的変数と説明変数を分けておく必要がある
◆いよいよモデリング
###通常の重回帰 prs_ols = linear_model.LinearRegression() prs_ols.fit(X, Y) #係数 prs_ols.coef_ #Out[1]: #array([ 0.57654319, 0.61402 , -0.01900102, 0.14484808, 0.73720864, # -0.20632423, -0.02950288, 0.00946516]) #切片 prs_ols.intercept_ #Out[1]: 0.42917013284910865 #列名を付与して表示 df1 = DataFrame(prs_ols.intercept_, index = ['(intercept)'], columns = ['Est Coeff-ols']) df2 = DataFrame(prs_ols.coef_, index = X, columns = ['Est Coeff-ols']) pd.concat([df1, df2]) # 縦結合(rbind) #Out[1]: # Est Coeff-ols # (intercept) 0.429170 #lcavol 0.564341 #lweight 0.622020 #age -0.021248 #lbph 0.096713 #svi 0.761673 #lcp -0.106051 #gleason 0.049228 #pgg45 0.004458
★RのようにP値や決定係数は出力されないのでしょうか。これだとちょっと使いにくい。Orangeパッケージの方がいいのかな。まだまだ勉強が足りないな~。
★RみたいなPredict関数はあるようだが、なんだかうまくいかなかった。これも宿題。
◆参考サイト
Python:リッジ回帰
http://blog.livedoor.jp/norikazu197768/archives/14995354.html
2013-01-08 18:51
nice!(1)
コメント(1)
トラックバック(0)
<a href="https://www.skorium.com /">카지노사이트</a>
<a href="https://thedropshippingnomad.com /">카지노사이트</a>
<a href="https://www.legumassociates.com /">카지노사이트</a>
by 카지노사이트 (2020-10-31 11:16)