In [1]:
%matplotlib inline


/Users/NIGG/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py:1035: UserWarning: Duplicate key in file "/Users/NIGG/.matplotlib/matplotlibrc", line #515
  (fname, cnt))
/Users/NIGG/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py:1035: UserWarning: Duplicate key in file "/Users/NIGG/.matplotlib/matplotlibrc", line #516
  (fname, cnt))

In [2]:
import numpy as np
import pandas as pd

# 統計用ツール
import statsmodels.api as sm
import statsmodels.tsa.api as tsa
from patsy import dmatrices

# 自作の空間統計用ツール
from spatialstat import *

#描画
import matplotlib.pyplot as plt
from pandas.tools.plotting import autocorrelation_plot
import seaborn as sns
sns.set(font=['IPAmincho'])

#深層学習
import chainer
from chainer import cuda, Function, gradient_check, Variable, optimizers, serializers, utils
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L

import pyper

In [3]:
data = pd.read_csv("TokyoSingle.csv")

In [4]:
data


Out[4]:
CENSUS P S L R RW CY A TS TT ... SOUTH RSD CMD IDD FAR FLR TDQ X Y CITY_CODE
0 13103017002 12400 182.54 100.01 5 4.5 200002 0 10 11.000000 ... 1 1 0 0 60 300 200001 35.63729 139.73630 13103
1 13109015003 7380 113.15 100.01 5 5.1 199910 3 6 22.857143 ... 0 1 0 0 60 200 200001 35.60556 139.70636 13109
2 13110002001 5580 88.96 54.60 3 4.0 200001 0 7 26.285714 ... 0 1 0 0 60 200 200001 35.60913 139.68124 13110
3 13110022002 22600 165.02 341.39 3 6.0 198510 171 6 29.285714 ... 0 1 0 0 50 100 200001 35.61154 139.67188 13110
4 13111035002 4280 87.50 60.69 3 3.3 199812 13 9 28.285714 ... 0 1 0 0 60 200 200001 35.55992 139.73674 13111
5 13111053002 8680 110.67 172.39 5 4.0 196904 369 11 31.857143 ... 1 0 0 1 60 200 200001 35.55233 139.72270 13111
6 13112057001 3960 61.96 80.01 3 2.8 198507 174 4 27.000000 ... 0 1 0 0 60 150 200001 35.66787 139.65393 13112
7 13112044002 6180 101.02 141.17 3 3.8 200003 0 10 29.000000 ... 0 1 0 0 50 150 200001 35.65750 139.66168 13112
8 13112022004 6380 100.40 147.71 4 4.0 198510 171 10 38.000000 ... 0 1 0 0 40 80 200001 35.66951 139.59331 13112
9 13112062004 6850 107.37 82.37 5 6.0 199709 28 1 37.000000 ... 1 0 1 0 80 300 200001 35.64649 139.65967 13112
10 13112035002 11880 160.28 105.80 4 6.0 200003 0 10 37.000000 ... 0 1 0 0 50 100 200001 35.61906 139.63240 13112
11 13113001003 5080 73.71 50.86 3 4.0 199808 17 8 27.000000 ... 0 1 0 0 60 150 200001 35.66452 139.67767 13113
12 13114015002 4680 71.88 64.49 3 4.0 199009 112 8 38.000000 ... 0 1 0 0 60 150 200001 35.72433 139.64977 13114
13 13114006005 5080 70.87 75.97 3 3.4 199910 3 10 36.000000 ... 0 1 0 0 50 150 200001 35.72611 139.63374 13114
14 13114006005 5980 94.19 80.81 4 5.4 199910 3 10 36.000000 ... 0 1 0 0 60 150 200001 35.72611 139.63374 13114
15 13114004003 6380 106.24 132.88 4 4.5 200003 0 9 40.000000 ... 0 1 0 0 40 80 200001 35.73301 139.63364 13114
16 13115005003 3980 69.66 83.32 3 3.0 197602 287 13 27.000000 ... 0 1 0 0 50 100 200001 35.67603 139.64664 13115
17 13115016005 4780 64.31 41.54 3 4.0 200002 0 12 20.000000 ... 0 1 0 0 60 200 200001 35.70289 139.65663 13115
18 13115003001 5580 84.88 60.00 4 3.1 199802 23 8 24.000000 ... 0 1 0 0 60 160 200001 35.70544 139.62915 13115
19 13115014005 5880 96.56 92.91 3 4.0 199406 67 3 41.000000 ... 0 1 0 0 40 80 200001 35.68856 139.60570 13115
20 13115011003 5980 86.34 86.65 4 8.0 199910 3 9 47.000000 ... 0 1 0 0 60 100 200001 35.72274 139.60092 13115
21 13115013002 6290 93.15 106.88 4 4.0 199909 4 10 39.000000 ... 0 1 0 0 60 160 200001 35.67868 139.60912 13115
22 13115025005 6450 98.60 91.17 2 3.3 199201 96 2 35.000000 ... 1 1 0 0 60 200 200001 35.69963 139.63411 13115
23 13115027001 6480 86.12 86.29 3 3.6 199912 1 10 28.000000 ... 0 1 0 0 50 100 200001 35.69725 139.60081 13115
24 13115033001 8280 115.42 97.22 5 4.0 199901 12 25 28.000000 ... 0 1 0 0 50 100 200001 35.69760 139.61537 13115
25 13118006006 5500 98.61 107.37 4 4.0 199909 4 15 20.000000 ... 0 0 0 1 60 300 200001 35.75137 139.78343 13118
26 13120048000 4750 71.68 79.78 3 3.5 199912 1 9 41.000000 ... 0 1 0 0 50 100 200001 35.72433 139.59924 13120
27 13120043001 5650 103.50 121.77 4 5.4 199911 2 12 41.000000 ... 0 1 0 0 50 100 200001 35.74334 139.57628 13120
28 13120043006 6680 102.68 188.63 6 5.0 197303 322 11 41.000000 ... 0 1 0 0 50 100 200001 35.75316 139.57335 13120
29 13120036001 6700 106.82 110.41 4 6.0 199910 3 9 33.571429 ... 0 1 0 0 50 100 200001 35.75177 139.65933 13120
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
77471 13121094002 3780 84.05 80.19 4 7.6 201208 35 6 32.000000 ... 0 1 0 0 60 200 201503 35.78478 139.76843 13121
77472 13121084005 3790 88.49 85.76 4 6.0 201507 0 23 23.000000 ... 1 0 0 1 60 200 201503 35.79751 139.81351 13121
77473 13121069004 3800 90.48 105.71 4 3.3 201510 0 9 31.000000 ... 0 1 0 0 60 200 201503 35.77339 139.77695 13121
77474 13121069001 3880 113.77 77.16 3 4.0 201502 5 6 37.000000 ... 1 1 0 0 60 160 201503 35.77557 139.77872 13121
77475 13121066002 3880 110.95 120.19 4 3.2 199811 200 14 26.000000 ... 0 1 0 0 60 200 201503 35.76407 139.81406 13121
77476 13121035004 3980 102.67 106.48 4 3.8 201505 2 8 29.000000 ... 1 0 0 0 60 200 201503 35.78090 139.79320 13121
77477 13122027004 2480 86.11 82.50 4 8.0 200401 138 33 30.000000 ... 0 1 0 0 60 200 201503 35.78828 139.86160 13122
77478 13122012006 2780 67.96 43.97 2 4.0 201407 12 10 29.000000 ... 0 1 0 0 60 160 201503 35.75344 139.86102 13122
77479 13122026008 3280 79.95 47.64 3 3.2 201506 1 12 26.000000 ... 0 1 0 0 60 200 201503 35.75736 139.83038 13122
77480 13122015005 3280 84.45 92.12 2 6.3 200609 106 9 30.000000 ... 1 1 0 0 60 200 201503 35.76800 139.86273 13122
77481 13122015005 3380 92.74 102.25 3 4.5 201502 5 13 30.000000 ... 0 1 0 0 60 200 201503 35.76640 139.86078 13122
77482 13122006003 3480 93.24 64.06 4 6.0 201504 3 5 37.000000 ... 0 1 0 0 80 200 201503 35.74762 139.87895 13122
77483 13122002008 3480 92.74 67.82 3 6.0 200806 85 20 23.000000 ... 0 1 0 0 60 200 201503 35.73670 139.86658 13122
77484 13122030004 3480 115.02 76.70 4 4.0 201508 0 10 28.000000 ... 0 0 0 1 60 200 201503 35.73710 139.83376 13122
77485 13122019005 3480 91.08 108.20 3 9.0 201504 3 18 30.000000 ... 0 1 0 0 50 150 201503 35.77827 139.87788 13122
77486 13122002002 3630 102.68 60.76 4 4.0 201502 5 14 30.000000 ... 0 0 0 1 60 200 201503 35.73327 139.85588 13122
77487 13122009003 3690 84.87 80.05 4 4.0 201507 0 3 30.000000 ... 0 1 0 0 60 200 201503 35.76002 139.87231 13122
77488 13122009003 3690 84.87 80.05 2 4.0 201506 1 3 41.000000 ... 0 1 0 0 60 160 201503 35.75940 139.87456 13122
77489 13122019006 3690 98.32 84.03 4 4.0 201412 7 10 30.000000 ... 1 1 0 0 60 200 201503 35.77195 139.87851 13122
77490 13122025001 3790 86.84 95.14 4 4.5 201504 3 15 23.000000 ... 0 1 0 0 50 150 201503 35.73881 139.87280 13122
77491 13122009003 3890 83.42 77.86 4 4.0 201507 0 3 30.000000 ... 0 1 0 0 60 200 201503 35.76002 139.87231 13122
77492 13122030004 3890 90.61 96.70 4 4.0 201503 4 13 28.000000 ... 0 0 0 1 60 200 201503 35.73883 139.83432 13122
77493 13122010004 3980 81.93 93.12 3 6.0 201501 6 11 28.000000 ... 1 0 0 1 60 200 201503 35.75322 139.84833 13122
77494 13122025001 3990 88.59 89.46 4 4.5 201506 1 15 23.000000 ... 0 1 0 0 50 150 201503 35.73881 139.87280 13122
77495 13123043004 2780 82.60 57.44 3 4.0 200106 169 15 23.000000 ... 0 0 0 0 60 200 201503 35.72261 139.88423 13123
77496 13123053002 3480 111.56 76.69 4 4.0 201504 3 17 18.000000 ... 0 1 0 0 60 150 201503 35.71659 139.87892 13123
77497 13123021002 3490 86.76 88.59 4 7.5 201505 2 23 32.000000 ... 1 1 0 0 60 150 201503 35.69842 139.87823 13123
77498 13123051002 3600 77.00 78.53 3 8.1 201412 7 18 23.000000 ... 0 1 0 0 60 100 201503 35.72606 139.87268 13123
77499 13123055001 3999 99.77 74.94 4 6.0 201503 4 10 35.000000 ... 0 0 0 0 60 300 201503 35.69921 139.89479 13123
77500 13123015003 4580 91.08 87.38 4 6.0 201506 1 15 37.000000 ... 0 1 0 0 60 150 201503 35.70281 139.91396 13123

77501 rows × 22 columns


In [5]:
data = data.dropna()

In [6]:
CITY_NAME = data['CITY_CODE'].copy()

In [7]:
CITY_NAME[CITY_NAME == 13101] = '01千代田区'
CITY_NAME[CITY_NAME == 13102] = "02中央区"
CITY_NAME[CITY_NAME == 13103] = "03港区"
CITY_NAME[CITY_NAME == 13104] = "04新宿区"
CITY_NAME[CITY_NAME == 13105] = "05文京区"
CITY_NAME[CITY_NAME == 13106] = "06台東区"
CITY_NAME[CITY_NAME == 13107] = "07墨田区"
CITY_NAME[CITY_NAME == 13108] = "08江東区"
CITY_NAME[CITY_NAME == 13109] = "09品川区"
CITY_NAME[CITY_NAME == 13110] = "10目黒区"
CITY_NAME[CITY_NAME == 13111] = "11大田区"
CITY_NAME[CITY_NAME == 13112] = "12世田谷区"
CITY_NAME[CITY_NAME == 13113] = "13渋谷区"
CITY_NAME[CITY_NAME == 13114] = "14中野区"
CITY_NAME[CITY_NAME == 13115] = "15杉並区"
CITY_NAME[CITY_NAME == 13116] = "16豊島区"
CITY_NAME[CITY_NAME == 13117] = "17北区"
CITY_NAME[CITY_NAME == 13118] = "18荒川区"
CITY_NAME[CITY_NAME == 13119] = "19板橋区"
CITY_NAME[CITY_NAME == 13120] = "20練馬区"
CITY_NAME[CITY_NAME == 13121] = "21足立区"
CITY_NAME[CITY_NAME == 13122] = "22葛飾区"
CITY_NAME[CITY_NAME == 13123] = "23江戸川区"

In [8]:
#Make Japanese Block name
BLOCK = data["CITY_CODE"].copy()
BLOCK[BLOCK == 13101] = "01都心・城南"
BLOCK[BLOCK == 13102] = "01都心・城南"
BLOCK[BLOCK == 13103] = "01都心・城南"
BLOCK[BLOCK == 13104] = "01都心・城南"
BLOCK[BLOCK == 13109] = "01都心・城南"
BLOCK[BLOCK == 13110] = "01都心・城南"
BLOCK[BLOCK == 13111] = "01都心・城南"
BLOCK[BLOCK == 13112] = "01都心・城南"
BLOCK[BLOCK == 13113] = "01都心・城南"
BLOCK[BLOCK == 13114] = "02城西・城北"
BLOCK[BLOCK == 13115] = "02城西・城北"
BLOCK[BLOCK == 13105] = "02城西・城北"
BLOCK[BLOCK == 13106] = "02城西・城北"
BLOCK[BLOCK == 13116] = "02城西・城北"
BLOCK[BLOCK == 13117] = "02城西・城北"
BLOCK[BLOCK == 13119] = "02城西・城北"
BLOCK[BLOCK == 13120] = "02城西・城北"
BLOCK[BLOCK == 13107] = "03城東"
BLOCK[BLOCK == 13108] = "03城東"
BLOCK[BLOCK == 13118] = "03城東"
BLOCK[BLOCK == 13121] = "03城東"
BLOCK[BLOCK == 13122] = "03城東"
BLOCK[BLOCK == 13123] = "03城東"

In [9]:
names = list(data.columns) + ['CITY_NAME', 'BLOCK']
data = pd.concat((data, CITY_NAME, BLOCK), axis = 1)
data.columns = names

変数名とデータの内容メモ

CENSUS: 市区町村コード(9桁)
P:      成約価格
S:      専有面積
L:      土地面積
R:      部屋数
RW:     前面道路幅員
CY:     建築年
A:      建築後年数(成約時)
TS:     最寄駅までの距離
TT:     東京駅までの時間
ACC:    ターミナル駅までの時間
WOOD:   木造ダミー
SOUTH:  南向きダミー
RSD:    住居系地域ダミー
CMD:    商業系地域ダミー
IDD:    工業系地域ダミー
FAR:    建ぺい率
FLR:    容積率
TDQ:    成約時点(四半期)
X:      緯度
Y:      経度
CITY_CODE: 市区町村コード(5桁)
CITY_NAME: 市区町村名
BLOCK:     地域ブロック名

In [10]:
s_data = data[['P', 'S', 'L', 'R', 'A', 'RW', 'TS', 'TT']]

In [11]:
s_data.describe()


Out[11]:
P S L R A RW TS TT
count 77388.000000 77388.000000 77388.000000 77388.000000 77388.000000 77388.000000 77388.000000 77388.000000
mean 5745.409689 101.496476 86.631660 3.488978 50.046067 4.898872 10.339898 30.807698
std 3056.826263 35.732280 40.390705 0.918125 92.814379 1.997453 4.865877 7.738183
min 850.000000 31.190000 19.610000 0.000000 0.000000 2.000000 0.000000 1.000000
25% 3980.000000 84.250000 60.780000 3.000000 1.000000 4.000000 7.000000 26.000000
50% 4980.000000 94.600000 79.430000 3.000000 4.000000 4.000000 10.000000 31.000000
75% 6380.000000 106.510000 100.200000 4.000000 46.000000 5.500000 13.000000 37.000000
max 30000.000000 497.810000 495.860000 47.000000 412.000000 35.000000 69.000000 154.000000

市区町村別の件数を集計


In [12]:
print(data['CITY_NAME'].value_counts())


12世田谷区    12340
20練馬区      9979
15杉並区      8131
11大田区      7052
21足立区      6479
19板橋区      4827
14中野区      3924
10目黒区      3418
22葛飾区      3165
23江戸川区     3156
09品川区      2424
16豊島区      2153
04新宿区      1885
17北区       1799
13渋谷区      1487
05文京区      1242
18荒川区      1005
08江東区       981
03港区        757
07墨田区       725
06台東区       371
02中央区        56
01千代田区       32
Name: CITY_NAME, dtype: int64

成約時点別×市区町村別の件数を集計


In [13]:
print(data.pivot_table(index=['TDQ'], columns=['CITY_NAME']))


                 CENSUS                                            \
CITY_NAME        01千代田区         02中央区          03港区         04新宿区   
TDQ                                                                 
200001     1.310105e+10           NaN  1.310302e+10  1.310406e+10   
200002              NaN           NaN  1.310302e+10  1.310405e+10   
200003     1.310100e+10           NaN  1.310302e+10  1.310405e+10   
200004              NaN           NaN  1.310302e+10  1.310405e+10   
200101              NaN  1.310202e+10  1.310302e+10  1.310405e+10   
200102              NaN  1.310202e+10  1.310302e+10  1.310406e+10   
200103     1.310105e+10           NaN  1.310302e+10  1.310406e+10   
200104              NaN           NaN  1.310302e+10  1.310405e+10   
200201              NaN           NaN  1.310302e+10  1.310406e+10   
200202     1.310105e+10  1.310204e+10  1.310302e+10  1.310405e+10   
200203     1.310105e+10           NaN  1.310302e+10  1.310406e+10   
200204     1.310105e+10  1.310203e+10  1.310302e+10  1.310406e+10   
200301     1.310105e+10  1.310201e+10  1.310302e+10  1.310405e+10   
200302     1.310101e+10  1.310204e+10  1.310302e+10  1.310406e+10   
200303     1.310104e+10  1.310201e+10  1.310301e+10  1.310406e+10   
200304              NaN  1.310202e+10  1.310302e+10  1.310405e+10   
200401              NaN  1.310202e+10  1.310302e+10  1.310405e+10   
200402     1.310104e+10  1.310201e+10  1.310302e+10  1.310406e+10   
200403     1.310104e+10  1.310202e+10  1.310302e+10  1.310405e+10   
200404              NaN  1.310202e+10  1.310302e+10  1.310406e+10   
200501              NaN  1.310201e+10  1.310302e+10  1.310406e+10   
200502              NaN           NaN  1.310302e+10  1.310406e+10   
200503              NaN  1.310203e+10  1.310302e+10  1.310406e+10   
200504              NaN  1.310203e+10  1.310301e+10  1.310406e+10   
200601              NaN           NaN  1.310302e+10  1.310406e+10   
200602              NaN           NaN  1.310302e+10  1.310405e+10   
200603              NaN  1.310201e+10  1.310302e+10  1.310406e+10   
200604              NaN  1.310204e+10  1.310302e+10  1.310406e+10   
200701              NaN           NaN  1.310302e+10  1.310405e+10   
200702              NaN  1.310201e+10  1.310302e+10  1.310406e+10   
...                 ...           ...           ...           ...   
200802              NaN  1.310203e+10  1.310302e+10  1.310406e+10   
200803              NaN  1.310203e+10  1.310302e+10  1.310405e+10   
200804              NaN           NaN  1.310302e+10  1.310407e+10   
200901              NaN           NaN  1.310302e+10  1.310405e+10   
200902              NaN           NaN  1.310300e+10  1.310406e+10   
200903              NaN           NaN  1.310301e+10  1.310405e+10   
200904              NaN  1.310204e+10  1.310302e+10  1.310406e+10   
201001              NaN           NaN  1.310302e+10  1.310406e+10   
201002              NaN  1.310201e+10  1.310302e+10  1.310405e+10   
201003              NaN           NaN  1.310302e+10  1.310405e+10   
201004              NaN  1.310201e+10  1.310302e+10  1.310405e+10   
201101              NaN           NaN  1.310302e+10  1.310405e+10   
201102              NaN  1.310201e+10  1.310302e+10  1.310406e+10   
201103              NaN  1.310202e+10  1.310302e+10  1.310406e+10   
201104              NaN           NaN  1.310301e+10  1.310406e+10   
201201     1.310100e+10           NaN  1.310302e+10  1.310405e+10   
201202     1.310105e+10  1.310202e+10  1.310301e+10  1.310406e+10   
201203              NaN           NaN  1.310302e+10  1.310406e+10   
201204              NaN  1.310202e+10  1.310302e+10  1.310405e+10   
201301              NaN  1.310202e+10  1.310301e+10  1.310406e+10   
201302     1.310104e+10  1.310201e+10  1.310302e+10  1.310406e+10   
201303              NaN           NaN  1.310301e+10  1.310406e+10   
201304     1.310105e+10           NaN  1.310302e+10  1.310405e+10   
201401              NaN  1.310201e+10  1.310302e+10  1.310405e+10   
201402     1.310105e+10           NaN  1.310302e+10  1.310406e+10   
201403              NaN           NaN  1.310302e+10  1.310405e+10   
201404              NaN  1.310201e+10  1.310302e+10  1.310406e+10   
201501     1.310102e+10           NaN  1.310302e+10  1.310406e+10   
201502     1.310100e+10  1.310201e+10  1.310302e+10  1.310405e+10   
201503              NaN           NaN           NaN  1.310405e+10   

                                                                   \
CITY_NAME         05文京区         06台東区         07墨田区         08江東区   
TDQ                                                                 
200001     1.310501e+10  1.310602e+10  1.310703e+10  1.310803e+10   
200002     1.310501e+10  1.310603e+10  1.310702e+10  1.310802e+10   
200003     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
200004     1.310501e+10  1.310602e+10  1.310701e+10  1.310801e+10   
200101     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200102     1.310501e+10  1.310603e+10  1.310701e+10  1.310802e+10   
200103     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200104     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200201     1.310501e+10  1.310602e+10           NaN  1.310802e+10   
200202     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
200203     1.310501e+10  1.310603e+10  1.310702e+10  1.310802e+10   
200204     1.310501e+10  1.310602e+10  1.310702e+10  1.310801e+10   
200301     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
200302     1.310501e+10  1.310601e+10  1.310702e+10  1.310803e+10   
200303     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
200304     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200401     1.310501e+10  1.310602e+10  1.310702e+10  1.310801e+10   
200402     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
200403     1.310501e+10  1.310602e+10  1.310702e+10  1.310801e+10   
200404     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200501     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200502     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200503     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
200504     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
200601     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
200602     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
200603     1.310501e+10  1.310601e+10  1.310701e+10  1.310802e+10   
200604     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
200701     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
200702     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
...                 ...           ...           ...           ...   
200802     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200803     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200804     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200901     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200902     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
200903     1.310501e+10  1.310601e+10  1.310701e+10  1.310801e+10   
200904     1.310501e+10  1.310601e+10  1.310701e+10  1.310802e+10   
201001     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
201002     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
201003     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
201004     1.310501e+10  1.310601e+10  1.310701e+10  1.310802e+10   
201101     1.310501e+10  1.310601e+10  1.310701e+10  1.310801e+10   
201102     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
201103     1.310501e+10  1.310602e+10  1.310701e+10  1.310801e+10   
201104     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
201201     1.310501e+10  1.310603e+10  1.310702e+10  1.310802e+10   
201202     1.310501e+10  1.310603e+10  1.310702e+10  1.310802e+10   
201203     1.310501e+10  1.310601e+10  1.310702e+10  1.310802e+10   
201204     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
201301     1.310501e+10  1.310602e+10  1.310702e+10  1.310801e+10   
201302     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
201303     1.310501e+10           NaN  1.310701e+10  1.310803e+10   
201304     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
201401     1.310501e+10  1.310601e+10  1.310701e+10  1.310802e+10   
201402     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
201403     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
201404     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   
201501     1.310501e+10  1.310601e+10  1.310701e+10  1.310802e+10   
201502     1.310501e+10  1.310602e+10  1.310701e+10  1.310802e+10   
201503     1.310501e+10  1.310602e+10  1.310702e+10  1.310802e+10   

                                       ...   CITY_CODE                       \
CITY_NAME         09品川区         10目黒区  ...       14中野区  15杉並区  16豊島区   17北区   
TDQ                                    ...                                    
200001     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200002     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200003     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200004     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200101     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200102     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200103     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200104     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200201     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200202     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200203     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200204     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200301     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200302     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200303     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200304     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200401     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200402     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200403     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200404     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200501     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200502     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200503     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200504     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200601     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200602     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200603     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200604     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200701     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200702     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
...                 ...           ...  ...         ...    ...    ...    ...   
200802     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200803     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200804     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200901     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200902     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
200903     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
200904     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201001     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201002     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201003     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201004     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201101     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201102     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
201103     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201104     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201201     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
201202     1.310902e+10  1.311001e+10  ...       13114  13115  13116  13117   
201203     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
201204     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201301     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
201302     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201303     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201304     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201401     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201402     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
201403     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201404     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201501     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   
201502     1.310901e+10  1.311001e+10  ...       13114  13115  13116  13117   
201503     1.310901e+10  1.311002e+10  ...       13114  13115  13116  13117   

                                                     
CITY_NAME  18荒川区  19板橋区  20練馬区  21足立区  22葛飾区 23江戸川区  
TDQ                                                  
200001     13118  13119  13120  13121  13122  13123  
200002     13118  13119  13120  13121  13122  13123  
200003     13118  13119  13120  13121  13122  13123  
200004     13118  13119  13120  13121  13122  13123  
200101     13118  13119  13120  13121  13122  13123  
200102     13118  13119  13120  13121  13122  13123  
200103     13118  13119  13120  13121  13122  13123  
200104     13118  13119  13120  13121  13122  13123  
200201     13118  13119  13120  13121  13122  13123  
200202     13118  13119  13120  13121  13122  13123  
200203     13118  13119  13120  13121  13122  13123  
200204     13118  13119  13120  13121  13122  13123  
200301     13118  13119  13120  13121  13122  13123  
200302     13118  13119  13120  13121  13122  13123  
200303     13118  13119  13120  13121  13122  13123  
200304     13118  13119  13120  13121  13122  13123  
200401     13118  13119  13120  13121  13122  13123  
200402     13118  13119  13120  13121  13122  13123  
200403     13118  13119  13120  13121  13122  13123  
200404     13118  13119  13120  13121  13122  13123  
200501     13118  13119  13120  13121  13122  13123  
200502     13118  13119  13120  13121  13122  13123  
200503     13118  13119  13120  13121  13122  13123  
200504     13118  13119  13120  13121  13122  13123  
200601     13118  13119  13120  13121  13122  13123  
200602     13118  13119  13120  13121  13122  13123  
200603     13118  13119  13120  13121  13122  13123  
200604     13118  13119  13120  13121  13122  13123  
200701     13118  13119  13120  13121  13122  13123  
200702     13118  13119  13120  13121  13122  13123  
...          ...    ...    ...    ...    ...    ...  
200802     13118  13119  13120  13121  13122  13123  
200803     13118  13119  13120  13121  13122  13123  
200804     13118  13119  13120  13121  13122  13123  
200901     13118  13119  13120  13121  13122  13123  
200902     13118  13119  13120  13121  13122  13123  
200903     13118  13119  13120  13121  13122  13123  
200904     13118  13119  13120  13121  13122  13123  
201001     13118  13119  13120  13121  13122  13123  
201002     13118  13119  13120  13121  13122  13123  
201003     13118  13119  13120  13121  13122  13123  
201004     13118  13119  13120  13121  13122  13123  
201101     13118  13119  13120  13121  13122  13123  
201102     13118  13119  13120  13121  13122  13123  
201103     13118  13119  13120  13121  13122  13123  
201104     13118  13119  13120  13121  13122  13123  
201201     13118  13119  13120  13121  13122  13123  
201202     13118  13119  13120  13121  13122  13123  
201203     13118  13119  13120  13121  13122  13123  
201204     13118  13119  13120  13121  13122  13123  
201301     13118  13119  13120  13121  13122  13123  
201302     13118  13119  13120  13121  13122  13123  
201303     13118  13119  13120  13121  13122  13123  
201304     13118  13119  13120  13121  13122  13123  
201401     13118  13119  13120  13121  13122  13123  
201402     13118  13119  13120  13121  13122  13123  
201403     13118  13119  13120  13121  13122  13123  
201404     13118  13119  13120  13121  13122  13123  
201501     13118  13119  13120  13121  13122  13123  
201502     13118  13119  13120  13121  13122  13123  
201503     13118  13119  13120  13121  13122  13123  

[63 rows x 483 columns]

成約時点別×地域ブロック別の件数を集計


In [14]:
print(data.pivot_table(index=['TDQ'], columns=['BLOCK']))


              CENSUS                                        P               \
BLOCK        01都心・城南       02城西・城北          03城東      01都心・城南      02城西・城北   
TDQ                                                                          
200001  1.311033e+10  1.311581e+10  1.312004e+10  7399.858911  5872.318681   
200002  1.311049e+10  1.311637e+10  1.312009e+10  7276.618938  5932.909408   
200003  1.311055e+10  1.311578e+10  1.312082e+10  7139.290816  6176.982456   
200004  1.311079e+10  1.311620e+10  1.312014e+10  7459.767967  5993.241259   
200101  1.311038e+10  1.311591e+10  1.312024e+10  7151.689342  5833.323113   
200102  1.311053e+10  1.311655e+10  1.312063e+10  7554.334135  5519.058796   
200103  1.311082e+10  1.311643e+10  1.312039e+10  7442.875670  5529.286207   
200104  1.311045e+10  1.311618e+10  1.312042e+10  7318.639191  5471.789796   
200201  1.311046e+10  1.311595e+10  1.312069e+10  6842.570213  5469.279188   
200202  1.311071e+10  1.311551e+10  1.312051e+10  6772.594912  5562.571148   
200203  1.311062e+10  1.311589e+10  1.312000e+10  7093.306619  5543.623567   
200204  1.311072e+10  1.311578e+10  1.312049e+10  6833.169727  5685.290974   
200301  1.311070e+10  1.311610e+10  1.311914e+10  7396.902975  5280.512821   
200302  1.311064e+10  1.311602e+10  1.312039e+10  6759.205567  5374.607407   
200303  1.311055e+10  1.311641e+10  1.312097e+10  6939.937391  5201.690043   
200304  1.311050e+10  1.311633e+10  1.312075e+10  7262.770251  5324.960497   
200401  1.311041e+10  1.311636e+10  1.312045e+10  7109.541119  5213.655108   
200402  1.311070e+10  1.311678e+10  1.312044e+10  6923.363345  4987.354267   
200403  1.311062e+10  1.311613e+10  1.311985e+10  7000.491568  5309.601212   
200404  1.311049e+10  1.311601e+10  1.312027e+10  7055.453600  5343.703800   
200501  1.311051e+10  1.311653e+10  1.312005e+10  6886.984151  5202.612126   
200502  1.311050e+10  1.311640e+10  1.311954e+10  7150.359728  5340.393874   
200503  1.311057e+10  1.311634e+10  1.311947e+10  7335.625516  5260.281683   
200504  1.311087e+10  1.311613e+10  1.311917e+10  7835.427525  5541.041437   
200601  1.311073e+10  1.311617e+10  1.311982e+10  7789.539701  5444.865127   
200602  1.311080e+10  1.311664e+10  1.311997e+10  7957.132747  5545.012022   
200603  1.311097e+10  1.311652e+10  1.311953e+10  7899.027634  5567.982686   
200604  1.311078e+10  1.311699e+10  1.311884e+10  8585.407240  5598.507763   
200701  1.311059e+10  1.311669e+10  1.311971e+10  8362.876740  5851.591239   
200702  1.311068e+10  1.311665e+10  1.311951e+10  8699.175887  5747.957524   
...              ...           ...           ...          ...          ...   
200802  1.311091e+10  1.311697e+10  1.311943e+10  8726.384946  5435.136049   
200803  1.311086e+10  1.311677e+10  1.311970e+10  8592.913420  5404.359375   
200804  1.311073e+10  1.311694e+10  1.311946e+10  7785.852273  5267.044248   
200901  1.311073e+10  1.311695e+10  1.311998e+10  6568.142184  5044.784749   
200902  1.311071e+10  1.311678e+10  1.311962e+10  6429.386024  5014.242623   
200903  1.311071e+10  1.311666e+10  1.311935e+10  7150.550905  5034.155670   
200904  1.311046e+10  1.311688e+10  1.312054e+10  6908.893939  5004.963519   
201001  1.311058e+10  1.311705e+10  1.312052e+10  7765.467148  5057.545642   
201002  1.311044e+10  1.311726e+10  1.312051e+10  7537.208481  5019.010515   
201003  1.311045e+10  1.311690e+10  1.311983e+10  7137.339175  4993.959406   
201004  1.311005e+10  1.311683e+10  1.312017e+10  6675.804154  5145.535135   
201101  1.311025e+10  1.311732e+10  1.311987e+10  6976.826165  4984.240534   
201102  1.311049e+10  1.311718e+10  1.311974e+10  6933.179211  5033.169742   
201103  1.311055e+10  1.311719e+10  1.312004e+10  6966.469582  4903.657282   
201104  1.311048e+10  1.311718e+10  1.311971e+10  6328.623408  5035.399886   
201201  1.311027e+10  1.311768e+10  1.312025e+10  6222.344512  4637.095986   
201202  1.311026e+10  1.311673e+10  1.312021e+10  6071.030585  4955.065480   
201203  1.311021e+10  1.311705e+10  1.311968e+10  6715.687831  4769.312000   
201204  1.311053e+10  1.311693e+10  1.312015e+10  6569.068468  4916.640488   
201301  1.311031e+10  1.311692e+10  1.311989e+10  6626.559322  4701.993534   
201302  1.311033e+10  1.311699e+10  1.311966e+10  6388.288571  4971.381910   
201303  1.311061e+10  1.311732e+10  1.311940e+10  6300.783489  4989.930285   
201304  1.311040e+10  1.311717e+10  1.312022e+10  7163.943128  4859.165391   
201401  1.311048e+10  1.311691e+10  1.311948e+10  6822.179104  4911.749465   
201402  1.311025e+10  1.311740e+10  1.311980e+10  7153.518272  5087.525253   
201403  1.311036e+10  1.311722e+10  1.312020e+10  6556.274667  5068.709552   
201404  1.311037e+10  1.311713e+10  1.312025e+10  6852.042691  5078.867982   
201501  1.311047e+10  1.311652e+10  1.312094e+10  6994.015232  5306.357456   
201502  1.311042e+10  1.311782e+10  1.312087e+10  6859.167411  5057.464821   
201503  1.311087e+10  1.311715e+10  1.312066e+10  6568.333333  5211.302752   

                              S                                 L  \
BLOCK          03城東     01都心・城南     02城西・城北       03城東    01都心・城南   
TDQ                                                                 
200001  4342.893939  109.886510  101.313049  95.806061  96.094109   
200002  4263.587500  110.943626   98.588850  96.373875  90.328984   
200003  3885.451613  110.268878  104.887825  89.108871  90.320893   
200004  3877.380952  109.713285  102.114779  91.677238  93.488316   
200101  4000.080645  111.666531   99.680849  93.551452  91.848413   
200102  3998.627907  115.039038   97.776389  92.194496  95.097476   
200103  3938.796296  115.121094   98.338046  90.566944  99.352522   
200104  3807.068345  114.098927   98.800694  90.891799  94.741104   
200201  3946.328696  108.481426   99.895203  94.344870  90.678340   
200202  3850.991736  108.577593  100.753221  94.817438  89.865068   
200203  3902.548077  112.454374  102.413408  96.396442  94.828676   
200204  3765.760563  110.505681  104.168551  95.223239  91.662525   
200301  3711.511628  117.275584  101.960842  94.006589  98.038215   
200302  3563.806849  107.987505  102.074481  93.208356  93.900165   
200303  3632.924779  111.113014   99.758961  91.883363  93.382203   
200304  3527.335676  114.895753  100.466547  92.002486  94.593853   
200401  3741.924476  113.213650  101.440565  90.395385  98.272628   
200402  3669.998561  107.927527   97.883939  92.669281  90.107189   
200403  3699.347656  110.898564   98.960162  94.314531  90.994585   
200404  3892.790576  110.278777   96.764214  94.518796  90.713611   
200501  3623.847390  107.042693   96.776877  94.167229  84.594306   
200502  3620.228175  110.890762   98.323964  92.478373  89.931878   
200503  3832.634884  110.107581   96.561386  95.084791  89.122522   
200504  3743.683516  115.244205   98.122275  92.941209  93.506717   
200601  3810.079182  113.246860   98.441923  93.572268  91.155432   
200602  3944.528873  112.933518   97.822036  97.791444  92.177226   
200603  3852.379779  109.085596   97.203689  92.237794  89.620466   
200604  4066.222022  115.380060   96.780582  95.986534  93.240332   
200701  4036.992537  110.395201   98.892175  95.334216  88.085110   
200702  4051.605455  112.706578   94.409969  93.955855  88.693422   
...             ...         ...         ...        ...        ...   
200802  4073.076950  114.779032   96.855806  97.110887  91.047993   
200803  4048.696226  112.015736   94.480533  93.900679  87.243550   
200804  3934.287037  110.003760   94.639416  95.405093  86.697438   
200901  3748.933929  101.480968   93.525290  94.656036  80.677122   
200902  3767.257053  101.103012   94.692705  95.880721  78.172402   
200903  3732.136508  109.276244   96.970144  92.538476  88.493688   
200904  3676.596667  103.466465   95.532146  92.793815  87.305488   
201001  3586.077295  112.258520   98.282982  93.171353  94.694585   
201002  3629.482072  113.664452   95.185772  94.813386  95.894028   
201003  3635.551331  111.118522   94.095520  93.342433  92.458213   
201004  3633.464052  103.400504   96.477819  95.044216  83.251840   
201101  3519.669611  108.764409   96.603932  91.858763  85.954839   
201102  3770.754839  109.740143   96.366953  94.011323  87.358889   
201103  3632.810036  106.479772   94.378908  94.004373  85.781141   
201104  3536.671381  100.830573   94.504107  94.249318  80.012006   
201201  3434.345238  100.832774   92.668482  96.376756  79.550030   
201202  3334.376068  101.839947   95.896014  94.123789  81.162340   
201203  3379.436170  107.701429   96.543652  96.905142  86.283095   
201204  3403.239645  106.357730   95.730671  95.343077  87.074595   
201301  3452.037037  109.063356   94.608405  97.298287  84.765627   
201302  3483.681004  107.559171   97.460754  96.070538  85.030571   
201303  3462.378601  101.901651   96.244309  95.532099  84.027352   
201304  3494.872910  108.257393   94.492481  93.579197  88.679526   
201401  3580.691630  108.491866   93.916895  94.515154  88.164813   
201402  3532.954545  109.673688   96.399455  93.780083  90.575781   
201403  3572.537549  107.543733   95.648207  96.449328  86.863100   
201404  3484.062469  107.780070   95.239241  97.161235  83.963225   
201501  3442.073982  107.368146   99.350724  96.764376  87.974371   
201502  3552.597533  105.017433   95.557935  95.747089  82.450647   
201503  3499.192857  106.680578   95.268624  94.126857  88.039600   

            ...              FLR          X                                 Y  \
BLOCK       ...             03城東    01都心・城南    02城西・城北       03城東     01都心・城南   
TDQ         ...                                                                 
200001      ...       234.393939  35.630043  35.722496  35.733464  139.680251   
200002      ...       212.500000  35.629669  35.724128  35.729507  139.676333   
200003      ...       202.096774  35.629520  35.725294  35.725902  139.678867   
200004      ...       199.238095  35.633891  35.721874  35.740678  139.671286   
200101      ...       206.322581  35.635773  35.723339  35.738500  139.675384   
200102      ...       206.666667  35.632474  35.727582  35.734954  139.676147   
200103      ...       209.351852  35.628844  35.726785  35.733586  139.672155   
200104      ...       207.266187  35.632836  35.725522  35.735263  139.677330   
200201      ...       211.573913  35.628624  35.722669  35.742449  139.681407   
200202      ...       211.157025  35.629130  35.721566  35.745758  139.675710   
200203      ...       214.423077  35.628354  35.725421  35.744459  139.675646   
200204      ...       205.408451  35.625121  35.721058  35.747268  139.672471   
200301      ...       200.186047  35.626068  35.722233  35.739261  139.672514   
200302      ...       211.986301  35.623800  35.726029  35.750007  139.675078   
200303      ...       213.734513  35.629136  35.731217  35.741070  139.675683   
200304      ...       224.108108  35.625096  35.727924  35.747115  139.678946   
200401      ...       212.657343  35.625870  35.726181  35.740994  139.681423   
200402      ...       216.776978  35.626256  35.731345  35.745774  139.675894   
200403      ...       219.765625  35.627503  35.725403  35.738646  139.675597   
200404      ...       218.324607  35.627143  35.725046  35.732742  139.680131   
200501      ...       218.955823  35.628011  35.730142  35.741997  139.680485   
200502      ...       222.753968  35.626627  35.728124  35.739197  139.677334   
200503      ...       232.497674  35.630453  35.729936  35.738252  139.677789   
200504      ...       218.263736  35.629625  35.726231  35.736446  139.671230   
200601      ...       222.267658  35.630608  35.728927  35.738099  139.676025   
200602      ...       216.158451  35.629633  35.730181  35.739113  139.671838   
200603      ...       212.205882  35.626906  35.729815  35.739319  139.670670   
200604      ...       223.552347  35.625756  35.731999  35.737863  139.675983   
200701      ...       211.977612  35.628254  35.733513  35.734373  139.678770   
200702      ...       220.872727  35.630020  35.733647  35.739891  139.675765   
...         ...              ...        ...        ...        ...         ...   
200802      ...       214.680851  35.627703  35.738356  35.747284  139.673660   
200803      ...       206.709434  35.633291  35.732597  35.739248  139.673331   
200804      ...       208.641975  35.634198  35.733916  35.740282  139.669751   
200901      ...       210.089286  35.630074  35.734541  35.745442  139.672215   
200902      ...       217.269592  35.627954  35.735144  35.746884  139.676090   
200903      ...       213.304762  35.629330  35.732546  35.744821  139.676358   
200904      ...       212.374074  35.633071  35.734704  35.749027  139.674321   
201001      ...       212.309179  35.630347  35.737451  35.755518  139.675238   
201002      ...       223.537849  35.627164  35.737017  35.755349  139.679731   
201003      ...       205.102662  35.632317  35.738675  35.756564  139.673538   
201004      ...       212.787582  35.634778  35.735675  35.759718  139.677998   
201101      ...       209.621908  35.629108  35.739901  35.757462  139.678536   
201102      ...       216.174194  35.631420  35.738100  35.754371  139.673153   
201103      ...       210.774194  35.629988  35.740201  35.758988  139.671948   
201104      ...       207.051581  35.624915  35.733924  35.761114  139.678299   
201201      ...       206.375000  35.624753  35.742329  35.753440  139.682148   
201202      ...       212.062678  35.622668  35.733227  35.760703  139.681607   
201203      ...       213.882979  35.621498  35.737343  35.755607  139.687295   
201204      ...       212.707101  35.620917  35.736367  35.752881  139.682315   
201301      ...       215.537037  35.627383  35.736899  35.745723  139.682754   
201302      ...       212.301075  35.621128  35.737420  35.747653  139.682540   
201303      ...       213.703704  35.627200  35.734930  35.742879  139.679392   
201304      ...       213.347826  35.626072  35.737011  35.746977  139.679402   
201401      ...       212.687225  35.622717  35.735975  35.742254  139.682915   
201402      ...       213.698347  35.622825  35.735145  35.746510  139.682949   
201403      ...       203.857708  35.631445  35.735341  35.750486  139.674674   
201404      ...       206.195062  35.625509  35.736719  35.754400  139.677781   
201501      ...       198.308804  35.622149  35.736509  35.769413  139.683071   
201502      ...       197.643092  35.619530  35.740447  35.763233  139.683499   
201503      ...       199.257143  35.602161  35.735219  35.759738  139.689096   

                                   CITY_CODE                              
BLOCK      02城西・城北        03城東       01都心・城南       02城西・城北          03城東  
TDQ                                                                       
200001  139.649451  139.834092  13110.299505  13115.793956  13120.015152  
200002  139.644771  139.842428  13110.459584  13116.351916  13120.050000  
200003  139.648323  139.845460  13110.525510  13115.761404  13120.790323  
200004  139.643573  139.838385  13110.761807  13116.184149  13120.104762  
200101  139.652287  139.835395  13110.351474  13115.891509  13120.201613  
200102  139.649514  139.837392  13110.495192  13116.530093  13120.596899  
200103  139.647426  139.841819  13110.792411  13116.406897  13120.361111  
200104  139.651751  139.842005  13110.418351  13116.157143  13120.388489  
200201  139.646920  139.833643  13110.431915  13115.928934  13120.660870  
200202  139.651515  139.829698  13110.686888  13115.495798  13120.479339  
200203  139.650306  139.831712  13110.595745  13115.869427  13119.961538  
200204  139.649557  139.829357  13110.694405  13115.764846  13120.457746  
200301  139.646554  139.832306  13110.670481  13116.084249  13119.108527  
200302  139.647117  139.824471  13110.618557  13116.000000  13120.356164  
200303  139.649193  139.834882  13110.518841  13116.389610  13120.929204  
200304  139.647735  139.829131  13110.474910  13116.306630  13120.713514  
200401  139.645021  139.836905  13110.379562  13116.338710  13120.419580  
200402  139.643990  139.833927  13110.674377  13116.763676  13120.410072  
200403  139.644290  139.836949  13110.595520  13116.107071  13119.820312  
200404  139.649179  139.839443  13110.466286  13115.986183  13120.240838  
200501  139.648260  139.833198  13110.479663  13116.508306  13120.016064  
200502  139.647200  139.834959  13110.468027  13116.381982  13119.511905  
200503  139.649991  139.831001  13110.536873  13116.318482  13119.441860  
200504  139.648032  139.838210  13110.838384  13116.106587  13119.142857  
200601  139.654241  139.836145  13110.697674  13116.146051  13119.791822  
200602  139.647578  139.839687  13110.775372  13116.621585  13119.936620  
200603  139.648569  139.833187  13110.946459  13116.501618  13119.496324  
200604  139.645956  139.832390  13110.752640  13116.970320  13118.812274  
200701  139.652187  139.841142  13110.556777  13116.672205  13119.679104  
200702  139.651173  139.832926  13110.648936  13116.631661  13119.483636  
...            ...         ...           ...           ...           ...  
200802  139.656294  139.826465  13110.883513  13116.946274  13119.400709  
200803  139.651862  139.837203  13110.831169  13116.751838  13119.671698  
200804  139.651720  139.835030  13110.702479  13116.923894  13119.419753  
200901  139.657372  139.828553  13110.704715  13116.928571  13119.946429  
200902  139.659791  139.827122  13110.683071  13116.757377  13119.586207  
200903  139.657754  139.829289  13110.680995  13116.641237  13119.317460  
200904  139.654332  139.830089  13110.427609  13116.860515  13120.503704  
201001  139.653327  139.820225  13110.555957  13117.029817  13120.483092  
201002  139.650750  139.826256  13110.409894  13117.243848  13120.474104  
201003  139.656494  139.822836  13110.419244  13116.883663  13119.787072  
201004  139.655110  139.817664  13110.020772  13116.812741  13120.137255  
201101  139.649089  139.823822  13110.225806  13117.298544  13119.833922  
201102  139.652478  139.821606  13110.462366  13117.154506  13119.706452  
201103  139.657300  139.818537  13110.517110  13117.169903  13120.003584  
201104  139.648339  139.811954  13110.453822  13117.161547  13119.658902  
201201  139.650280  139.819006  13110.250000  13117.659686  13120.205357  
201202  139.655550  139.817293  13110.234043  13116.713523  13120.168091  
201203  139.654861  139.819908  13110.179894  13117.031304  13119.645390  
201204  139.657071  139.822542  13110.497297  13116.914634  13120.109467  
201301  139.657950  139.823817  13110.281356  13116.900862  13119.851852  
201302  139.651693  139.827017  13110.302857  13116.974874  13119.627240  
201303  139.651808  139.829478  13110.582555  13117.300813  13119.374486  
201304  139.650595  139.831361  13110.367299  13117.145482  13120.190635  
201401  139.653129  139.829940  13110.455224  13116.886510  13119.449339  
201402  139.645310  139.826844  13110.222591  13117.379798  13119.768595  
201403  139.649722  139.828153  13110.333333  13117.196881  13120.158103  
201404  139.649729  139.819608  13110.343387  13117.106222  13120.209877  
201501  139.659141  139.810189  13110.443709  13116.502193  13120.888305  
201502  139.641752  139.823394  13110.388393  13117.797001  13120.827303  
201503  139.656263  139.825987  13110.840000  13117.133028  13120.621429  

[63 rows x 63 columns]

Histogram

価格(真数)


In [15]:
data['P'].hist()


Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x103a8c898>

価格(自然対数)


In [16]:
(np.log(data['P'])).hist()


Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x1156f5f98>

建築後年数


In [17]:
data['A'].hist()


Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x11573bdd8>

In [18]:
plt.figure(figsize=(20,8))

plt.subplot(4, 2, 1)
data['P'].hist()
plt.title(u"成約価格")

plt.subplot(4, 2, 2)
data['S'].hist()
plt.title("専有面積")

plt.subplot(4, 2, 3)
data['L'].hist()
plt.title("土地面積")

plt.subplot(4, 2, 4)
data['R'].hist()
plt.title("部屋数")

plt.subplot(4, 2, 5)
data['A'].hist()
plt.title("建築後年数")

plt.subplot(4, 2, 6)
data['RW'].hist()
plt.title("前面道路幅員")

plt.subplot(4, 2, 7)
data['TS'].hist()
plt.title("最寄駅までの距離")

plt.subplot(4, 2, 8)
data['TT'].hist()
plt.title(u"東京駅までの時間")


Out[18]:
<matplotlib.text.Text at 0x118569e80>

Plot

件数の推移


In [19]:
plt.figure(figsize=(20,8))
data['TDQ'].value_counts().plot(kind='bar')


Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x115a7a5c0>

In [20]:
plt.figure(figsize=(20,8))
data['CITY_NAME'].value_counts().plot(kind='bar') #市区町村別の件数


Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x11cda8e80>

Main Analysis

OLS part


In [21]:
vars = ['P', 'S', 'L', 'R', 'RW', 'A', 'TS', 'TT', 'WOOD', 'SOUTH', 'CMD', 'IDD', 'FAR', 'X', 'Y']
eq = fml_build(vars)

y, X = dmatrices(eq, data=data, return_type='dataframe')

CITY_NAME = pd.get_dummies(data['CITY_NAME'])
TDQ = pd.get_dummies(data['TDQ'])

X = pd.concat((X, CITY_NAME, TDQ), axis=1)

datas = pd.concat((y, X), axis=1)

datas = datas[datas['12世田谷区'] == 1][0:5000]

In [22]:
datas.head()


Out[22]:
P Intercept S L R RW A TS TT WOOD ... 201302 201303 201304 201401 201402 201403 201404 201501 201502 201503
6 3960 1 61.96 80.01 3 2.8 174 4 27 1 ... 0 0 0 0 0 0 0 0 0 0
7 6180 1 101.02 141.17 3 3.8 0 10 29 1 ... 0 0 0 0 0 0 0 0 0 0
8 6380 1 100.40 147.71 4 4.0 171 10 38 1 ... 0 0 0 0 0 0 0 0 0 0
9 6850 1 107.37 82.37 5 6.0 28 1 37 1 ... 0 0 0 0 0 0 0 0 0 0
10 11880 1 160.28 105.80 4 6.0 0 10 37 1 ... 0 0 0 0 0 0 0 0 0 0

5 rows × 102 columns


In [23]:
vars = ['S', 'L', 'R', 'RW', 'A', 'TS', 'TT', 'WOOD', 'SOUTH', 'CMD', 'IDD', 'FAR']
#vars += vars + list(TDQ.columns)

In [272]:
class CAR(Chain):
    def __init__(self, unit1, unit2, unit3, col_num):
        self.unit1 = unit1
        self.unit2 = unit2
        self.unit3 = unit3
        super(CAR, self).__init__(
            l1 = L.Linear(col_num, unit1),
            l2 = L.Linear(self.unit1, self.unit1),
            l3 = L.Linear(self.unit1, self.unit2),
            l4 = L.Linear(self.unit2, self.unit3),
            l5 = L.Linear(self.unit3, self.unit3),
            l6 = L.Linear(self.unit3, 1),
        )
    
    def __call__(self, x, y):
        fv = self.fwd(x, y)
        print(fv.data.shape)
        print(y.data.shape)
        loss = F.mean_squared_error(fv, y)
        return loss
    
    def fwd(self, x, y):
        h1 = F.sigmoid(self.l1(x))
        h2 = F.sigmoid(self.l2(h1))
        h3 = F.sigmoid(self.l3(h2))
        h4 = F.sigmoid(self.l4(h3))
        h5 = F.sigmoid(self.l5(h4))
        h6 = self.l6(h5)
        return h6

In [273]:
class OLS_DLmodel(object):
    def __init__(self, data, vars, bs=200, n=1000):
        self.vars = vars
        eq = fml_build(vars)
        y, X = dmatrices(eq, data=datas, return_type='dataframe')
        self.y_in = y[:-n]
        self.X_in = X[:-n]
        self.y_ex = y[-n:]
        self.X_ex = X[-n:]
        self.ido_in = data['X'][:-n]
        self.keido_in = data['Y'][:-n]
        self.ido_ex = data['X'][-n:]
        self.keido_ex = data['Y'][-n:]
        
        self.logy_in = np.log(self.y_in)
        self.logy_ex = np.log(self.y_ex)
        
        self.bs = bs

    def OLS(self):
        X_in = self.X_in
        model = sm.OLS(self.logy_in, X_in, intercept=False)
        self.reg = model.fit()
        print(self.reg.summary())
        
    def directDL(self, ite=100, bs=200, add=False):
        logy_in = np.array(self.logy_in, dtype='float32')
        X_in = np.array(self.X_in, dtype='float32')

        y = Variable(logy_in)
        x = Variable(X_in)

        num, col_num = X_in.shape
        
        if add is False:
            self.model1 = CAR(15, 15, 5, col_num)
        
        optimizer = optimizers.SGD()
        optimizer.setup(self.model1)

        for j in range(ite):
            sffindx = np.random.permutation(num)
            for i in range(0, num, bs):
                x = Variable(X_in[sffindx[i:(i+bs) if (i+bs) < num else num]])
                y = Variable(logy_in[sffindx[i:(i+bs) if (i+bs) < num else num]])
                self.model1.zerograds()
                loss = self.model1(x, y)
                loss.backward()
                optimizer.update()
            if j % 1000 == 0:
                loss_val = loss.data
                print('epoch:', j)
                print('train mean loss={}'.format(loss_val))
                print(' - - - - - - - - - ')
                
        y_ex = np.array(self.y_ex, dtype='float32').reshape(len(self.y_ex))
        X_ex = np.array(self.X_ex, dtype='float32')
        X_ex = Variable(X_ex)

        logy_pred =  self.model1.fwd(X_ex, X_ex).data
        y_pred = np.exp(logy_pred)
        error = y_ex - y_pred.reshape(len(y_pred),)
        plt.hist(error[:])
        
    def DL(self, ite=100, bs=200, add=False):
        y_in = np.array(self.y_in, dtype='float32').reshape(len(self.y_in))
                                                            
        resid = y_in - np.exp(self.reg.predict())
        resid = np.array(resid, dtype='float32').reshape(len(resid),1)
        
        X_in = np.array(self.X_in, dtype='float32')

        y = Variable(resid)
        x = Variable(X_in)

        num, col_num = X_in.shape
        
        if add is False:
            self.model1 = CAR(10, 10, 3, col_num)
            
        optimizer = optimizers.Adam()
        optimizer.setup(self.model1)

        for j in range(ite):
            sffindx = np.random.permutation(num)
            for i in range(0, num, bs):
                x = Variable(X_in[sffindx[i:(i+bs) if (i+bs) < num else num]])
                y = Variable(resid[sffindx[i:(i+bs) if (i+bs) < num else num]])
                self.model1.zerograds()
                loss = self.model1(x, y)
                loss.backward()
                optimizer.update()
            if j % 1000 == 0:
                loss_val = loss.data
                print('epoch:', j)
                print('train mean loss={}'.format(loss_val))
                print(' - - - - - - - - - ')
                
        resid_pred =  self.model1.fwd(X_in, X_in).data  
        y_in = np.array(self.y_in, dtype='float32').reshape(len(self.y_in))
        
        logy_pred = np.matrix(self.X_in)*np.matrix(self.reg.params).T
        
        self.pred = np.exp(logy_pred) + resid_pred
        self.resid2 = np.array(y_in - self.pred.reshape(len(self.pred),))[0]
            
    def predict(self):
        y_ex = np.array(self.y_ex, dtype='float32').reshape(len(self.y_ex))
        
        X_ex = np.array(self.X_ex, dtype='float32')
        X_ex = Variable(X_ex)
        resid_pred =  self.model1.fwd(X_ex, X_ex).data  
        print(resid_pred[:10])
        
        self.logy_pred = np.matrix(self.X_ex)*np.matrix(self.reg.params).T
        self.error1 = np.array(y_ex - np.exp(self.logy_pred.reshape(len(self.logy_pred),)))[0]
        
        self.pred = np.exp(self.logy_pred) + resid_pred
        self.error2 = np.array(y_ex - self.pred.reshape(len(self.pred),))[0]
        
    def compare(self):
        plt.hist(self.error1)
        plt.hist(self.error2)
        
    def sar(self):
        weight = ((np.array([self.ido_in]).T - np.array([self.ido_in]))**2 + 
        (np.array([self.keido_in]).T - np.array([self.keido_in]))**2)

        for i in range(len(weight)):
            N = sum(weight[i])
            weight[i] = weight[i]/N

        Ws = np.matrix(weight)*np.matrix(self.resid2).T

        model2 = sm.OLS(np.matrix(self.resid2).T, Ws)
        reg = model2.fit()
        rho = reg.params
        
        self.sigmas = []

        for i in range(len(self.ido_ex)):
            x = np.append(np.array(self.ido_in), np.array(self.ido_ex)[i])
            y = np.append(np.array(self.keido_in), np.array(self.keido_ex)[i])
            weight = ((np.array([x]).T - np.array([x]))**2 + 
                (np.array([y]).T - np.array([y]))**2)

            N = np.array([list(np.matrix(weight)*np.matrix(np.ones(len(weight), 1)))[0] for k in range(len(weight))])
            weight[j] = weight/N

            res = np.append(model.resid2, 0)
            Ws = np.matrix(weight)*np.matrix(res).T
            sigma = rho*Ws.T
            sigma = np.array(sigma)[0][-1]
            self.sigmas.append(sigma)

In [274]:
vars = ['P', 'S', 'L', 'R', 'RW', 'A', 'TS', 'TT', 'WOOD', 'SOUTH', 'CMD', 'IDD', 'FAR', 'X', 'Y']
#vars += vars + list(TDQ.columns)

In [275]:
model = OLS_DLmodel(datas, vars)

In [276]:
model.OLS()


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      P   R-squared:                       0.787
Model:                            OLS   Adj. R-squared:                  0.786
Method:                 Least Squares   F-statistic:                     1053.
Date:                Sun, 12 Feb 2017   Prob (F-statistic):               0.00
Time:                        21:09:02   Log-Likelihood:                 1439.6
No. Observations:                4000   AIC:                            -2849.
Df Residuals:                    3985   BIC:                            -2755.
Df Model:                          14                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept   -193.5733     27.780     -6.968      0.000      -248.038  -139.109
S              0.0032      0.000     29.399      0.000         0.003     0.003
L              0.0034   9.15e-05     36.829      0.000         0.003     0.004
R              0.0107      0.003      3.448      0.001         0.005     0.017
RW             0.0064      0.001      4.487      0.000         0.004     0.009
A             -0.0008   3.07e-05    -26.396      0.000        -0.001    -0.001
TS            -0.0093      0.001    -14.480      0.000        -0.011    -0.008
TT            -0.0029      0.001     -3.605      0.000        -0.005    -0.001
WOOD          -0.0183      0.007     -2.462      0.014        -0.033    -0.004
SOUTH          0.0237      0.007      3.463      0.001         0.010     0.037
CMD            0.0341      0.022      1.561      0.119        -0.009     0.077
IDD           -0.0539      0.039     -1.380      0.168        -0.131     0.023
FAR           -0.0047      0.000     -9.897      0.000        -0.006    -0.004
X             -1.1695      0.154     -7.606      0.000        -1.471    -0.868
Y              1.7461      0.179      9.781      0.000         1.396     2.096
==============================================================================
Omnibus:                      313.879   Durbin-Watson:                   1.383
Prob(Omnibus):                  0.000   Jarque-Bera (JB):             1123.789
Skew:                          -0.342   Prob(JB):                    9.38e-245
Kurtosis:                       5.505   Cond. No.                     2.45e+06
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 2.45e+06. This might indicate that there are
strong multicollinearity or other numerical problems.

In [277]:
model.DL(ite=10, bs=200)


(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
epoch: 0
train mean loss=1875433.25
 - - - - - - - - - 
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)
(200, 1)

In [211]:
model.predict()


[[-0.18480808]
 [-0.18097544]
 [-0.18558913]
 [-0.17947304]
 [-0.1813302 ]
 [-0.18139777]
 [-0.18157399]
 [-0.18489236]
 [-0.18399435]
 [-0.18388116]]

In [212]:
model.compare()



In [216]:
model.DL(30000, bs=200, add=True)


epoch: 0
train mean loss=2661427.25
 - - - - - - - - - 
epoch: 1000
train mean loss=4598955.0
 - - - - - - - - - 
epoch: 2000
train mean loss=4201277.0
 - - - - - - - - - 
epoch: 3000
train mean loss=2345711.75
 - - - - - - - - - 
epoch: 4000
train mean loss=17583000.0
 - - - - - - - - - 
epoch: 5000
train mean loss=2216756.75
 - - - - - - - - - 
epoch: 6000
train mean loss=4752346.0
 - - - - - - - - - 
epoch: 7000
train mean loss=6701641.5
 - - - - - - - - - 
epoch: 8000
train mean loss=2107108.25
 - - - - - - - - - 
epoch: 9000
train mean loss=1898800.0
 - - - - - - - - - 
epoch: 10000
train mean loss=4624477.0
 - - - - - - - - - 
epoch: 11000
train mean loss=1503958.875
 - - - - - - - - - 
epoch: 12000
train mean loss=1566986.25
 - - - - - - - - - 
epoch: 13000
train mean loss=3244070.0
 - - - - - - - - - 
epoch: 14000
train mean loss=2000256.0
 - - - - - - - - - 
epoch: 15000
train mean loss=4460300.0
 - - - - - - - - - 
epoch: 16000
train mean loss=2108647.0
 - - - - - - - - - 
epoch: 17000
train mean loss=1648146.875
 - - - - - - - - - 
epoch: 18000
train mean loss=8736821.0
 - - - - - - - - - 
epoch: 19000
train mean loss=1581624.0
 - - - - - - - - - 
epoch: 20000
train mean loss=1752820.5
 - - - - - - - - - 
epoch: 21000
train mean loss=1193056.0
 - - - - - - - - - 
epoch: 22000
train mean loss=4023660.75
 - - - - - - - - - 
epoch: 23000
train mean loss=2575228.25
 - - - - - - - - - 
epoch: 24000
train mean loss=3721236.25
 - - - - - - - - - 
epoch: 25000
train mean loss=7738276.5
 - - - - - - - - - 
epoch: 26000
train mean loss=1352367.0
 - - - - - - - - - 
epoch: 27000
train mean loss=2701874.25
 - - - - - - - - - 
epoch: 28000
train mean loss=7112670.5
 - - - - - - - - - 
epoch: 29000
train mean loss=4123350.75
 - - - - - - - - - 

In [218]:
serializers.save_npz('model1(1)', model.model1)

In [226]:
model.DL(10000, bs=200, add=True)


epoch: 0
train mean loss=1518404.375
 - - - - - - - - - 
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-226-d4a3501a80a2> in <module>()
----> 1 model.DL(10000, bs=200, add=True)

<ipython-input-182-6accaa5d25da> in DL(self, ite, bs, add)
     88                 y = Variable(resid[sffindx[i:(i+bs) if (i+bs) < num else num]])
     89                 self.model1.zerograds()
---> 90                 loss = self.model1(x, y)
     91                 loss.backward()
     92                 optimizer.update()

<ipython-input-35-b76bb8632c54> in __call__(self, x, y)
     14 
     15     def __call__(self, x, y):
---> 16         fv = self.fwd(x, y)
     17         loss = F.mean_squared_error(fv, y)
     18         return loss

<ipython-input-35-b76bb8632c54> in fwd(self, x, y)
     24         h4 = F.sigmoid(self.l4(h3))
     25         h5 = F.sigmoid(self.l5(h4))
---> 26         h6 = self.l6(h5)
     27         return h6

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/links/connection/linear.py in __call__(self, x)
     90             with cuda.get_device(self._device_id):
     91                 self._initialize_params(x.size // len(x.data))
---> 92         return linear.linear(x, self.W, self.b)

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/functions/connection/linear.py in linear(x, W, b)
     77         return LinearFunction()(x, W)
     78     else:
---> 79         return LinearFunction()(x, W, b)

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/function.py in __call__(self, *inputs)
    188 
    189         if self.type_check_enable:
--> 190             self._check_data_type_forward(in_data)
    191 
    192         hooks = collections.OrderedDict(chainer.get_function_hooks())

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/function.py in _check_data_type_forward(self, in_data)
    264         in_type = type_check.get_types(in_data, 'in_types', False)
    265         try:
--> 266             self.check_type_forward(in_type)
    267         except type_check.InvalidType as e:
    268             msg = """

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/functions/connection/linear.py in check_type_forward(self, in_types)
     13     def check_type_forward(self, in_types):
     14         n_in = in_types.size()
---> 15         type_check.expect(2 <= n_in, n_in <= 3)
     16         x_type, w_type = in_types[:2]
     17 

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/utils/type_check.py in expect(*bool_exprs)
    465     for expr in bool_exprs:
    466         assert isinstance(expr, Testable)
--> 467         expr.expect()
    468 
    469 prod = Variable(numpy.prod, 'prod')

/Users/NIGG/anaconda/lib/python3.5/site-packages/chainer/utils/type_check.py in expect(self)
    431         right = self._eval_right()
    432 
--> 433         if not self.func(left, right):
    434             raise InvalidType(
    435                 '{0} {1} {2}'.format(self.lhs, self.exp, self.rhs),

KeyboardInterrupt: 

In [228]:
serializers.save_npz('model1(2)', model.model1)

In [229]:
model.predict()


[[-266.32055664]
 [ -92.56298065]
 [ 232.43139648]
 [ 654.1295166 ]
 [ 452.95437622]
 [  24.40013885]
 [ 654.13354492]
 [-550.11517334]
 [ 654.14874268]
 [-922.35955811]]

青がOLSの誤差、緑がOLSと深層学習を組み合わせた誤差。


In [230]:
model.compare()



In [231]:
fig = plt.figure()
ax = fig.add_subplot(111)

errors = [model.error1, model.error2]

bp = ax.boxplot(errors)

plt.grid()
plt.ylim([-5000,5000])

plt.title('分布の箱ひげ図')

plt.show()



In [232]:
print(np.mean(model.error1))
print(np.mean(model.error2))


215.303997813
152.639932368

In [233]:
print(np.var(model.error1))
print(np.var(model.error2))


3442290.59518
2982075.27679

In [245]:
model.sar()


Out[245]:
741

In [263]:
error2 = (model.error2[:741] - np.array(model.sigmas))

In [264]:
fig = plt.figure()
ax = fig.add_subplot(111)

errors = [model.error1, error2]

bp = ax.boxplot(errors)

plt.grid()
plt.ylim([-5000,5000])

plt.title('分布の箱ひげ図')

plt.show()



In [265]:
print(np.mean(model.error1))
print(np.mean(error2))


215.303997813
200.301218709

In [266]:
print(np.mean(np.abs(model.error1)))
print(np.mean(np.abs(error2)))


1129.89620045
959.270534699

In [267]:
print(max(np.abs(model.error1)))
print(max(np.abs(error2)))


19132.3280446
14074.8183331

In [268]:
print(np.var(model.error1))
print(np.var(error2))


3442290.59518
2034630.06298

In [269]:
X = model.X_ex['X'].values
Y = model.X_ex['Y'].values

In [270]:
import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D

fig=plt.figure()
ax=Axes3D(fig)
 
ax.scatter3D(X[:741], Y[:741], error2)
plt.show()



In [271]:
import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D

fig=plt.figure()
ax=Axes3D(fig)
 
ax.scatter3D(X, Y, model.error2)
plt.show()



In [265]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri



#============
# First plot
#============
# Plot the surface.  The triangles in parameter space determine which x, y, z
# points are connected by an edge.
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_trisurf(X, Y, e)
ax.set_zlim(-1, 1)
plt.show()

In [ ]: