上から順に自分で実行したい場合は、
逆に全部計算された状態のノートブックを得たい場合は、
です。
まず ipython 内部で matplotlib を使える設定を行い、
を import します。
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets
次にサンプルデータセットの Boston をロードします。
これは http://archive.ics.uci.edu/ml/datasets/Housing で公開されているボストン郊外の家の価格のデータです。 UC Irvine Machine Learning Repository (by Bache, K. & Lichman, M. (2013)) では機械学習のアルゴリズムの比較用や学習用に便利な標準データを公開しています。
そして、本文書のようなチュートリアル作成時に便利なように、scikit-learn ライブラリではこのデータを手軽に利用できるように、利用しやすい形で予め用意しています。
DESCR 属性にデータの説明文字列が入っています。 DESCR の説明を読むと、target に目的変数(家の価格の中央値)が、data には上記の素性データの数値が入っていることがわかります。
boston = datasets.load_boston()
print boston.DESCR
Boston House Prices dataset Notes ------ Data Set Characteristics: :Number of Instances: 506 :Number of Attributes: 13 numeric/categorical predictive :Median Value (attribute 14) is usually the target :Attribute Information (in order): - CRIM per capita crime rate by town - ZN proportion of residential land zoned for lots over 25,000 sq.ft. - INDUS proportion of non-retail business acres per town - CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise) - NOX nitric oxides concentration (parts per 10 million) - RM average number of rooms per dwelling - AGE proportion of owner-occupied units built prior to 1940 - DIS weighted distances to five Boston employment centres - RAD index of accessibility to radial highways - TAX full-value property-tax rate per $10,000 - PTRATIO pupil-teacher ratio by town - B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town - LSTAT % lower status of the population - MEDV Median value of owner-occupied homes in $1000's :Missing Attribute Values: None :Creator: Harrison, D. and Rubinfeld, D.L. This is a copy of UCI ML housing dataset. http://archive.ics.uci.edu/ml/datasets/Housing This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University. The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic prices and the demand for clean air', J. Environ. Economics & Management, vol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics ...', Wiley, 1980. N.B. Various transformations are used in the table on pages 244-261 of the latter. The Boston house-price data has been used in many machine learning papers that address regression problems. **References** - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261. - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann. - many more! (see http://archive.ics.uci.edu/ml/datasets/Housing)
データに含まれる項目は keys() で確認できます。keys() はディクショナリのキー全体をリストとして取得するメソッドです。
boston.keys()
['data', 'feature_names', 'DESCR', 'target']
データの数などを調べるには data.shape() を用います。返り値は (サンプル数, 次元数) です。
boston.data.shape
(506, 13)
13個の項目名は feature_names で確認できます。
boston.feature_names
array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='|S7')
データを DataFrame と呼ばれる標準的な形式に変換してみましょう。全部表示するのは大変なので先頭だけ表示します。
df = pd.DataFrame(boston.data)
df.head()
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.00632 | 18 | 2.31 | 0 | 0.538 | 6.575 | 65.2 | 4.0900 | 1 | 296 | 15.3 | 396.90 | 4.98 |
1 | 0.02731 | 0 | 7.07 | 0 | 0.469 | 6.421 | 78.9 | 4.9671 | 2 | 242 | 17.8 | 396.90 | 9.14 |
2 | 0.02729 | 0 | 7.07 | 0 | 0.469 | 7.185 | 61.1 | 4.9671 | 2 | 242 | 17.8 | 392.83 | 4.03 |
3 | 0.03237 | 0 | 2.18 | 0 | 0.458 | 6.998 | 45.8 | 6.0622 | 3 | 222 | 18.7 | 394.63 | 2.94 |
4 | 0.06905 | 0 | 2.18 | 0 | 0.458 | 7.147 | 54.2 | 6.0622 | 3 | 222 | 18.7 | 396.90 | 5.33 |
データに項目名をつけて、target となる価格も MEDV という名前をつけて表に追加します。
df.columns = boston.feature_names
df['MEDV'] = boston.target
df.head()
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT | MEDV | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.00632 | 18 | 2.31 | 0 | 0.538 | 6.575 | 65.2 | 4.0900 | 1 | 296 | 15.3 | 396.90 | 4.98 | 24.0 |
1 | 0.02731 | 0 | 7.07 | 0 | 0.469 | 6.421 | 78.9 | 4.9671 | 2 | 242 | 17.8 | 396.90 | 9.14 | 21.6 |
2 | 0.02729 | 0 | 7.07 | 0 | 0.469 | 7.185 | 61.1 | 4.9671 | 2 | 242 | 17.8 | 392.83 | 4.03 | 34.7 |
3 | 0.03237 | 0 | 2.18 | 0 | 0.458 | 6.998 | 45.8 | 6.0622 | 3 | 222 | 18.7 | 394.63 | 2.94 | 33.4 |
4 | 0.06905 | 0 | 2.18 | 0 | 0.458 | 7.147 | 54.2 | 6.0622 | 3 | 222 | 18.7 | 396.90 | 5.33 | 36.2 |
簡単な集計値は describe() で計算してくれます。
df.describe()
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT | MEDV | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 | 506.000000 |
mean | 3.593761 | 11.363636 | 11.136779 | 0.069170 | 0.554695 | 6.284634 | 68.574901 | 3.795043 | 9.549407 | 408.237154 | 18.455534 | 356.674032 | 12.653063 | 22.532806 |
std | 8.596783 | 23.322453 | 6.860353 | 0.253994 | 0.115878 | 0.702617 | 28.148861 | 2.105710 | 8.707259 | 168.537116 | 2.164946 | 91.294864 | 7.141062 | 9.197104 |
min | 0.006320 | 0.000000 | 0.460000 | 0.000000 | 0.385000 | 3.561000 | 2.900000 | 1.129600 | 1.000000 | 187.000000 | 12.600000 | 0.320000 | 1.730000 | 5.000000 |
25% | 0.082045 | 0.000000 | 5.190000 | 0.000000 | 0.449000 | 5.885500 | 45.025000 | 2.100175 | 4.000000 | 279.000000 | 17.400000 | 375.377500 | 6.950000 | 17.025000 |
50% | 0.256510 | 0.000000 | 9.690000 | 0.000000 | 0.538000 | 6.208500 | 77.500000 | 3.207450 | 5.000000 | 330.000000 | 19.050000 | 391.440000 | 11.360000 | 21.200000 |
75% | 3.647423 | 12.500000 | 18.100000 | 0.000000 | 0.624000 | 6.623500 | 94.075000 | 5.188425 | 24.000000 | 666.000000 | 20.200000 | 396.225000 | 16.955000 | 25.000000 |
max | 88.976200 | 100.000000 | 27.740000 | 1.000000 | 0.871000 | 8.780000 | 100.000000 | 12.126500 | 24.000000 | 711.000000 | 22.000000 | 396.900000 | 37.970000 | 50.000000 |
簡単に集計グラフを作ってみましょう。 横軸に価格、縦軸に度数をとったヒストグラムを作ってみます。
plt.hist(df.MEDV)
plt.title('Housing prices')
plt.xlabel('price')
plt.ylabel('frequency')
plt.show()
価格と平均部屋数の関係を散布図に表示してみます。
plt.scatter(df.RM, df.MEDV)
plt.title('Average Number of Rooms vs Housing prices')
plt.xlabel('average number of rooms')
plt.ylabel('price')
plt.show()
全属性の間の散布図のマトリックスを表示することも出来ます。(表示されるまでに時間がかかります。不要な文字出力を抑制したい場合は、scatter_matrixの行の行末にセミコロンを追加してみてください)
from pandas.tools.plotting import scatter_matrix
scatter_matrix(df, figsize=(14, 14), diagonal='hist')
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10b513ed0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b5cab90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b758710>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b7bcc10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c140990>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b7e1690>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c2353d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c2adf50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c31e610>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c3a12d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c3f0d90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c57ed50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c604910>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c674510>], [<matplotlib.axes._subplots.AxesSubplot object at 0x10c6f9410>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c852fd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c8e01d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c88d850>, <matplotlib.axes._subplots.AxesSubplot object at 0x10c9ca690>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ca4f590>, <matplotlib.axes._subplots.AxesSubplot object at 0x10cab2ad0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10cb39790>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b786050>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b4a3910>, <matplotlib.axes._subplots.AxesSubplot object at 0x10b55e810>, <matplotlib.axes._subplots.AxesSubplot object at 0x10cbf9c10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10cd7c8d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10cde43d0>], [<matplotlib.axes._subplots.AxesSubplot object at 0x10ce67390>, <matplotlib.axes._subplots.AxesSubplot object at 0x10cee2f10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d04fa50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d0d4950>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d23b610>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d2bd7d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d269ad0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d3a6c90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d42ab90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d49c110>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d514d90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d56e790>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d5fe750>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d783310>], [<matplotlib.axes._subplots.AxesSubplot object at 0x10d7e4e10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d86ad10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10d8d09d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10da54b90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10da01d10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10db4b090>, <matplotlib.axes._subplots.AxesSubplot object at 0x10dbc1f50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10dc10e10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10dc98810>, <matplotlib.axes._subplots.AxesSubplot object at 0x10dd08410>, <matplotlib.axes._subplots.AxesSubplot object at 0x10dd96190>, <matplotlib.axes._subplots.AxesSubplot object at 0x10df0de10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10df73dd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10dff7e90>], [<matplotlib.axes._subplots.AxesSubplot object at 0x10e15f650>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e1e1810>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e18ebd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e2c9d90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e54fc90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e5c0150>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e638dd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e6917d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e723790>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e7a7350>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e809e50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e88fd50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e8f5a10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ea78bd0>], [<matplotlib.axes._subplots.AxesSubplot object at 0x10ea25d50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10eb700d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ebe4f90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ec56510>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ecdc1d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10e112950>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ed99ad0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ef1d690>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ef82e10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10f908ad0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10f96e690>, <matplotlib.axes._subplots.AxesSubplot object at 0x10f9f2650>, <matplotlib.axes._subplots.AxesSubplot object at 0x10fa76210>, <matplotlib.axes._subplots.AxesSubplot object at 0x10fad9d10>], [<matplotlib.axes._subplots.AxesSubplot object at 0x10fc5ec10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10fcc68d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x10fe46a90>, <matplotlib.axes._subplots.AxesSubplot object at 0x10fcf2c10>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ff31f50>, <matplotlib.axes._subplots.AxesSubplot object at 0x10ffb5e50>, <matplotlib.axes._subplots.AxesSubplot object at 0x1100273d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1100ab090>, <matplotlib.axes._subplots.AxesSubplot object at 0x1100f9a50>, <matplotlib.axes._subplots.AxesSubplot object at 0x110288a10>, <matplotlib.axes._subplots.AxesSubplot object at 0x11030d5d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x11037e110>, <matplotlib.axes._subplots.AxesSubplot object at 0x1103f5fd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x11045ac90>], [<matplotlib.axes._subplots.AxesSubplot object at 0x1104dfe50>, <matplotlib.axes._subplots.AxesSubplot object at 0x110496550>, <matplotlib.axes._subplots.AxesSubplot object at 0x1106d3350>, <matplotlib.axes._subplots.AxesSubplot object at 0x110858250>, <matplotlib.axes._subplots.AxesSubplot object at 0x1108a6110>, <matplotlib.axes._subplots.AxesSubplot object at 0x11092d450>, <matplotlib.axes._subplots.AxesSubplot object at 0x1109926d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x110a15890>, <matplotlib.axes._subplots.AxesSubplot object at 0x110a9b450>, <matplotlib.axes._subplots.AxesSubplot object at 0x110afdd50>, <matplotlib.axes._subplots.AxesSubplot object at 0x110c82c50>, <matplotlib.axes._subplots.AxesSubplot object at 0x110ce9910>, <matplotlib.axes._subplots.AxesSubplot object at 0x110d6bad0>, <matplotlib.axes._subplots.AxesSubplot object at 0x110d18c50>], [<matplotlib.axes._subplots.AxesSubplot object at 0x110e63f90>, <matplotlib.axes._subplots.AxesSubplot object at 0x110ee8e90>, <matplotlib.axes._subplots.AxesSubplot object at 0x110f5b410>, <matplotlib.axes._subplots.AxesSubplot object at 0x1118110d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x11185da90>, <matplotlib.axes._subplots.AxesSubplot object at 0x1118eda50>, <matplotlib.axes._subplots.AxesSubplot object at 0x111970610>, <matplotlib.axes._subplots.AxesSubplot object at 0x1119e1150>, <matplotlib.axes._subplots.AxesSubplot object at 0x111a67050>, <matplotlib.axes._subplots.AxesSubplot object at 0x111abfcd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x111b42e90>, <matplotlib.axes._subplots.AxesSubplot object at 0x111afb590>, <matplotlib.axes._subplots.AxesSubplot object at 0x111e38390>, <matplotlib.axes._subplots.AxesSubplot object at 0x111ebd290>], [<matplotlib.axes._subplots.AxesSubplot object at 0x1120217d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1120a6490>, <matplotlib.axes._subplots.AxesSubplot object at 0x1120f4e50>, <matplotlib.axes._subplots.AxesSubplot object at 0x112283e10>, <matplotlib.axes._subplots.AxesSubplot object at 0x1123089d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x112379510>, <matplotlib.axes._subplots.AxesSubplot object at 0x1123fc410>, <matplotlib.axes._subplots.AxesSubplot object at 0x110673650>, <matplotlib.axes._subplots.AxesSubplot object at 0x1124bc890>, <matplotlib.axes._subplots.AxesSubplot object at 0x1124d6990>, <matplotlib.axes._subplots.AxesSubplot object at 0x1125a5e10>, <matplotlib.axes._subplots.AxesSubplot object at 0x112727d10>, <matplotlib.axes._subplots.AxesSubplot object at 0x11278ef10>, <matplotlib.axes._subplots.AxesSubplot object at 0x113011bd0>], [<matplotlib.axes._subplots.AxesSubplot object at 0x11306b5d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1130fc590>, <matplotlib.axes._subplots.AxesSubplot object at 0x113183150>, <matplotlib.axes._subplots.AxesSubplot object at 0x1131e4c50>, <matplotlib.axes._subplots.AxesSubplot object at 0x113369b50>, <matplotlib.axes._subplots.AxesSubplot object at 0x1133d0810>, <matplotlib.axes._subplots.AxesSubplot object at 0x1134539d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1133ffd90>, <matplotlib.axes._subplots.AxesSubplot object at 0x11363be90>, <matplotlib.axes._subplots.AxesSubplot object at 0x1136bfd90>, <matplotlib.axes._subplots.AxesSubplot object at 0x113832310>, <matplotlib.axes._subplots.AxesSubplot object at 0x1138a9f90>, <matplotlib.axes._subplots.AxesSubplot object at 0x113904990>, <matplotlib.axes._subplots.AxesSubplot object at 0x113993950>], [<matplotlib.axes._subplots.AxesSubplot object at 0x113a18510>, <matplotlib.axes._subplots.AxesSubplot object at 0x113a8b050>, <matplotlib.axes._subplots.AxesSubplot object at 0x113b01f10>, <matplotlib.axes._subplots.AxesSubplot object at 0x113b66bd0>, <matplotlib.axes._subplots.AxesSubplot object at 0x113bead90>, <matplotlib.axes._subplots.AxesSubplot object at 0x113b959d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x113de0290>, <matplotlib.axes._subplots.AxesSubplot object at 0x113f65190>, <matplotlib.axes._subplots.AxesSubplot object at 0x113fc86d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x11404c390>, <matplotlib.axes._subplots.AxesSubplot object at 0x11409bd50>, <matplotlib.axes._subplots.AxesSubplot object at 0x11412bd10>, <matplotlib.axes._subplots.AxesSubplot object at 0x1141b08d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x11421f410>], [<matplotlib.axes._subplots.AxesSubplot object at 0x1142a2310>, <matplotlib.axes._subplots.AxesSubplot object at 0x1142fcf90>, <matplotlib.axes._subplots.AxesSubplot object at 0x11438d190>, <matplotlib.axes._subplots.AxesSubplot object at 0x114336790>, <matplotlib.axes._subplots.AxesSubplot object at 0x114652690>, <matplotlib.axes._subplots.AxesSubplot object at 0x1146e8410>, <matplotlib.axes._subplots.AxesSubplot object at 0x1147409d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1147c2b90>, <matplotlib.axes._subplots.AxesSubplot object at 0x1146ff950>, <matplotlib.axes._subplots.AxesSubplot object at 0x1148a2e90>, <matplotlib.axes._subplots.AxesSubplot object at 0x114927b50>, <matplotlib.axes._subplots.AxesSubplot object at 0x114997710>, <matplotlib.axes._subplots.AxesSubplot object at 0x114a1c610>, <matplotlib.axes._subplots.AxesSubplot object at 0x114a83150>], [<matplotlib.axes._subplots.AxesSubplot object at 0x114c05310>, <matplotlib.axes._subplots.AxesSubplot object at 0x114ab0990>, <matplotlib.axes._subplots.AxesSubplot object at 0x114ced7d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x114e706d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x114ed5c10>, <matplotlib.axes._subplots.AxesSubplot object at 0x114f5b8d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x114fc12d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x115045290>, <matplotlib.axes._subplots.AxesSubplot object at 0x1150bde10>, <matplotlib.axes._subplots.AxesSubplot object at 0x11512c950>, <matplotlib.axes._subplots.AxesSubplot object at 0x1151b1850>, <matplotlib.axes._subplots.AxesSubplot object at 0x11531a510>, <matplotlib.axes._subplots.AxesSubplot object at 0x11539b6d0>, <matplotlib.axes._subplots.AxesSubplot object at 0x1153499d0>]], dtype=object)
各変数の間の相関係数を計算してみましょう
corrs = df.corr()
corrs
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT | MEDV | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CRIM | 1.000000 | -0.199458 | 0.404471 | -0.055295 | 0.417521 | -0.219940 | 0.350784 | -0.377904 | 0.622029 | 0.579564 | 0.288250 | -0.377365 | 0.452220 | -0.385832 |
ZN | -0.199458 | 1.000000 | -0.533828 | -0.042697 | -0.516604 | 0.311991 | -0.569537 | 0.664408 | -0.311948 | -0.314563 | -0.391679 | 0.175520 | -0.412995 | 0.360445 |
INDUS | 0.404471 | -0.533828 | 1.000000 | 0.062938 | 0.763651 | -0.391676 | 0.644779 | -0.708027 | 0.595129 | 0.720760 | 0.383248 | -0.356977 | 0.603800 | -0.483725 |
CHAS | -0.055295 | -0.042697 | 0.062938 | 1.000000 | 0.091203 | 0.091251 | 0.086518 | -0.099176 | -0.007368 | -0.035587 | -0.121515 | 0.048788 | -0.053929 | 0.175260 |
NOX | 0.417521 | -0.516604 | 0.763651 | 0.091203 | 1.000000 | -0.302188 | 0.731470 | -0.769230 | 0.611441 | 0.668023 | 0.188933 | -0.380051 | 0.590879 | -0.427321 |
RM | -0.219940 | 0.311991 | -0.391676 | 0.091251 | -0.302188 | 1.000000 | -0.240265 | 0.205246 | -0.209847 | -0.292048 | -0.355501 | 0.128069 | -0.613808 | 0.695360 |
AGE | 0.350784 | -0.569537 | 0.644779 | 0.086518 | 0.731470 | -0.240265 | 1.000000 | -0.747881 | 0.456022 | 0.506456 | 0.261515 | -0.273534 | 0.602339 | -0.376955 |
DIS | -0.377904 | 0.664408 | -0.708027 | -0.099176 | -0.769230 | 0.205246 | -0.747881 | 1.000000 | -0.494588 | -0.534432 | -0.232471 | 0.291512 | -0.496996 | 0.249929 |
RAD | 0.622029 | -0.311948 | 0.595129 | -0.007368 | 0.611441 | -0.209847 | 0.456022 | -0.494588 | 1.000000 | 0.910228 | 0.464741 | -0.444413 | 0.488676 | -0.381626 |
TAX | 0.579564 | -0.314563 | 0.720760 | -0.035587 | 0.668023 | -0.292048 | 0.506456 | -0.534432 | 0.910228 | 1.000000 | 0.460853 | -0.441808 | 0.543993 | -0.468536 |
PTRATIO | 0.288250 | -0.391679 | 0.383248 | -0.121515 | 0.188933 | -0.355501 | 0.261515 | -0.232471 | 0.464741 | 0.460853 | 1.000000 | -0.177383 | 0.374044 | -0.507787 |
B | -0.377365 | 0.175520 | -0.356977 | 0.048788 | -0.380051 | 0.128069 | -0.273534 | 0.291512 | -0.444413 | -0.441808 | -0.177383 | 1.000000 | -0.366087 | 0.333461 |
LSTAT | 0.452220 | -0.412995 | 0.603800 | -0.053929 | 0.590879 | -0.613808 | 0.602339 | -0.496996 | 0.488676 | 0.543993 | 0.374044 | -0.366087 | 1.000000 | -0.737663 |
MEDV | -0.385832 | 0.360445 | -0.483725 | 0.175260 | -0.427321 | 0.695360 | -0.376955 | 0.249929 | -0.381626 | -0.468536 | -0.507787 | 0.333461 | -0.737663 | 1.000000 |
表のままでは判りにくいので、表をカラーマップ表現してみましょう。強い相関がある項目の間は濃い色で表現されます。
plt.pcolor(corrs, cmap='bwr', vmin=-1.0, vmax=1.0)
plt.yticks(np.arange(0.5, len(corrs.index), 1), corrs.index)
plt.xticks(np.arange(0.5, len(corrs.columns), 1), corrs.columns)
plt.colorbar()
<matplotlib.colorbar.Colorbar instance at 0x113d8d908>
まず一番標準的な線形回帰を行い、実測値と予測の差をヒストグラムにしてみましょう。 線形回帰は linear_model の中の LinearRegression を使用します。fit でフィッティングを行い、predict で予測値を計算します。
from sklearn.linear_model import LinearRegression
lr = LinearRegression(normalize=True)
lr.fit(boston.data, boston.target)
predicted = lr.predict(boston.data)
plt.hist(boston.target - predicted, bins=50)
plt.show()
zip(boston.feature_names, lr.coef_)
[('CRIM', -0.10717055656035429), ('ZN', 0.046395219529799095), ('INDUS', 0.020860239532173093), ('CHAS', 2.6885613993178934), ('NOX', -17.79575866030898), ('RM', 3.8047524602579923), ('AGE', 0.00075106170331850177), ('DIS', -1.4757587965198151), ('RAD', 0.30565503833909996), ('TAX', -0.012329346305269899), ('PTRATIO', -0.9534635546905571), ('B', 0.0093925127221889167), ('LSTAT', -0.52546663290079032)]
RidgeCV を代わりに使用すれば、Ridge(L2正則化)を交差検定付きで実行してくれます。
Ridge は次の値を最小化するように $w$ を計算します。 $$\frac{1}{2N} \|y - Xw\|^2_2 + \alpha \|w\|_2^2$$ 調整すべきパラメータ $\alpha$ は RidgeCV の中で交差検定を用いて最適化されます。
from sklearn.linear_model import RidgeCV
rcv = RidgeCV(alphas=np.array([0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1.0]), normalize=True)
rcv.fit(boston.data, boston.target)
rcv.alpha_
0.01
zip(boston.feature_names, rcv.coef_)
[('CRIM', -0.10271022863565661), ('ZN', 0.043381103163936223), ('INDUS', 0.0054830659634807852), ('CHAS', 2.7480845085497325), ('NOX', -16.652015485972797), ('RM', 3.860449821453694), ('AGE', -0.00028886503301219592), ('DIS', -1.4137049038849401), ('RAD', 0.26878296104320004), ('TAX', -0.010573015429370438), ('PTRATIO', -0.9352549818745266), ('B', 0.00936468655028788), ('LSTAT', -0.51657414543985458)]
同様に、Lasso(L1正則化)も交差検定付きで試してみます。
Lasso は次の値を最小化するように $w$ を計算します。 $$\frac{1}{2N} \|y - Xw\|^2_2 + \alpha \|w\|_1$$ 調整すべきパラメータ $\alpha$ は LassoCV の中で交差検定を用いて最適化されます。
from sklearn.linear_model import LassoCV
lcv = LassoCV(alphas=np.array([0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1.0]), normalize=True)
lcv.fit(boston.data, boston.target)
lcv.alpha_
0.01
係数をみると、0 になっているものが4つ('INDUS', 'AGE', 'RAD', 'TAX')あるのが判ります。Lassoを使うことで、説明変数を簡単に絞り込むことができます。
それらの変数について散布図を作ると、確かに綺麗な相関関係はなさそう(外れ値の影響を受けている?)に見えます。
zip(boston.feature_names, lcv.coef_)
[('CRIM', -0.035814470357667223), ('ZN', 0.013100142228700694), ('INDUS', -0.0), ('CHAS', 2.3546373801504301), ('NOX', -8.5675052079650484), ('RM', 4.2338755140970692), ('AGE', -0.0), ('DIS', -0.74338441842915803), ('RAD', 0.0), ('TAX', -0.0), ('PTRATIO', -0.81908903593384119), ('B', 0.0072753189416042982), ('LSTAT', -0.52099969954440617)]
plt.scatter(df.INDUS, df.MEDV)
<matplotlib.collections.PathCollection at 0x1035940d0>
plt.scatter(df.AGE, df.MEDV)
<matplotlib.collections.PathCollection at 0x11c7ba590>
plt.scatter(df.RAD, df.MEDV)
<matplotlib.collections.PathCollection at 0x11db89a50>
plt.scatter(df.TAX, df.MEDV)
<matplotlib.collections.PathCollection at 0x11dda0f10>
時間が余っているひとは ElasticNetCV も試してみてください。
from sklearn.linear_model import ElasticNetCV
encv = ElasticNetCV(alphas=np.array([0.0001, 0.0003, 0.01, 0.03, 0.1, 0.3, 1.0]), \
l1_ratio=np.array([0.5, 0.8, 0.9, 0.95, 0.99, 0.995, 1.0]), normalize=True)
encv.fit(boston.data, boston.target)
zip(boston.feature_names, encv.coef_)
[('CRIM', -0.052422882256581427), ('ZN', 0.013229767571307456), ('INDUS', -0.056642579542418789), ('CHAS', 2.2545397276957306), ('NOX', -3.7655903017514341), ('RM', 3.4505864947903389), ('AGE', -0.0030697250082558983), ('DIS', -0.26375547328424648), ('RAD', 0.0), ('TAX', -0.0021533039435720357), ('PTRATIO', -0.61567102542265739), ('B', 0.0063601151272272552), ('LSTAT', -0.33050795754315432)]
encv.alpha_, encv.l1_ratio_
(0.01, 0.90000000000000002)