{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport numpy as np\nimport pandas as pd\nimport scipy\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport sklearn\nimport lightgbm as lgbm\nimport datetime\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import mean_squared_error\nfrom itertools import product","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T02:41:46.403409Z","iopub.execute_input":"2022-07-14T02:41:46.404339Z","iopub.status.idle":"2022-07-14T02:41:48.982747Z","shell.execute_reply.started":"2022-07-14T02:41:46.404187Z","shell.execute_reply":"2022-07-14T02:41:48.981615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dpath  = '../input/competitive-data-science-predict-future-sales/'","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:48.984593Z","iopub.execute_input":"2022-07-14T02:41:48.985489Z","iopub.status.idle":"2022-07-14T02:41:48.990740Z","shell.execute_reply.started":"2022-07-14T02:41:48.985440Z","shell.execute_reply":"2022-07-14T02:41:48.989628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(dpath + 'sales_train.csv')\ntest = pd.read_csv(dpath + 'test.csv')\n\nshops = pd.read_csv(dpath + 'shops.csv')\n\nitems = pd.read_csv(dpath + 'items.csv')\ncats = pd.read_csv(dpath + 'item_categories.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:48.992710Z","iopub.execute_input":"2022-07-14T02:41:48.993198Z","iopub.status.idle":"2022-07-14T02:41:52.084008Z","shell.execute_reply.started":"2022-07-14T02:41:48.993152Z","shell.execute_reply":"2022-07-14T02:41:52.083075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp2=train.groupby('date')['item_id'].count()\ndf=pd.DataFrame(temp2)\ndf.rename(columns={'item_id':'item_id_cnt'},inplace=True)\ndf.plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:52.086015Z","iopub.execute_input":"2022-07-14T02:41:52.086372Z","iopub.status.idle":"2022-07-14T02:41:52.592883Z","shell.execute_reply.started":"2022-07-14T02:41:52.086343Z","shell.execute_reply":"2022-07-14T02:41:52.591829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp2=train[train['item_id']==5037][train['shop_id']==5][['date','item_cnt_day']]\n\n#Convert datetime type\ndef str_to_datetime(s):\n    split=s.split('.')\n    d,m,y=int(split[0]),int(split[1]),int(split[2])\n    return datetime.datetime(year=y,month=m,day=d)\ntemp2['date']=temp2['date'].apply(str_to_datetime).sort_values()\ntemp2.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:52.594416Z","iopub.execute_input":"2022-07-14T02:41:52.594863Z","iopub.status.idle":"2022-07-14T02:41:52.636713Z","shell.execute_reply.started":"2022-07-14T02:41:52.594821Z","shell.execute_reply":"2022-07-14T02:41:52.635652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f,ax=plt.subplots(figsize=(8,6))\nsns.boxplot(x=train['item_price'])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:52.638246Z","iopub.execute_input":"2022-07-14T02:41:52.638577Z","iopub.status.idle":"2022-07-14T02:41:53.421181Z","shell.execute_reply.started":"2022-07-14T02:41:52.638549Z","shell.execute_reply":"2022-07-14T02:41:53.420081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[(train.item_price < 250000 )& (train.item_cnt_day < 1000)]\ntrain.loc[train['item_cnt_day'] < 1, \"item_cnt_day\"] = 0","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.422399Z","iopub.execute_input":"2022-07-14T02:41:53.422741Z","iopub.status.idle":"2022-07-14T02:41:53.589100Z","shell.execute_reply.started":"2022-07-14T02:41:53.422711Z","shell.execute_reply":"2022-07-14T02:41:53.588079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SHOPS DATA SET CLEANING","metadata":{"execution":{"iopub.status.busy":"2022-06-22T04:38:46.111265Z","iopub.execute_input":"2022-06-22T04:38:46.111682Z","iopub.status.idle":"2022-06-22T04:38:46.118447Z","shell.execute_reply.started":"2022-06-22T04:38:46.111647Z","shell.execute_reply":"2022-06-22T04:38:46.117184Z"}}},{"cell_type":"code","source":"shops['city']=shops['shop_name'].str.split(\" \").map(lambda x:x[0])\nshops['category']=shops['shop_name'].str.split(\" \").map(lambda x:x[1])\nshops.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.590339Z","iopub.execute_input":"2022-07-14T02:41:53.590674Z","iopub.status.idle":"2022-07-14T02:41:53.606375Z","shell.execute_reply.started":"2022-07-14T02:41:53.590642Z","shell.execute_reply":"2022-07-14T02:41:53.605366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Encoder only the main categories, which are most repeated. Put OTHERS for the ones that are not too much repeated.\ncategory=[]\nfor cat in shops['category'].unique():\n    if len(shops[shops['category']==cat])>=5:\n        category.append(cat)\nshops['category']=shops['category'].apply(lambda x: x if (x in category) else 'others')\nshops.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.607611Z","iopub.execute_input":"2022-07-14T02:41:53.608022Z","iopub.status.idle":"2022-07-14T02:41:53.631990Z","shell.execute_reply.started":"2022-07-14T02:41:53.607987Z","shell.execute_reply":"2022-07-14T02:41:53.631090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shops['category']=LabelEncoder().fit_transform(shops.category)\nshops['city']=LabelEncoder().fit_transform(shops.city)\nshops.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.634782Z","iopub.execute_input":"2022-07-14T02:41:53.635150Z","iopub.status.idle":"2022-07-14T02:41:53.647963Z","shell.execute_reply.started":"2022-07-14T02:41:53.635120Z","shell.execute_reply":"2022-07-14T02:41:53.647133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shops=shops[['shop_id','city','category']]\nshops.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.649203Z","iopub.execute_input":"2022-07-14T02:41:53.649527Z","iopub.status.idle":"2022-07-14T02:41:53.660341Z","shell.execute_reply.started":"2022-07-14T02:41:53.649499Z","shell.execute_reply":"2022-07-14T02:41:53.659618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CATEGORIES DATA SET CLEANING","metadata":{}},{"cell_type":"code","source":"cats['type']=cats['item_category_name'].apply(lambda x: x.split(' ')[0]).astype(str)\ncats.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.661547Z","iopub.execute_input":"2022-07-14T02:41:53.662445Z","iopub.status.idle":"2022-07-14T02:41:53.677093Z","shell.execute_reply.started":"2022-07-14T02:41:53.662401Z","shell.execute_reply":"2022-07-14T02:41:53.676197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Encoder only the main categories, which are most repeated. Put OTHERS for the ones that are not too much repeated.\ncateg=[]\nfor cat in cats.type.unique():\n    if len(cats[cats.type==cat])>=5:\n        categ.append(cat)\ncats.type=cats.type.apply(lambda x: x if (x in categ) else 'others')\ncats.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.678305Z","iopub.execute_input":"2022-07-14T02:41:53.679209Z","iopub.status.idle":"2022-07-14T02:41:53.700514Z","shell.execute_reply.started":"2022-07-14T02:41:53.679155Z","shell.execute_reply":"2022-07-14T02:41:53.699629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cats['type_code']=LabelEncoder().fit_transform(cats['type'])\ncats.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.701849Z","iopub.execute_input":"2022-07-14T02:41:53.702309Z","iopub.status.idle":"2022-07-14T02:41:53.713441Z","shell.execute_reply.started":"2022-07-14T02:41:53.702276Z","shell.execute_reply":"2022-07-14T02:41:53.712744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Seperating category name into two new columns.\ncats['split']=cats['item_category_name'].apply(lambda x: x.split('-'))\ncats['sub_type']=cats['split'].apply(lambda x: x[1].strip() if len(x)>1 else x[0].strip())\ncats.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.714601Z","iopub.execute_input":"2022-07-14T02:41:53.715262Z","iopub.status.idle":"2022-07-14T02:41:53.736898Z","shell.execute_reply.started":"2022-07-14T02:41:53.715216Z","shell.execute_reply":"2022-07-14T02:41:53.736053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cats['sub_type_code']=LabelEncoder().fit_transform(cats.sub_type)\ncats=cats[['item_category_id','type_code','sub_type_code']]\ncats.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.738002Z","iopub.execute_input":"2022-07-14T02:41:53.738384Z","iopub.status.idle":"2022-07-14T02:41:53.750285Z","shell.execute_reply.started":"2022-07-14T02:41:53.738353Z","shell.execute_reply":"2022-07-14T02:41:53.749534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ITEMS DATA SET CLEANING","metadata":{}},{"cell_type":"code","source":"import re\ndef name_correction(x):\n    x = x.lower() # all letters lower case\n    x = x.partition('[')[0] # partition by square brackets\n    x = x.partition('(')[0] # partition by curly brackets\n    x = re.sub('[^A-Za-z0-9А-Яа-я]+', ' ', x) # remove special characters\n    x = x.replace('  ', ' ') # replace double spaces with single spaces\n    x = x.strip() # remove leading and trailing white space\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.751638Z","iopub.execute_input":"2022-07-14T02:41:53.752231Z","iopub.status.idle":"2022-07-14T02:41:53.761421Z","shell.execute_reply.started":"2022-07-14T02:41:53.752195Z","shell.execute_reply":"2022-07-14T02:41:53.760462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split item names by first bracket\nitems[\"name1\"], items[\"name2\"] = items['item_name'].str.split(\"[\", 1).str\nitems[\"name1\"], items[\"name3\"] = items['item_name'].str.split(\"(\", 1).str\n\n# replace special characters and turn to lower case\nitems[\"name2\"] = items['name2'].str.replace('[^A-Za-z0-9А-Яа-я]+', \" \").str.lower()\nitems[\"name3\"] = items['name3'].str.replace('[^A-Za-z0-9А-Яа-я]+', \" \").str.lower()\nitems = items.fillna('0')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:53.763053Z","iopub.execute_input":"2022-07-14T02:41:53.763549Z","iopub.status.idle":"2022-07-14T02:41:54.128388Z","shell.execute_reply.started":"2022-07-14T02:41:53.763503Z","shell.execute_reply":"2022-07-14T02:41:54.127340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Word correction\nitems[\"item_name\"] = items[\"item_name\"].apply(lambda x: name_correction(x))\nitems.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.129712Z","iopub.execute_input":"2022-07-14T02:41:54.130179Z","iopub.status.idle":"2022-07-14T02:41:54.285780Z","shell.execute_reply.started":"2022-07-14T02:41:54.130136Z","shell.execute_reply":"2022-07-14T02:41:54.284792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extract key words of name2 column\nitems[\"type\"] = items['name2'].apply(lambda x: x[0:8] if x.split(\" \")[0] == \"xbox\" else x.split(\" \")[0] )\nitems.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.287162Z","iopub.execute_input":"2022-07-14T02:41:54.287507Z","iopub.status.idle":"2022-07-14T02:41:54.322386Z","shell.execute_reply.started":"2022-07-14T02:41:54.287477Z","shell.execute_reply":"2022-07-14T02:41:54.321301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Cleaning\nitems.loc[(items.type == \"x360\") | (items.type == \"xbox360\") | (items.type == \"xbox 360\") ,\"type\"] = \"xbox 360\"\nitems.loc[ items.type == \"\", \"type\"] = \"mac\"\nitems.type = items.type.apply( lambda x: x.replace(\" \", \"\") )\nitems.loc[ (items.type == 'pc' )| (items.type == 'pс') | (items.type == \"pc\"), \"type\" ] = \"pc\"\nitems.loc[ items.type == 'рs3' , \"type\"] = \"ps3\"","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.324161Z","iopub.execute_input":"2022-07-14T02:41:54.324530Z","iopub.status.idle":"2022-07-14T02:41:54.372033Z","shell.execute_reply.started":"2022-07-14T02:41:54.324497Z","shell.execute_reply":"2022-07-14T02:41:54.371172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\"Item_id\" counting grouped by \"type\". The colum \"item_id\" will be for the new count after grouping\ngroup_sum = items.groupby([\"type\"]).agg({\"item_id\": \"count\"})\ngroup_sum = group_sum.reset_index()\ngroup_sum.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.373416Z","iopub.execute_input":"2022-07-14T02:41:54.373989Z","iopub.status.idle":"2022-07-14T02:41:54.388723Z","shell.execute_reply.started":"2022-07-14T02:41:54.373948Z","shell.execute_reply":"2022-07-14T02:41:54.387670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group_sum.type.unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.390446Z","iopub.execute_input":"2022-07-14T02:41:54.391194Z","iopub.status.idle":"2022-07-14T02:41:54.399441Z","shell.execute_reply.started":"2022-07-14T02:41:54.391136Z","shell.execute_reply":"2022-07-14T02:41:54.398211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Borrar categorias con pocos items\ndrop_cols = []\nfor cat in group_sum.type.unique():\n    if group_sum.loc[(group_sum.type == cat), \"item_id\"].values[0] <40:\n        drop_cols.append(cat)\ndrop_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.400468Z","iopub.execute_input":"2022-07-14T02:41:54.400793Z","iopub.status.idle":"2022-07-14T02:41:54.424613Z","shell.execute_reply.started":"2022-07-14T02:41:54.400766Z","shell.execute_reply":"2022-07-14T02:41:54.423629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.426212Z","iopub.execute_input":"2022-07-14T02:41:54.427299Z","iopub.status.idle":"2022-07-14T02:41:54.441606Z","shell.execute_reply.started":"2022-07-14T02:41:54.427253Z","shell.execute_reply":"2022-07-14T02:41:54.440580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items.name2 = items.name2.apply( lambda x: \"other\" if (x in drop_cols) else x )\nitems = items.drop([\"type\"], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.443187Z","iopub.execute_input":"2022-07-14T02:41:54.443632Z","iopub.status.idle":"2022-07-14T02:41:54.467830Z","shell.execute_reply.started":"2022-07-14T02:41:54.443590Z","shell.execute_reply":"2022-07-14T02:41:54.467040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items[items['name2']==\"other\"]\n#only two item_id had a \"name2\" column with less than 40 values","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.469028Z","iopub.execute_input":"2022-07-14T02:41:54.469450Z","iopub.status.idle":"2022-07-14T02:41:54.485359Z","shell.execute_reply.started":"2022-07-14T02:41:54.469418Z","shell.execute_reply":"2022-07-14T02:41:54.484510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items.name2 = LabelEncoder().fit_transform(items.name2)\nitems.name3 = LabelEncoder().fit_transform(items.name3)\nitems.drop([\"item_name\", \"name1\"],axis = 1, inplace= True)\ntrain.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.491508Z","iopub.execute_input":"2022-07-14T02:41:54.491900Z","iopub.status.idle":"2022-07-14T02:41:54.532970Z","shell.execute_reply.started":"2022-07-14T02:41:54.491867Z","shell.execute_reply":"2022-07-14T02:41:54.531996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SALES (Train) DATAFRAME CLEANING","metadata":{}},{"cell_type":"code","source":"#Esto es por que se necesita un seguimiento de cada item en cada shop en cada mes\nfrom itertools import product\nmatrix=[]\ncols  = [\"date_block_num\", \"shop_id\", \"item_id\"]\nfor i in range(34):\n    sales=train[train.date_block_num==i]\n    matrix.append(np.array(list(product( [i], sales.shop_id.unique(), sales.item_id.unique()))))\n    \nmatrix=pd.DataFrame(np.vstack(matrix),columns=cols)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:41:54.534314Z","iopub.execute_input":"2022-07-14T02:41:54.535361Z","iopub.status.idle":"2022-07-14T02:42:06.924435Z","shell.execute_reply.started":"2022-07-14T02:41:54.535310Z","shell.execute_reply":"2022-07-14T02:42:06.923298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix['date_block_num']=matrix['date_block_num'].astype(int)\nmatrix['shop_id']=matrix['shop_id'].astype(np.int8)\nmatrix['item_id']=matrix['item_id'].astype(np.int16)\nmatrix.sort_values(cols,inplace=True)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:06.925673Z","iopub.execute_input":"2022-07-14T02:42:06.926041Z","iopub.status.idle":"2022-07-14T02:42:12.209550Z","shell.execute_reply.started":"2022-07-14T02:42:06.926010Z","shell.execute_reply":"2022-07-14T02:42:12.208231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['revenue']=train['item_cnt_day']*train['item_price']\ntrain.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:12.210961Z","iopub.execute_input":"2022-07-14T02:42:12.211761Z","iopub.status.idle":"2022-07-14T02:42:12.249780Z","shell.execute_reply.started":"2022-07-14T02:42:12.211722Z","shell.execute_reply":"2022-07-14T02:42:12.248699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group=train.groupby(cols).agg({'item_cnt_day':['sum']})\ngroup.columns=['item_cnt_month']\ngroup.reset_index(inplace=True)\ngroup.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:12.250896Z","iopub.execute_input":"2022-07-14T02:42:12.252176Z","iopub.status.idle":"2022-07-14T02:42:12.991984Z","shell.execute_reply.started":"2022-07-14T02:42:12.252125Z","shell.execute_reply":"2022-07-14T02:42:12.990898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Poner Items_cnt_month in the matrix df\nmatrix=pd.merge(matrix,group,on=cols,how='left')\nmatrix['item_cnt_month']=matrix['item_cnt_month'].fillna(0).astype(np.float16)\nmatrix.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:12.993144Z","iopub.execute_input":"2022-07-14T02:42:12.993468Z","iopub.status.idle":"2022-07-14T02:42:20.774803Z","shell.execute_reply.started":"2022-07-14T02:42:12.993441Z","shell.execute_reply":"2022-07-14T02:42:20.773715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Juntar TEST df con el matrix df.\ntest['date_block_num']=34\ntest[\"date_block_num\"] = test[\"date_block_num\"].astype(np.int8)\ntest['shop_id']=test['shop_id'].astype(np.int8)\ntest['item_id']=test['item_id'].astype(np.int16)\ntest.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:20.776145Z","iopub.execute_input":"2022-07-14T02:42:20.776504Z","iopub.status.idle":"2022-07-14T02:42:20.798974Z","shell.execute_reply.started":"2022-07-14T02:42:20.776474Z","shell.execute_reply":"2022-07-14T02:42:20.798076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop('ID',inplace=True,axis=1)\ntest.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:20.800381Z","iopub.execute_input":"2022-07-14T02:42:20.801035Z","iopub.status.idle":"2022-07-14T02:42:20.818218Z","shell.execute_reply.started":"2022-07-14T02:42:20.800975Z","shell.execute_reply":"2022-07-14T02:42:20.817497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.concat([matrix,test],ignore_index=True,sort=False,keys=cols)\nmatrix.fillna(0,inplace=True)\nmatrix.tail(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:20.819345Z","iopub.execute_input":"2022-07-14T02:42:20.820323Z","iopub.status.idle":"2022-07-14T02:42:20.960426Z","shell.execute_reply.started":"2022-07-14T02:42:20.820286Z","shell.execute_reply":"2022-07-14T02:42:20.959384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## JUNTANDO SHOPS Df al MATRIX df","metadata":{}},{"cell_type":"code","source":"shops.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:20.962174Z","iopub.execute_input":"2022-07-14T02:42:20.962506Z","iopub.status.idle":"2022-07-14T02:42:20.973473Z","shell.execute_reply.started":"2022-07-14T02:42:20.962476Z","shell.execute_reply":"2022-07-14T02:42:20.972472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Two new columns to be added: City and Category\nmatrix=pd.merge(matrix,shops,on=['shop_id'],how='left')\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:20.974509Z","iopub.execute_input":"2022-07-14T02:42:20.975507Z","iopub.status.idle":"2022-07-14T02:42:23.080107Z","shell.execute_reply.started":"2022-07-14T02:42:20.975473Z","shell.execute_reply":"2022-07-14T02:42:23.079119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## JUNTANDO ITEMS Df al MATRIX df","metadata":{}},{"cell_type":"code","source":"items.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:23.081290Z","iopub.execute_input":"2022-07-14T02:42:23.081677Z","iopub.status.idle":"2022-07-14T02:42:23.092880Z","shell.execute_reply.started":"2022-07-14T02:42:23.081637Z","shell.execute_reply":"2022-07-14T02:42:23.091687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Four new columns to be added: item_category, name2, name3 and type\nmatrix=pd.merge(matrix,items,on='item_id',how='left')\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:23.094588Z","iopub.execute_input":"2022-07-14T02:42:23.095409Z","iopub.status.idle":"2022-07-14T02:42:27.791532Z","shell.execute_reply.started":"2022-07-14T02:42:23.095366Z","shell.execute_reply":"2022-07-14T02:42:27.790393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## JUNTANDO CATEGORIES Df al MATRIX df","metadata":{}},{"cell_type":"code","source":"cats.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:27.793515Z","iopub.execute_input":"2022-07-14T02:42:27.797442Z","iopub.status.idle":"2022-07-14T02:42:27.809591Z","shell.execute_reply.started":"2022-07-14T02:42:27.797374Z","shell.execute_reply":"2022-07-14T02:42:27.808506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TWO new columns to be added: type_code, sub_Type_code\nmatrix=pd.merge(matrix,cats,on='item_category_id',how='left')\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:27.811105Z","iopub.execute_input":"2022-07-14T02:42:27.812163Z","iopub.status.idle":"2022-07-14T02:42:30.335700Z","shell.execute_reply.started":"2022-07-14T02:42:27.812117Z","shell.execute_reply":"2022-07-14T02:42:30.334544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix[\"city\"] = matrix[\"city\"].astype(np.int8)\nmatrix[\"category\"] = matrix[\"category\"].astype(np.int8)\nmatrix[\"item_category_id\"] = matrix[\"item_category_id\"].astype(np.int8)\nmatrix[\"sub_type_code\"] = matrix[\"sub_type_code\"].astype(np.int8)\nmatrix[\"name2\"] = matrix[\"name2\"].astype(np.int8)\nmatrix[\"name3\"] = matrix[\"name3\"].astype(np.int16)\nmatrix[\"type_code\"] = matrix[\"type_code\"].astype(np.int8)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:30.337346Z","iopub.execute_input":"2022-07-14T02:42:30.337686Z","iopub.status.idle":"2022-07-14T02:42:30.793233Z","shell.execute_reply.started":"2022-07-14T02:42:30.337656Z","shell.execute_reply":"2022-07-14T02:42:30.792365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CREATING LAGS TO THE FEATURES","metadata":{}},{"cell_type":"code","source":"def lag_feature(df,lags,cols ):\n    for col in cols:\n        print('Adding lag feature in ',col)\n        tmp=df[['date_block_num','shop_id','item_id',col]]\n        for i in lags:\n            shifted=tmp.copy()\n            shifted.columns=['date_block_num','shop_id','item_id',col+'_shifted_'+str(i)]\n            shifted.date_block_num=shifted.date_block_num+i\n            df=pd.merge(df,shifted,on=['date_block_num','shop_id','item_id'],how='left')\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:30.794405Z","iopub.execute_input":"2022-07-14T02:42:30.794725Z","iopub.status.idle":"2022-07-14T02:42:30.802409Z","shell.execute_reply.started":"2022-07-14T02:42:30.794689Z","shell.execute_reply":"2022-07-14T02:42:30.801315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LAG FOR ITEM_CNT_MONTH","metadata":{}},{"cell_type":"code","source":"matrix=lag_feature(matrix,[1,2,3],[\"item_cnt_month\"])\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:42:30.804132Z","iopub.execute_input":"2022-07-14T02:42:30.804621Z","iopub.status.idle":"2022-07-14T02:43:00.259047Z","shell.execute_reply.started":"2022-07-14T02:42:30.804575Z","shell.execute_reply":"2022-07-14T02:43:00.257916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## AVERAGES COUPLES TO DO:\n\n### FOR ITEM_COUNT\n\n\n**DATE_BLOCK_NUM  &  ITEM_CATEGORY_ID  (Lag)**\n\n**DATE_BLOCK_NUM  &  CATEGORY (Lag)**\n \n**DATE_BLOCK_NUM (Lag)**\n\n**DATE_BLOCK_NUM  &  ITEM_ID (Lag)**\n\n\n\n### FOR ITEM PRICE\n\n**ITEM_ID**\n\n**DATE_BLOCK_NUM  &  ITEM_ID (Lag)**","metadata":{}},{"cell_type":"markdown","source":"#### LAG FOR DATE_ITEM_CAT_AVG","metadata":{}},{"cell_type":"code","source":"#Averaging item_cnt_month by \"date_block_num and item_category_id\"\ngroup=matrix.groupby(['date_block_num','item_category_id']).agg({'item_cnt_month':['mean']})\ngroup.columns=['date_item_cat_avg']\ngroup.reset_index(inplace=True)\ngroup.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:00.260268Z","iopub.execute_input":"2022-07-14T02:43:00.260592Z","iopub.status.idle":"2022-07-14T02:43:01.040279Z","shell.execute_reply.started":"2022-07-14T02:43:00.260563Z","shell.execute_reply":"2022-07-14T02:43:01.039304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.merge(matrix,group,on=['date_block_num','item_category_id'],how='left')\nmatrix['date_item_cat_avg']=matrix['date_item_cat_avg'].astype(np.float16)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:01.041419Z","iopub.execute_input":"2022-07-14T02:43:01.041756Z","iopub.status.idle":"2022-07-14T02:43:03.126228Z","shell.execute_reply.started":"2022-07-14T02:43:01.041726Z","shell.execute_reply":"2022-07-14T02:43:03.125150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=lag_feature(matrix,[1,2],['date_item_cat_avg'])\nmatrix.drop(['date_item_cat_avg'],axis=1,inplace=True)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:03.127459Z","iopub.execute_input":"2022-07-14T02:43:03.127793Z","iopub.status.idle":"2022-07-14T02:43:24.331134Z","shell.execute_reply.started":"2022-07-14T02:43:03.127765Z","shell.execute_reply":"2022-07-14T02:43:24.330403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LAG FOR DATA_CAT_AVG","metadata":{}},{"cell_type":"code","source":"#Averaging item_cnt_month by \"date_block_num and category\"\ngroup=matrix.groupby(['date_block_num','category']).agg({'item_cnt_month':['mean']})\ngroup.columns=['date_cat_avg']\ngroup.reset_index(inplace=True)\ngroup.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:24.332505Z","iopub.execute_input":"2022-07-14T02:43:24.333024Z","iopub.status.idle":"2022-07-14T02:43:25.104112Z","shell.execute_reply.started":"2022-07-14T02:43:24.332991Z","shell.execute_reply":"2022-07-14T02:43:25.103434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.merge(matrix,group,on=['date_block_num','category'],how='left')\nmatrix['date_cat_avg']=matrix['date_cat_avg'].astype(np.float16)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:25.105358Z","iopub.execute_input":"2022-07-14T02:43:25.105840Z","iopub.status.idle":"2022-07-14T02:43:28.701949Z","shell.execute_reply.started":"2022-07-14T02:43:25.105810Z","shell.execute_reply":"2022-07-14T02:43:28.700760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=lag_feature(matrix,[1,2],['date_cat_avg'])\nmatrix.drop(['date_cat_avg'],axis=1,inplace=True)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:28.703066Z","iopub.execute_input":"2022-07-14T02:43:28.703414Z","iopub.status.idle":"2022-07-14T02:43:49.896136Z","shell.execute_reply.started":"2022-07-14T02:43:28.703384Z","shell.execute_reply":"2022-07-14T02:43:49.895030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LAG FOR DATE_AVG_ITEM_CNT","metadata":{}},{"cell_type":"code","source":"group=matrix.groupby(['date_block_num']).agg({'item_cnt_month':['mean']})\ngroup.columns=['date_avg_item_cnt']\ngroup.reset_index(inplace=True)\ngroup.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:49.897533Z","iopub.execute_input":"2022-07-14T02:43:49.898591Z","iopub.status.idle":"2022-07-14T02:43:50.220030Z","shell.execute_reply.started":"2022-07-14T02:43:49.898534Z","shell.execute_reply":"2022-07-14T02:43:50.218854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.merge(matrix,group,on='date_block_num',how='left')\nmatrix.date_avg_item_cnt = matrix[\"date_avg_item_cnt\"].astype(np.float16)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:50.221359Z","iopub.execute_input":"2022-07-14T02:43:50.221725Z","iopub.status.idle":"2022-07-14T02:43:54.125777Z","shell.execute_reply.started":"2022-07-14T02:43:50.221695Z","shell.execute_reply":"2022-07-14T02:43:54.125087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=lag_feature(matrix,[1,2],[\"date_avg_item_cnt\"])\nmatrix.drop(['date_avg_item_cnt'],inplace=True,axis=1)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:43:54.126929Z","iopub.execute_input":"2022-07-14T02:43:54.127436Z","iopub.status.idle":"2022-07-14T02:44:16.027373Z","shell.execute_reply.started":"2022-07-14T02:43:54.127403Z","shell.execute_reply":"2022-07-14T02:44:16.026375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LAG FOR DATE_ITEM_AVG_ITEM_CNT","metadata":{}},{"cell_type":"code","source":"group=matrix.groupby(['date_block_num','item_id']).agg({'item_cnt_month':['mean']})\ngroup.columns=['date_item_avg_item_cnt']\ngroup.reset_index(inplace=True)\ngroup.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:44:16.029141Z","iopub.execute_input":"2022-07-14T02:44:16.029689Z","iopub.status.idle":"2022-07-14T02:44:17.143399Z","shell.execute_reply.started":"2022-07-14T02:44:16.029655Z","shell.execute_reply":"2022-07-14T02:44:17.142387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.merge(matrix,group,on=['date_block_num','item_id'],how='left')\nmatrix.date_item_avg_item_cnt=matrix['date_item_avg_item_cnt'].astype(np.float16)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:44:17.145398Z","iopub.execute_input":"2022-07-14T02:44:17.146363Z","iopub.status.idle":"2022-07-14T02:44:22.093095Z","shell.execute_reply.started":"2022-07-14T02:44:17.146308Z","shell.execute_reply":"2022-07-14T02:44:22.091863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=lag_feature(matrix,[1,2,3],['date_item_avg_item_cnt'])\nmatrix.drop(['date_item_avg_item_cnt'],inplace=True,axis=1)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:44:22.094533Z","iopub.execute_input":"2022-07-14T02:44:22.095162Z","iopub.status.idle":"2022-07-14T02:44:54.352519Z","shell.execute_reply.started":"2022-07-14T02:44:22.095124Z","shell.execute_reply":"2022-07-14T02:44:54.351541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LAG FOR DATE_ITEM_ID_PRICE_AVG","metadata":{}},{"cell_type":"code","source":"group=train.groupby(['item_id']).agg({'item_price':['mean']})\ngroup.columns=['item_id_price_avg']\ngroup.reset_index(inplace=True)\ngroup.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:44:54.353797Z","iopub.execute_input":"2022-07-14T02:44:54.354172Z","iopub.status.idle":"2022-07-14T02:44:54.441378Z","shell.execute_reply.started":"2022-07-14T02:44:54.354140Z","shell.execute_reply":"2022-07-14T02:44:54.440269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.merge(matrix,group,on=['item_id'],how='left')\nmatrix['item_id_price_avg']=matrix['item_id_price_avg'].astype(np.float16)\nmatrix.head(3)      \n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:44:54.442661Z","iopub.execute_input":"2022-07-14T02:44:54.442986Z","iopub.status.idle":"2022-07-14T02:44:59.981496Z","shell.execute_reply.started":"2022-07-14T02:44:54.442958Z","shell.execute_reply":"2022-07-14T02:44:59.980470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group=train.groupby(['date_block_num','item_id']).agg({'item_price':['mean']})\ngroup.columns=['date_item_id_price_avg']\ngroup.reset_index(inplace=True)\ngroup.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:44:59.982703Z","iopub.execute_input":"2022-07-14T02:44:59.983062Z","iopub.status.idle":"2022-07-14T02:45:00.240409Z","shell.execute_reply.started":"2022-07-14T02:44:59.983033Z","shell.execute_reply":"2022-07-14T02:45:00.239442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.merge(matrix,group,on=['date_block_num','item_id'],how='left')\nmatrix['date_item_id_price_avg']=matrix['date_item_id_price_avg'].astype(np.float16)\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:00.241313Z","iopub.execute_input":"2022-07-14T02:45:00.241621Z","iopub.status.idle":"2022-07-14T02:45:03.636665Z","shell.execute_reply.started":"2022-07-14T02:45:00.241595Z","shell.execute_reply":"2022-07-14T02:45:03.635689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=lag_feature(matrix,[1,2,3],['date_item_id_price_avg'])\nmatrix.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:03.638110Z","iopub.execute_input":"2022-07-14T02:45:03.638526Z","iopub.status.idle":"2022-07-14T02:45:34.151042Z","shell.execute_reply.started":"2022-07-14T02:45:03.638494Z","shell.execute_reply":"2022-07-14T02:45:34.150147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### DROPPING FEATURES","metadata":{}},{"cell_type":"code","source":"for i in [1,2,3]:\n    matrix['delta_price_shifted_'+str(i)]=(matrix['date_item_id_price_avg_shifted_'+str(i)]-matrix['item_id_price_avg'])/matrix['item_id_price_avg']\n\n\nfeatures_to_drop = [\"item_id_price_avg\", \"date_item_id_price_avg\"]\n\nmatrix.drop(features_to_drop, axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:34.152248Z","iopub.execute_input":"2022-07-14T02:45:34.152594Z","iopub.status.idle":"2022-07-14T02:45:36.680230Z","shell.execute_reply.started":"2022-07-14T02:45:34.152564Z","shell.execute_reply":"2022-07-14T02:45:36.679042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:36.681870Z","iopub.execute_input":"2022-07-14T02:45:36.682248Z","iopub.status.idle":"2022-07-14T02:45:36.696251Z","shell.execute_reply.started":"2022-07-14T02:45:36.682218Z","shell.execute_reply":"2022-07-14T02:45:36.695143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### FORECASTING MODEL LGBM","metadata":{}},{"cell_type":"code","source":"X_train = matrix[matrix.date_block_num < 33].drop(['item_cnt_month'], axis=1)\nY_train = matrix[matrix.date_block_num < 33]['item_cnt_month']\nX_valid = matrix[matrix.date_block_num == 33].drop(['item_cnt_month'], axis=1)\nY_valid = matrix[matrix.date_block_num == 33]['item_cnt_month']\nX_test = matrix[matrix.date_block_num == 34].drop(['item_cnt_month'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:36.697644Z","iopub.execute_input":"2022-07-14T02:45:36.698122Z","iopub.status.idle":"2022-07-14T02:45:42.062662Z","shell.execute_reply.started":"2022-07-14T02:45:36.698085Z","shell.execute_reply":"2022-07-14T02:45:42.061868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = Y_train.clip(0, 20)\nY_valid = Y_valid.clip(0, 20)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:42.064152Z","iopub.execute_input":"2022-07-14T02:45:42.064788Z","iopub.status.idle":"2022-07-14T02:45:42.435043Z","shell.execute_reply.started":"2022-07-14T02:45:42.064741Z","shell.execute_reply":"2022-07-14T02:45:42.434003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = lgbm.Dataset(data=X_train, label=Y_train)\nvalid_data = lgbm.Dataset(data=X_valid, label=Y_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:42.436565Z","iopub.execute_input":"2022-07-14T02:45:42.436923Z","iopub.status.idle":"2022-07-14T02:45:42.441653Z","shell.execute_reply.started":"2022-07-14T02:45:42.436884Z","shell.execute_reply":"2022-07-14T02:45:42.440903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# configuracion de los parametros\nparams = {\"objective\" : \"regression\", \n          \"metric\" : \"rmse\", \n          'n_estimators':10000, \n          'early_stopping_rounds':50,\n          \"num_leaves\" : 2**7-1, \n          \"learning_rate\" : 0.01, \n          \"bagging_fraction\" : 0.9,\n          \"feature_fraction\" : 0.3, \n          \"bagging_seed\" : 0\n         }\n         \n          \n# entrenamiento\nlgbm_model = lgbm.train(params, \n                        train_data, \n                        valid_sets=[train_data, valid_data], \n                        verbose_eval=1000) ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:45:42.442754Z","iopub.execute_input":"2022-07-14T02:45:42.443604Z","iopub.status.idle":"2022-07-14T02:53:16.822997Z","shell.execute_reply.started":"2022-07-14T02:45:42.443565Z","shell.execute_reply":"2022-07-14T02:53:16.821952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm.plot_importance(\n    lgbm_model, \n    max_num_features=50, \n    importance_type='gain', \n    figsize=(12,8));","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:53:16.824927Z","iopub.execute_input":"2022-07-14T02:53:16.825437Z","iopub.status.idle":"2022-07-14T02:53:17.439375Z","shell.execute_reply.started":"2022-07-14T02:53:16.825389Z","shell.execute_reply":"2022-07-14T02:53:17.438389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=lgbm_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:53:17.440881Z","iopub.execute_input":"2022-07-14T02:53:17.441768Z","iopub.status.idle":"2022-07-14T02:53:26.564548Z","shell.execute_reply.started":"2022-07-14T02:53:17.441731Z","shell.execute_reply":"2022-07-14T02:53:26.563702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:53:26.569277Z","iopub.execute_input":"2022-07-14T02:53:26.572396Z","iopub.status.idle":"2022-07-14T02:53:26.581707Z","shell.execute_reply.started":"2022-07-14T02:53:26.572346Z","shell.execute_reply":"2022-07-14T02:53:26.580689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ID': test.index, 'item_cnt_month': pred})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:53:26.583342Z","iopub.execute_input":"2022-07-14T02:53:26.583820Z","iopub.status.idle":"2022-07-14T02:53:27.346895Z","shell.execute_reply.started":"2022-07-14T02:53:26.583787Z","shell.execute_reply":"2022-07-14T02:53:27.345928Z"},"trusted":true},"execution_count":null,"outputs":[]}]}