{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport gc\nimport pandas as pd\nimport numpy as np\nimport datetime\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm  \nfrom sklearn.model_selection import train_test_split,GridSearchCV\nfrom sklearn.preprocessing import StandardScaler,LabelEncoder\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.model_selection import KFold,StratifiedKFold\nfrom sklearn.experimental import enable_iterative_imputer\nfrom sklearn.impute import IterativeImputer\n\nimport warnings\ndef ignore_warn(*args, **kwargs):\n    pass\nwarnings.warn = ignore_warn","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Weather data processing function"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def fill_weather_dataset(weather_df, mode_type):\n    \n    weather_df.loc[:,'timestamp'] = weather_df['timestamp'].astype(str)\n    # Find Missing Dates\n    time_format = \"%Y-%m-%d %H:%M:%S\"\n    start_date = datetime.datetime.strptime(weather_df['timestamp'].min(),time_format)\n    end_date = datetime.datetime.strptime(weather_df['timestamp'].max(),time_format)\n    total_hours = int(((end_date - start_date).total_seconds() + 3600) / 3600)\n    hours_list = [(end_date - datetime.timedelta(hours=x)).strftime(time_format) for x in range(total_hours)]\n\n    for site_id in range(16):\n        site_hours = np.array(weather_df[weather_df['site_id'] == site_id]['timestamp'])\n        new_rows = pd.DataFrame(np.setdiff1d(hours_list,site_hours),columns=['timestamp'])\n        new_rows['site_id'] = site_id\n        weather_df = pd.concat([weather_df,new_rows])\n\n        weather_df = weather_df.reset_index(drop=True) \n\n         \n\n    # Add new Features\n    weather_df[\"datetime\"] = pd.to_datetime(weather_df[\"timestamp\"])\n    weather_df[\"day\"] = weather_df[\"datetime\"].dt.day\n    weather_df[\"week\"] = weather_df[\"datetime\"].dt.week\n    weather_df[\"month\"] = weather_df[\"datetime\"].dt.month\n    weather_df[\"hour\"] = weather_df[\"datetime\"].dt.hour\n    weather_df[\"weekday\"] = weather_df[\"datetime\"].dt.weekday\n#    \n    #Use IterativeImputer to fill missing value \n#    df_weather_timestamp = weather_df.timestamp\n#    weather_df = weather_df.drop(['timestamp','datetime'],axis=1)\n#    imp = IterativeImputer(max_iter=20, random_state=0)\n#    df_weather_train_np = imp.fit_transform(weather_df)\n#    weather_df = pd.DataFrame(df_weather_train_np, columns=weather_df.columns)\n#    weather_df.loc[:,'timestamp'] = df_weather_timestamp\n    \n    # Reset Index for Fast Update\n    weather_df = weather_df.set_index(['site_id','day','month'])\n    air_temperature_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['air_temperature'].mean(),columns=[\"air_temperature\"])\n    air_temperature_filler = air_temperature_filler.fillna(method='ffill')\n    weather_df.update(air_temperature_filler,overwrite=False)\n\n    # Step 1\n    cloud_coverage_filler = weather_df.groupby(['site_id','day','month'])['cloud_coverage'].mean()\n    # Step 2\n    cloud_coverage_filler = pd.DataFrame(cloud_coverage_filler.fillna(method='ffill'),columns=[\"cloud_coverage\"])\n\n    weather_df.update(cloud_coverage_filler,overwrite=False)\n\n    due_temperature_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['dew_temperature'].mean(),columns=[\"dew_temperature\"])\n    due_temperature_filler = pd.DataFrame(due_temperature_filler.fillna(method='ffill'),columns=[\"dew_temperature\"])\n    weather_df.update(due_temperature_filler,overwrite=False)\n\n    # Step 1\n    sea_level_filler = weather_df.groupby(['site_id','day','month'])['sea_level_pressure'].mean()\n    # Step 2\n    sea_level_filler = pd.DataFrame(sea_level_filler.fillna(method='ffill'),columns=['sea_level_pressure'])\n\n    weather_df.update(sea_level_filler,overwrite=False)\n\n    wind_direction_filler =  pd.DataFrame(weather_df.groupby(['site_id','day','month'])['wind_direction'].mean(),columns=['wind_direction'])\n    wind_direction_filler =  pd.DataFrame(wind_direction_filler.fillna(method='ffill'),columns=[\"wind_direction\"])\n    weather_df.update(wind_direction_filler,overwrite=False)\n\n    wind_speed_filler =  pd.DataFrame(weather_df.groupby(['site_id','day','month'])['wind_speed'].mean(),columns=['wind_speed'])\n    wind_speed_filler =  pd.DataFrame(wind_speed_filler.fillna(method='ffill'),columns=[\"wind_speed\"])\n    weather_df.update(wind_speed_filler,overwrite=False)\n     \n\n    # Step 1\n    precip_depth_filler = weather_df.groupby(['site_id','day','month'])['precip_depth_1_hr'].mean()\n    # Step 2\n    precip_depth_filler = pd.DataFrame(precip_depth_filler.fillna(method='ffill'),columns=['precip_depth_1_hr'])\n    weather_df.update(precip_depth_filler,overwrite=False)\n    if mode_type == 'Dnn':\n        holidays = [\"2016-01-01\", \"2016-01-18\", \"2016-02-15\", \"2016-05-30\", \"2016-07-04\",\n                    \"2016-09-05\", \"2016-10-10\", \"2016-11-11\", \"2016-11-24\", \"2016-12-26\",\n                    \"2017-01-01\", \"2017-01-16\", \"2017-02-20\", \"2017-05-29\", \"2017-07-04\",\n                    \"2017-09-04\", \"2017-10-09\", \"2017-11-10\", \"2017-11-23\", \"2017-12-25\",\n                    \"2018-01-01\", \"2018-01-15\", \"2018-02-19\", \"2018-05-28\", \"2018-07-04\",\n                    \"2018-09-03\", \"2018-10-08\", \"2018-11-12\", \"2018-11-22\", \"2018-12-25\",\n                    \"2019-01-01\"] \n        weather_df[\"is_holiday\"] = (weather_df.datetime.dt.date.astype(\"str\").isin(holidays)).astype(int)\n        \n        beaufort = [(0, 0, 0.3), (1, 0.3, 1.6), (2, 1.6, 3.4), (3, 3.4, 5.5), (4, 5.5, 8), (5, 8, 10.8), (6, 10.8, 13.9), \n          (7, 13.9, 17.2), (8, 17.2, 20.8), (9, 20.8, 24.5), (10, 24.5, 28.5), (11, 28.5, 33), (12, 33, 200)]\n        for item in beaufort:\n            weather_df.loc[(weather_df['wind_speed']>=item[1]) & (weather_df['wind_speed']<item[2]), 'beaufort_scale'] = item[0]\n\n\n \n    weather_df = weather_df.drop(['offset','datetime'],axis=1) \n    weather_df = weather_df.reset_index()     \n\n\n    return weather_df\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Reduce memory function"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Original code from https://www.kaggle.com/gemartin/load-data-reduce-memory-usage by @gemartin\n\nfrom pandas.api.types import is_datetime64_any_dtype as is_datetime\nfrom pandas.api.types import is_categorical_dtype\n\ndef reduce_mem_usage(df, use_float16=False):\n    \"\"\"\n    Iterate through all the columns of a dataframe and modify the data type to reduce memory usage.        \n    \"\"\"\n    \n    start_mem = df.memory_usage().sum() / 1024**2\n    print(\"Memory usage of dataframe is {:.2f} MB\".format(start_mem))\n    \n    for col in df.columns:\n        if is_datetime(df[col]) or is_categorical_dtype(df[col]):\n            continue\n        col_type = df[col].dtype\n        \n        if col_type != object:\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 use_float16 and 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        else:\n            df[col] = df[col].astype(\"category\")\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print(\"Memory usage after optimization is: {:.2f} MB\".format(end_mem))\n    print(\"Decreased by {:.1f}%\".format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Building data processing function"},{"metadata":{"trusted":true},"cell_type":"code","source":"def data_building_processing(df_data):\n    '''===========Building data processing======================'''\n    print('Processing building data...')\n\n    lbl = LabelEncoder() \n    lbl.fit(list(df_data['primary_use'].values)) \n    df_data['primary_use'] = lbl.transform(list(df_data['primary_use'].values))\n    imp = IterativeImputer(max_iter=30, random_state=0)\n    df_build = imp.fit_transform(df_data)\n    df_data = pd.DataFrame(df_build, columns=df_data.columns)\n    df_data.loc[:,'floor_count'] = df_data['floor_count'].apply(int)\n    df_data.loc[:,'year_built'] = df_data['year_built'].apply(int)  \n\n#    df_data['year_built_1920'] = df_data['year_built'].apply(lambda x: 1 if x<1920 else 0 )\n#    df_data['year_built_1920_1950'] = df_data['year_built'].apply(lambda x: 1 if 1920<=x & x<1950 else 0 )\n#    df_data['year_built_1950_1970'] = df_data['year_built'].apply(lambda x: 1 if 1950<=x & x<1970 else 0 )\n#    df_data['year_built_1970_2000'] = df_data['year_built'].apply(lambda x: 1 if 1970<=x & x<2000 else 0 )\n#    df_data['year_built_2000'] = df_data['year_built'].apply(lambda x: 1 if x>=2000 else 0 )\n    return df_data","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Collect data for feature engineering"},{"metadata":{"trusted":true},"cell_type":"code","source":"def features_engineering(df, mode_type):\n    \n\n    classify_columns = ['building_id','meter','site_id','primary_use',\n                        'hour','weekday'] \n    # Sort by timestamp\n    df.sort_values(\"timestamp\")\n    df.reset_index(drop=True)\n    \n    # Add more features\n    df['square_feet'] =  np.log1p(df['square_feet'])\n    \n    drop = [\"timestamp\",\"sea_level_pressure\", \"wind_direction\", \"wind_speed\",]\n    df = df.drop(drop, axis=1)\n    gc.collect()\n    \n    # Encode Categorical Data\n    for i in tqdm(classify_columns):\n        le = LabelEncoder()\n        df[i] = le.fit_transform(df[i])\n        \n    if mode_type == 'Dnn':\n        numericals = [\"square_feet\", \"year_built\", \"air_temperature\", \"cloud_coverage\",\n              \"dew_temperature\", \"precip_depth_1_hr\", \"floor_count\", 'beaufort_scale']\n        print('Start working with numerical characteristics...')\n        for i in tqdm(numericals):\n            ss_X=StandardScaler() \n            df.loc[:,i] = ss_X.fit_transform(df[i].values.reshape(-1, 1))    \n    \n    return df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Main Processingfunction"},{"metadata":{"trusted":true},"cell_type":"code","source":"def data_pro(df_data, df_weather_train, df_weather_test, df_building, data_type='train',mode_type='lgb'):\n    ## REducing memory\n    df_data = reduce_mem_usage(df_data,use_float16=True)\n    df_building = reduce_mem_usage(df_building,use_float16=True)\n    '''===Align local timestamps===='''\n    weather = pd.concat([df_weather_train,df_weather_test],ignore_index=True)\n    weather_key = ['site_id', 'timestamp']\n    temp_skeleton = weather[weather_key + ['air_temperature']].drop_duplicates(subset=weather_key).sort_values(by=weather_key).copy()\n    # calculate ranks of hourly temperatures within date/site_id chunks\n    temp_skeleton['temp_rank'] = temp_skeleton.groupby(['site_id', temp_skeleton.timestamp.dt.date])['air_temperature'].rank('average')\n    # create a dataframe of site_ids (0-16) x mean hour rank of temperature within day (0-23)\n    df_2d = temp_skeleton.groupby(['site_id', temp_skeleton.timestamp.dt.hour])['temp_rank'].mean().unstack(level=1)\n    # Subtract the columnID of temperature peak by 14, getting the timestamp alignment gap.\n    site_ids_offsets = pd.Series(df_2d.values.argmax(axis=1) - 14)\n    site_ids_offsets.index.name = 'site_id'\n    \n    def timestamp_align(df):\n        df['offset'] = df.site_id.map(site_ids_offsets)\n        df['timestamp_aligned'] = (df.timestamp - pd.to_timedelta(df.offset, unit='H'))\n        df['timestamp'] = df['timestamp_aligned']\n        del df['timestamp_aligned']\n        return df\n \n    if data_type == 'test':\n        print(\"Test data detected...\")\n        df_weather_test = timestamp_align(df_weather_test)\n        df_weather_test = fill_weather_dataset(df_weather_test, mode_type)\n        df_weather_test = reduce_mem_usage(df_weather_test,use_float16=True)\n        #merge\n        df_building = data_building_processing(df_building)\n        df_data = pd.merge(df_data, df_building, on='building_id', how='left')\n        df_data = df_data.merge(df_weather_test,how='left',left_on=['site_id','timestamp'],right_on=['site_id','timestamp'])\n    \n        print(\"Start feature processing...\")    \n        df_data = features_engineering(df_data, mode_type)\n        gc.collect()\n        \n        return df_data\n    \n    elif data_type == 'train':\n        print(\"Train data detected...\")\n        df_weather_train = timestamp_align(df_weather_train)\n        df_weather_train = fill_weather_dataset(df_weather_train, mode_type)\n        df_weather_train = reduce_mem_usage(df_weather_train,use_float16=True)\n        #merge\n        \n        df_building = data_building_processing(df_building)\n        df_data = df_data.merge(df_building, left_on='building_id',right_on='building_id',how='left')\n        df_data = df_data.merge(df_weather_train,how='left',left_on=['site_id','timestamp'],right_on=['site_id','timestamp'])\n        target_data = df_data['meter_reading']\n        df_data = df_data.drop('meter_reading',axis=1)\n        print(\"Start feature processing...\")\n        \n        df_data = features_engineering(df_data, mode_type)\n        gc.collect()\n\n        return df_data, target_data","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Preprocessing training data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/ashrae-energy-prediction/train.csv')\ntrain_df = train_df[train_df['building_id'] != 1099 ]\ntrain_df = train_df.query('not (building_id <= 104 & meter == 0 & timestamp <= \"2016-05-20\")')\nbuilding_df = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')\nweather_df = pd.read_csv(\"../input/ashrae-energy-prediction/weather_train.csv\",parse_dates=[\"timestamp\"],)\ndf_weather_test = pd.read_csv(\"../input/ashrae-energy-prediction/weather_test.csv\",parse_dates=[\"timestamp\"],)\n\ntrain_data,target_data = data_pro(train_df, weather_df, df_weather_test, building_df, data_type='train')\ntarget_data = np.log1p(target_data)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Preprocessing test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.read_csv('../input/ashrae-energy-prediction/test.csv')\nrow_ids = df_test[\"row_id\"]\ndf_test.drop(\"row_id\", axis=1, inplace=True)\nbuilding_df = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')\nweather_df = pd.read_csv(\"../input/ashrae-energy-prediction/weather_train.csv\",parse_dates=[\"timestamp\"],) \ndf_weather_test = pd.read_csv(\"../input/ashrae-energy-prediction/weather_test.csv\",parse_dates=[\"timestamp\"],)\ntest_data = data_pro(df_test, weather_df, df_weather_test, building_df, data_type='test')\ntest_data['row_id'] = row_ids\n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Model 1:LGBM  **\n\nSome parameters of the model refer to this https://www.kaggle.com/aitude/ashrae-kfold-lightgbm-without-leak-1-08 "},{"metadata":{"trusted":true},"cell_type":"code","source":"import lightgbm as lgb\nparams = {\n    \"objective\": \"regression\",\n    \"boosting\": \"gbdt\",\n   'num_threads':16,\n    'num_leaves': 1280,\n#    'max_depth':10,\n#    'min_data_in_leaf':20,\n    \"feature_fraction\": 0.85,\n#    'bagging_fraction':0.85,\n    \"reg_lambda\": 2,\n    \"metric\": \"rmse\",\n#    'max_bin':255,\n    'learning_rates':0.05\n}\n\ncategorical_features = [\"site_id\", \"building_id\", \"primary_use\", \"hour\", \"weekday\",  \"meter\",'day','month','week',]\n#                        'year_built_1920','year_built_1920_1950','year_built_1950_1970','year_built_1970_2000','year_built_2000'] \n\nkf = KFold(n_splits=3)\nlgb_models = []\nevals_results = []\nfor train_index,test_index in kf.split(train_data):\n    train_features = train_data.loc[train_index]\n    train_target = target_data.loc[train_index]\n    \n    test_features = train_data.loc[test_index]\n    test_target = target_data.loc[test_index]\n    \n    d_training = lgb.Dataset(train_features, label=train_target,categorical_feature=categorical_features, free_raw_data=False)\n    d_test = lgb.Dataset(test_features, label=test_target,categorical_feature=categorical_features, free_raw_data=False)\n    \n    evals_result = {}\n    model = lgb.train(params, train_set=d_training,\n                      num_boost_round=1000, \n                      valid_sets=[d_training,d_test], \n                      evals_result=evals_result,\n                      verbose_eval=25, \n                      early_stopping_rounds=50)\n    lgb_models.append(model)\n    evals_results.append(evals_result)\n    del train_features, train_target, test_features, test_target, d_training, d_test\n    gc.collect()\n    \n    \nprint('plot...')            \nfor i in range(len(evals_results)):\n    plt.figure()\n    plt.subplots_adjust( wspace = 1, hspace = 0.01)\n    ax = plt.subplot(1,2,1)\n    lgb.plot_metric(evals_results[i], ax=ax)\n    ax.set_title(\"model{}'s rmsle \".format(i))\n    \n    ax = plt.subplot(1,2,2)\n    lgb.plot_importance(lgb_models[i], max_num_features=27, ax=ax)\n    ax.set_title(\"model{}'s feature importance \".format(i))\n    plt.show() \n    gc.collect()\n\ndel(train_df, weather_df, df_weather_test, building_df) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"LGBM Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#target_tests = []    \n#for model in lgb_models:\n#    if  target_tests == []:\n#        target_tests = np.expm1(model.predict(test_data, num_iteration=model.best_iteration)) / len(lgb_models)\n#    else:\n#        target_tests += np.expm1(model.predict(test_data, num_iteration=model.best_iteration)) / len(lgb_models)\n#    del model\n#    gc.collect()\n\n#results_df = pd.DataFrame({\"row_id\": row_ids, \"meter_reading\": np.clip(target_tests, 0, a_max=None)})\n#del row_ids,target_tests,df_test,test_data\n#gc.collect()\n#results_df.to_csv(\"submission.csv\", index=False)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Model 2:Keras Dnn**\n\nYou can see this solution in here https://www.kaggle.com/nerobugger/ashrae-eda-lgb-and-dnn-no-leak-part-2/"}],"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}