kaggleのASHRAE -Start Here: A GENTLE Introductionをやってみる
リンク
データーはtrain.csv
とtest.csv
、sample_submission.csv
、building_metadata.csv
、weather_test.csv
、weather_train.csv
になる。
インポート
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import gc
# matplotlib and seaborn for plotting
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import matplotlib.patches as patches #matolotlibで円や長方形などの図形を描くライブラリ https://note.nkmk.me/python-matplotlib-patches-circle-rectangle/
from plotly import tools, subplots # plotlyは対話的なグラフが描画できる。
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.express as px
pd.set_option('max_columns', 150) #pandasのオプションを設定。 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.describe_option.html
py.init_notebook_mode(connected=True) #connectedをTrueにすると、PlotlyのJavascriptをインターネットから取得するようになります。
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
import os,random, math, psutil, pickle #psutil CPUのコア数、ディスクの空き容量、メモリの使用量等、ハードウェアの情報を取得する。
print(os.listdir("input"))
%%time
root = './input/'
train_df = pd.read_csv(root + 'train.csv')
train_df["timestamp"] = pd.to_datetime(train_df["timestamp"], format='%Y-%m-%d %H:%M:%S')
weather_train_df = pd.read_csv(root + 'weather_train.csv')
test_df = pd.read_csv(root + 'test.csv')
weather_test_df = pd.read_csv(root + 'weather_test.csv')
building_meta_df = pd.read_csv(root + 'building_metadata.csv')
sample_submission = pd.read_csv(root + 'sample_submission.csv')
データの大きさ
train_dfの概要
building_id:建物に付けられたID
meter:メータの種類
timestamp:タイムスタンプ
meter_reading:メータ読み値
train_df.head()
train_df.tail()
weather_train_dfの概要
site_id:サイトID
timestamp:タイムスタンプ
air_temperature:気温
cloud_coverage:雲
dew_temperature:露点温度
precip_depth_1_hr:1時間あたりの降雨量
sea_level_pressure:気圧
wind_direction:風向
wind_speed::風速
weather_train_df.head()
weather_test_dfの概要
weather_test_df.head()
building_meta_dfの概要
欠損値を調べる
train_dfのtimestamp
をindexにする
グラフのsubplotを1行4列作る
メータの種類ごとにデータを抜き取ってindexをリセットする
to_timedelta
メソッドで時間の差を算出しtotal_seconds()で秒にしている
そしてそれを3600で割っているので1時間値にしてint
に変換している。
ここでto_timedelta
が返す時間の差はUNIXエポックとの差を出している。
最初の時間が2016-01-01なので403224.0になる。
df.timestamp -= df.timestamp.min()
はdf.timestamp列の中で一番小さい数値
を各行の値から差し引いている。つまり最初の数値403224を引いているので
0,1,2,3,4,5,....8783となる。8760と成らないのは2016はうるう年だから。
そのあと1449x8784のNumpyの配列をつくりnp.nanで初期化している
df.valuesを1行つづ取り出して2列目のメータ番号が目的のメータ番号でなければ
continueする。missmap[ビルのID番号、時間を整数に置き換えた]=meter_reading
がゼロなら0
そうでなければ1を入れる。これをsns.heatmap
に入れて描画させる
train_df
欠損値の集計
total = train_df.isnull().sum().sort_values(ascending = False)
percent = (train_df.isnull().sum()/train_df.isnull().count()*100).sort_values(ascending = False)
missing__train_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing__train_data.head(4)
weather_train_df
欠損値の集計
total = weather_train_df.isnull().sum().sort_values(ascending = False)
percent = (weather_train_df.isnull().sum()/weather_train_df.isnull().count()*100).sort_values(ascending = False)
missing_weather_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_weather_data.head(9)
weather_test_df
欠損値の集計
total = weather_test_df.isnull().sum().sort_values(ascending = False)
percent = (weather_test_df.isnull().sum()/weather_test_df.isnull().count()*100).sort_values(ascending = False)
missing_weather_test_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_weather_test_data.head(9)
building_meta_df
欠損値の集計
total = building_meta_df.isnull().sum().sort_values(ascending = False)
percent = (building_meta_df.isnull().sum()/building_meta_df.isnull().count()*100).sort_values(ascending = False)
missing_building_meta_df = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
missing_building_meta_df.head(6)
train_dfの各列間の相関係数を算出
correlations = train_df.corr()['meter_reading'].sort_values()
print('Most Positive Correlations:\n', correlations.tail(15))
print('\nMost Negative Correlations:\n', correlations.head(15))
weather_train_dfの気温をヒストグラムに描画する
plt.hist(weather_train_df['air_temperature'],bins=60,color='#f46d43')
weather_test_dfの気温をヒストグラムに描画する
plt.hist(weather_train_df['air_temperature'],bins=60,color='#66bd63')