{"cells":[{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"# install the latest LOFO\n!pip install lofo-importance==0.2.4","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\ntrain_df = pd.read_csv(\"../input/ashrae-energy-prediction/train.csv\")\ntrain_df.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Train and test are split by time"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# sample 1M from 20M to calculate the importance faster\n\ntrain_df = train_df.sample(10**6, random_state=0)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building_df = pd.read_csv(\"../input/ashrae-energy-prediction/building_metadata.csv\")\n\ntrain_df = train_df.merge(building_df, on=\"building_id\", how=\"left\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weather_df = pd.read_csv(\"../input/ashrae-energy-prediction/weather_train.csv\")\n\ntrain_df = train_df.merge(weather_df, on=[\"site_id\", \"timestamp\"], how=\"left\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"day_of_week\"] = pd.to_datetime(train_df[\"timestamp\"]).dt.weekday\ntrain_df[\"hour\"] = pd.to_datetime(train_df[\"timestamp\"]).dt.hour\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model from https://www.kaggle.com/corochann/ashrae-simple-lgbm-submission\n\nimport lightgbm as lgb\nfrom sklearn.preprocessing import LabelEncoder\n\nle_pu = LabelEncoder()\ntrain_df[\"primary_use_le\"] = le_pu.fit_transform(train_df[\"primary_use\"])\n\ncat_cols = ['building_id', 'meter', 'site_id', 'primary_use_le', \"day_of_week\"]\n\nparams = {'num_leaves': 31,\n          'objective': 'regression',\n          'learning_rate': 0.1,\n          \"boosting\": \"gbdt\",\n          \"bagging_freq\": 5,\n          \"bagging_fraction\": 0.1,\n          \"feature_fraction\": 0.9,\n          \"num_rounds\": 600\n          }\n\nmodel = lgb.LGBMRegressor(**params)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom lofo import LOFOImportance, Dataset, plot_importance\n\n# time based cv split\ntrain_df.sort_values(\"timestamp\", inplace=True)\ncv = KFold(n_splits=4, shuffle=False, random_state=0)\n\ntrain_df[\"target\"] = np.log1p(train_df[\"meter_reading\"])\nfeatures = ['square_feet', 'year_built', 'floor_count', 'air_temperature', 'cloud_coverage', 'dew_temperature',\n               'precip_depth_1_hr', 'sea_level_pressure', 'wind_direction', 'wind_speed', 'hour'] + cat_cols\n\ndataset = Dataset(df=train_df, target=\"target\", features=features)\n\nlofo_imp = LOFOImportance(dataset, model=model, cv=cv, scoring=\"neg_mean_squared_error\", fit_params={\"categorical_feature\": cat_cols})\n\nimportance_df = lofo_imp.get_importance()\n\nplot_importance(importance_df, figsize=(12, 12))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}