{"cells":[{"metadata":{"_uuid":"825d1f9c3d9d22be4d2b377c53fa1141a3695fd8","collapsed":true,"_cell_guid":"5583f849-eae8-44b8-98b2-fd6a45ddc826","trusted":false},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport datetime\n%matplotlib inline\nimport glob\nimport missingno as mssno\n\n\nfrom sklearn import preprocessing\nfrom sklearn.linear_model import LogisticRegression\nimport gc\nimport lightgbm as lb\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\n\nfrom sklearn.metrics import accuracy_score,confusion_matrix, roc_auc_score ,roc_curve,auc\nfrom sklearn.model_selection import train_test_split,cross_val_score,GridSearchCV,StratifiedKFold\nfrom sklearn.tree import DecisionTreeClassifier,DecisionTreeRegressor\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import Imputer\nfrom sklearn.preprocessing import PolynomialFeatures\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.feature_selection import VarianceThreshold\nfrom sklearn.feature_selection import SelectFromModel\nfrom sklearn.utils import shuffle","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"94a767285bee9051a0fa5bd2db9e1d94f611f25f","collapsed":true,"_cell_guid":"3a667aba-a337-4b33-a20c-f91c3ad47781","trusted":false},"cell_type":"code","source":"train=pd.read_csv(\"../input/train.csv\",sep=',')\ntest=pd.read_csv(\"../input/test.csv\",sep=',')\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a24c822d3fe1c4bf5906db9c5a5313dd10456184","_cell_guid":"098883d5-d6a1-40a3-91b3-8e323bae7ca4","trusted":false,"collapsed":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"83ce0c7d7aa538ccb65003f1509b33806371d147","_cell_guid":"7d159e70-a8c8-4969-a2be-6b0662904011","trusted":false,"collapsed":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"32a9a36a1a8cc5cd7327f0b2b7b592e20eb9b4e2","_cell_guid":"76b410c3-0253-4687-bddb-5daafa599124","trusted":false,"collapsed":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"624e9a82581133a9db96dd1a4b83a4820f8c638e","_cell_guid":"0fad306d-c717-446a-83e8-a5ec20238cf9","trusted":false,"collapsed":true},"cell_type":"code","source":"train.describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b61300619d4b3826f9e97f1910e57d56148d0ae","collapsed":true,"_cell_guid":"8c5df0a5-d756-458e-8bbf-dde6860d1a8d","trusted":false},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"06371af73c1947df2876699c7e19a2ae03bc5e6c","_cell_guid":"96762872-6986-4c4d-8ceb-0422fe8c4757","trusted":false,"collapsed":true},"cell_type":"code","source":"#counting the null values\ntrain.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"624fc52901c2a3ddcf97fbf33977fc54972140c4","_cell_guid":"f31aa59a-dcc2-4140-b7e6-579e7778b451","trusted":false,"collapsed":true},"cell_type":"code","source":"#null values visualization\nmssno.bar(train,color='g',figsize=(16,5),fontsize=12)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1cbece1345ca2791b472e1d359b6b801815bcdf1","_cell_guid":"a8e62100-43b1-40a2-b883-5087c4f72512","trusted":false,"collapsed":true},"cell_type":"code","source":"mssno.bar(test,color='r',figsize=(16,5),fontsize=12)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"25278278d78c8e74cfd41a7383a89d8a1706bbe3","_cell_guid":"418ad5bc-640a-4d34-a1cd-f66528070575","trusted":false,"collapsed":true},"cell_type":"code","source":"#no. of unique values \na=train.columns\na1=[len(train[col].unique()) for col in a]\nsns.set(font_scale=1.2)\nax = sns.barplot(a, a1, palette='rainbow', log=True)\nax.set(xlabel='Feature', ylabel='log(unique count)', title='Number of unique per feature')\nfor p, uniq in zip(ax.patches, a1):\n    height = p.get_height()\n    ax.text(p.get_x()+p.get_width()/2.,\n            height + 10,\n            uniq,\n            ha=\"center\") \nax.set_xticklabels(ax.get_xticklabels(),rotation=90)\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c10343f155bd3d4abe91fba0c5a1ff9a60d550e2","_cell_guid":"191ea3e9-e714-4038-b11c-8423b546f911","trusted":false,"collapsed":true},"cell_type":"code","source":"#grouping by deal probability\ntrain.groupby('deal_probability').nunique()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"46336bd748ff4b4869fe6e900f91ec70feda110b","collapsed":true,"_cell_guid":"079a7dc8-fdce-4c15-a8ff-bb33ef95184e","trusted":false},"cell_type":"code","source":"#Replacing NULL values with 0.\ntrain = train.replace(np.NaN,0)\ntest = test.replace(np.NaN,0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6e8acb7a670f4f55fe3eb27704b02a8f7540a2b5","collapsed":true,"_cell_guid":"f441ee20-f742-4e90-bbfc-548b50fdbf5d","trusted":false},"cell_type":"code","source":"#converting to datetime format \ntrain.activation_date = pd.to_datetime(train.activation_date)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d19d443d11df1b1a8c916b1c948fa0aab851ea75","collapsed":true,"_cell_guid":"eadb31f3-0f36-401e-8086-d7a5d6d44a87","trusted":false},"cell_type":"code","source":"train['day_of_month'] = train.activation_date.apply(lambda x: x.day)\ntrain['day_of_week'] = train.activation_date.apply(lambda x: x.weekday())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"66f6109151c3f73057c41800a09fab349da72bb1","_cell_guid":"b140b932-b414-4b27-96f7-6ed5f26353a4","trusted":false,"collapsed":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4b68ce4f04aa1d25e58d350f76542b9b20bf543b","collapsed":true,"_cell_guid":"0001a8a1-04c5-400b-96d3-5590e983578e","trusted":false},"cell_type":"code","source":"test.activation_date = pd.to_datetime(test.activation_date)\ntest['day_of_month'] = test.activation_date.apply(lambda x: x.day)\ntest['day_of_week'] = test.activation_date.apply(lambda x: x.weekday())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f6e46ab012d4e116c1e5cfd3f18a765b209ab4ba","_cell_guid":"9da1000f-9a90-4048-b345-3953e6601c85","trusted":false,"collapsed":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c3b9805c36a71d42ec78643f62905919d69668ec","collapsed":true,"_cell_guid":"e5c2bbb2-58c3-463d-9dfa-0dae87e5ffb1","trusted":false},"cell_type":"code","source":"train['char_len_title'] = train.title.apply(lambda x: len(str(x)))\ntrain['char_len_desc'] = train.description.apply(lambda x: len(str(x)))\ntest['char_len_title'] = test.title.apply(lambda x: len(str(x)))\ntest['char_len_desc'] = test.description.apply(lambda x: len(str(x)))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8ba8b8d7f6421800fc11c6a0b01ce90312ea41ff","_cell_guid":"1f7ff7fd-acbe-4369-9afd-750496ae6b04","trusted":false,"collapsed":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bbebe4d5156f4323cc8a90cd962fc40119d3edac","_cell_guid":"142036e9-a90a-4cfa-8d6c-198a0325f4e8","trusted":false,"collapsed":true},"cell_type":"code","source":"cols = ['parent_category_name', 'category_name', 'price', 'user_type', 'item_seq_number', 'image_top_1','day_of_month','day_of_week','char_len_title','char_len_desc']\ndummy_cols = ['parent_category_name', 'category_name','user_type']\ny = train['deal_probability'].copy()\nx_train = train[cols].copy()\nx_test  = test[cols].copy()\ndel train, test; gc.collect()\nn = len(x_train)\nx = pd.concat([x_train, x_test])\nx = pd.get_dummies(x, columns=dummy_cols)\nx.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0c2d77cc400bc41accdfdb5b3085ba98ad8d019d","_cell_guid":"397aaa51-0bf2-4fc8-a076-b009e0160a38","trusted":false,"collapsed":true},"cell_type":"code","source":"x_train = x.iloc[:n, :]\nx_test = x.iloc[n:, :]\ndel x; gc.collect()\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5d5a0cd8ff1ca24a090108c1859b8e49d5f3c093","collapsed":true,"_cell_guid":"2600191e-733b-40a0-ba9c-d93a17f24011","trusted":false},"cell_type":"code","source":"x, x_val, y, y_val = train_test_split(x_train, y, test_size=0.2, random_state=40)\n\n# Create the LightGBM data containers\ntrain_data = lb.Dataset(x, label=y)\nval_data = lb.Dataset(x_val, label=y_val)\n\nparameters = {\n    'task': 'train',\n    'boosting_type': 'gbdt',\n    'objective': 'regression',\n    'metric': 'rmse',\n    'num_leaves': 31,\n    'learning_rate': 0.05,\n    'feature_fraction': 0.9,\n    'bagging_fraction': 0.8,\n    'bagging_freq': 5,\n    'verbose': 50\n}\n\nmodel = lb.train(parameters,\n                  train_data,\n                  valid_sets=val_data,\n                  num_boost_round=2000,\n                  early_stopping_rounds=120,\n                  verbose_eval=50)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"03a9e429d7e4973248aa6e09c7c79c1124e610d5","_cell_guid":"6d4bcd67-f50c-44cf-a8f7-8d1fd716491a","trusted":false,"collapsed":true},"cell_type":"code","source":"\n#will update as i improve the result.\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6e8c6152dc4ba8d3622a99fdedae43af18100a06","_cell_guid":"3610b243-aa52-490b-ac5d-e2e919cf4e07","trusted":false,"collapsed":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b8cd1736add042c8a7d9a58c350bbc0884af6d89","collapsed":true,"_cell_guid":"51419a14-ce14-4105-8d27-c105648db42d","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}