{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport sys\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.linear_model import Lasso\nfrom sklearn.linear_model import Ridge\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn import tree\n\nfrom sklearn.neural_network import MLPClassifier\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sys.setrecursionlimit(10**3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reduce memory\n# Original code from https://www.kaggle.com/gemartin/load-data-reduce-memory-usage by @gemartin\ndef reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2    \n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)    \n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/train.csv\")\ntrain = reduce_mem_usage(train)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(\"../input/ashrae-energy-prediction/test.csv\")\ntest = reduce_mem_usage(test)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_train.csv\")\nweather_train = reduce_mem_usage(weather_train)\nweather_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train['site_id']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_test = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_test.csv\")\nweather_test = reduce_mem_usage(weather_test)\nweather_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_metadata = pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/building_metadata.csv\")\nbuilding_metadata = reduce_mem_usage(building_metadata)\nbuilding_metadata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_metadata.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check missing data for train\nnp.sum(train.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check missing value for test\nnp.sum(test.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check missing data for weather_train\nnp.sum(weather_train.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check missing data for weather_train\nnp.sum(weather_test.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check missing value for building_metadata\nnp.sum(building_metadata.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# filling missing values for the weather_train\nweather_train['air_temperature'].fillna(weather_train['air_temperature'].mean, inplace = True)\nweather_train['cloud_coverage'].fillna(weather_train['cloud_coverage'].mean, inplace = True)\nweather_train['dew_temperature'].fillna(weather_train['dew_temperature'].mean, inplace = True)\nweather_train['precip_depth_1_hr'].fillna(weather_train['precip_depth_1_hr'].mean, inplace = True)\nweather_train['precip_depth_1_hr'].fillna(weather_train['precip_depth_1_hr'].mean, inplace = True)\nweather_train['sea_level_pressure'].fillna(weather_train['sea_level_pressure'].mean, inplace = True)\nweather_train['wind_direction'].fillna(weather_train['wind_direction'].mean, inplace = True)\nweather_train['wind_speed'].fillna(weather_train['wind_speed'].mean, inplace = True)\nnp.sum(weather_train.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# filling missing values for the weather_test\nweather_test['air_temperature'].fillna(weather_test['air_temperature'].mean, inplace = True)\nweather_test['cloud_coverage'].fillna(weather_test['cloud_coverage'].mean, inplace = True)\nweather_test['dew_temperature'].fillna(weather_test['dew_temperature'].mean, inplace = True)\nweather_test['precip_depth_1_hr'].fillna(weather_test['precip_depth_1_hr'].mean, inplace = True)\nweather_test['precip_depth_1_hr'].fillna(weather_test['precip_depth_1_hr'].mean, inplace = True)\nweather_test['sea_level_pressure'].fillna(weather_test['sea_level_pressure'].mean, inplace = True)\nweather_test['wind_direction'].fillna(weather_test['wind_direction'].mean, inplace = True)\nweather_test['wind_speed'].fillna(weather_test['wind_speed'].mean, inplace = True)\nnp.sum(weather_test.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# filling the missing values of building_metadata\nbuilding_metadata['year_built'].fillna(building_metadata['year_built'].mean, inplace = True)\nbuilding_metadata['floor_count'].fillna(building_metadata['floor_count'].mean, inplace = True)\nnp.sum(building_metadata.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.concat([train,weather_train[['air_temperature']], building_metadata[['primary_use', 'year_built']]], axis=1, sort = False)\nnp.sum(train.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lb = LabelEncoder()\ntrain['primary_use'] = lb.fit_transform(train['primary_use'].astype(str))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['air_temperature'].fillna(train['air_temperature'].mean, inplace = True)\ntrain['primary_use'].fillna(train['primary_use'].mean, inplace = True)\ntrain['year_built'].fillna(train['year_built'].mean, inplace = True)\nnp.sum(train.isnull())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"target = train['meter_reading']\nfeature = train[['building_id', 'meter', 'primary_use']]\ntarget = lb.fit_transform(target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#target = reduce_mem_usage(target)\nfeature = reduce_mem_usage(feature)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"clf = tree.DecisionTreeClassifier()\nclf.fit(feature, target)\npredict = clf.predict(feature)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}