{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nfrom pandas.io.json import json_normalize\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport datetime\ncolor = sns.color_palette()\n%matplotlib inline\n\nfrom plotly import tools\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\n\nfrom sklearn import model_selection, preprocessing, metrics\nimport lightgbm as lgb\n\npd.options.mode.chained_assignment = None\npd.options.display.max_columns = 999","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0bafd446c17b20746f7fe496f6f0c5b9a3d1d57c"},"cell_type":"code","source":"def load_df(csv_path='../input/train.csv', nrows=None):\n    JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    \n    df = pd.read_csv(csv_path, \n                     converters={column: json.loads for column in JSON_COLUMNS}, \n                     dtype={'fullVisitorId': 'str'}, # Important!!\n                     nrows=nrows)\n    \n    for column in JSON_COLUMNS:\n        column_as_df = json_normalize(df[column])\n        column_as_df.columns = [f\"{column}.{subcolumn}\" for subcolumn in column_as_df.columns]\n        df = df.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)\n    print(f\"Loaded {os.path.basename(csv_path)}. Shape: {df.shape}\")\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f255e40329e9d89a4d59cced818b077e9fe4b3b6","scrolled":false},"cell_type":"code","source":"#show traindata\ntrain_df = load_df()\ntest_df = load_df(\"../input/test.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce9b2e53b9591d8a2617b7da19cb5dbb6b4102a6","scrolled":true},"cell_type":"code","source":"\n#train_df[\"totals.transactionRevenue\"] = train_df[\"totals.transactionRevenue\"].fillna(0,inplace=True)\ny_data = [\"date\",\"totals.transactionRevenue\"]\ntrain_y = train_df[y_data].astype(\"float\")\n#train_y = train_y.groupby(\"fullVisitorId\").sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5d051c60494d47299ad4fb25f7dd97bee096a5a"},"cell_type":"code","source":"\n#conbain train and test\n# Align the training and testing data, keep only columns present in both dataframes\ntrain_df, test_df = train_df.align(test_df, join = 'inner', axis = 1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e49a698bee0c30511fe13cfbd66ff3252f5b32d5"},"cell_type":"code","source":"#check column which have only one category\none_category = [c for c in train_df.columns if train_df[c].nunique()==1]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"738fb41a9b64094ccf21fbf7b2db6d7f0d887577"},"cell_type":"code","source":"train_df = train_df.drop(one_category,axis=1)\ntest_df = test_df.drop(one_category,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"25a0a144c20dff4c8675d02fac8ea637de2a10bf","scrolled":true},"cell_type":"code","source":"# Impute 0 for missing target values\n#train_y.fillna(0, inplace=True).value\n#train_y = train_df[\"totals.transactionRevenue\"].values\ntrain_id = train_df[\"fullVisitorId\"].values\ntest_id = test_df[\"fullVisitorId\"].values\n\n# label encode the categorical variables and convert the numerical variables to float\ncat_cols = [\"channelGrouping\", \"device.browser\", \"device.deviceCategory\", \"device.operatingSystem\", \n            \"geoNetwork.city\", \"geoNetwork.continent\", \"geoNetwork.country\", \"geoNetwork.metro\",\n            \"geoNetwork.networkDomain\", \"geoNetwork.region\", \"geoNetwork.subContinent\", \"trafficSource.adContent\", \n            \"trafficSource.adwordsClickInfo.adNetworkType\", \"trafficSource.adwordsClickInfo.gclId\", \n            \"trafficSource.adwordsClickInfo.page\", \"trafficSource.adwordsClickInfo.slot\", \"trafficSource.campaign\",\n            \"trafficSource.keyword\", \"trafficSource.medium\", \"trafficSource.referralPath\", \"trafficSource.source\"]\nnum_cols = [\"totals.hits\", \"totals.pageviews\"]\nfor col in cat_cols:\n    print(col)\n    lbl = preprocessing.LabelEncoder()\n    lbl.fit(list(train_df[col].values.astype('str')) + list(test_df[col].values.astype('str')))\n    train_df[col] = lbl.transform(list(train_df[col].values.astype('str')))\n    test_df[col] = lbl.transform(list(test_df[col].values.astype('str')))\nfor col in num_cols:\n    train_df[col] = train_df[col].astype(float)\n    test_df[col] = test_df[col].astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08062b33d5907aea1f34d1f9090887488fe31e4a","_kg_hide-input":false,"_kg_hide-output":false},"cell_type":"code","source":"train_df['date'] = train_df['date'].apply(lambda x: datetime.date(int(str(x)[:4]), int(str(x)[4:6]), int(str(x)[6:])))\ntest_df['date'] = test_df['date'].apply(lambda x: datetime.date(int(str(x)[:4]), int(str(x)[4:6]), int(str(x)[6:])))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"09959e774e1f4a14b8d6d0fe7d0d71b7f8efa2d5","_kg_hide-input":true},"cell_type":"code","source":"dev_bool = train_df[\"date\"] <= datetime.date(2017,5,31)\nval_bool = train_df[\"date\"] > datetime.date(2017,5,31)\n\n# Split the train dataset into development and valid based on time \ndev_df = train_df[dev_bool]\nval_df = train_df[val_bool]\ndev_y = train_y[dev_bool]\nval_y = train_y[val_bool]\n\n\ndev_X = dev_df[cat_cols + num_cols]\nval_X = val_df[cat_cols + num_cols]\ntest_X = test_df[cat_cols + num_cols]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f3999861410c0c8b2246c79a5f4bdf7e21e7020"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af1a43cbb6e630dfc45e7d9bd9dbf2057f0b0ba1"},"cell_type":"code","source":"dev_y[\"totals.transactionRevenue\"].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78f00b69c3647e9346f97bd8ed858aaaf55a3d61"},"cell_type":"code","source":"val_y[\"totals.transactionRevenue\"].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbaa4373bb24edee3593aac88cb46613cbc75baa"},"cell_type":"code","source":"dev_y[\"totals.transactionRevenue\"].fillna(0,inplace=True)\nval_y[\"totals.transactionRevenue\"].fillna(0,inplace=True)\ndev_y = np.log1p(dev_y[\"totals.transactionRevenue\"].values)\nval_y = np.log1p(val_y[\"totals.transactionRevenue\"].values)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fb27acfb9e1c4132fabba7061aa007913ab7c509"},"cell_type":"markdown","source":"#グリッドサーチと交差検証を行う\nfrom sklearn.cross_validation import train_test_split\nfrom sklearn.model_selection import cross_val_score\n\n\n#サーチするパラメータの設定\nparam_list = [0.001, 0.01, 0.1, 1, 10, 100]\nparam_list_subsumple = [0.0001,0.001,0.1,0.2]\nbest_score = 0\nbest_parameters  = {}\n\nfor learning_rate in param_list:\n        LBGM = lgb.LGBMRegressor(gamma=gamma, C=C)\n        # cross_val_score() による交差検証\n        scores = cross_val_score(LGBM, dev_X, dev_y, cv=5)\n        # k 個の評価値の平均を用いる\n        score = np.mean(scores)\n        if score > best_score:\n            best_score = score\n            best_parameters = {'learning_rate' : learning_rate}\n"},{"metadata":{"trusted":true,"_uuid":"94d15e663bfa9914041735d270be3753a09c43b1","scrolled":false},"cell_type":"code","source":"param_list = [0.01,0.03,0.04,0.05,0.1,0.2,0.25,0.3]\n#0.03,0.2が良い\ndef run_lgb(train_X, train_y, val_X, val_y, test_X,learning_rate=0.1):\n    params = {\n        \"objective\" : \"regression\",\n        \"metric\" : \"rmse\",\n        \"num_leaves\" : 30,\n        \"learning_rate\" : learning_rate,\n        \"bagging_fraction\" : 0.7,\n        \"feature_fraction\" : 0.5,\n        \"bagging_frequency\" : 5,\n        \"bagging_seed\" : 2018,\n        \"verbosity\" : -1\n        }\n\n    lgtrain = lgb.Dataset(train_X, label=train_y)\n    lgval = lgb.Dataset(val_X, label=val_y)\n    model = lgb.train(params, lgtrain, 1000, valid_sets=[lgval], early_stopping_rounds=100, verbose_eval=100)\n    \n    pred_test_y = model.predict(test_X, num_iteration=model.best_iteration)\n    return pred_test_y, model\n# Training the model #\n#bestmodel = []\nbestscore = 1000\n#best_learning_rate = []\n#bestpred_test = []\nfor learning_rate in param_list:\n    print(\"\\n\"+\"learning_rate =\"+str(learning_rate))\n    pred_test, model = run_lgb(dev_X, dev_y, val_X, val_y, test_X,learning_rate)\n    if bestscore > model.best_score[\"valid_0\"][\"rmse\"]:\n        bestmodel = model\n        bestscore = model.best_score[\"valid_0\"][\"rmse\"]\n        best_learning_rate = learning_rate\n        bestpred_test = pred_test\nprint(\"bestscore=\"+str(model.best_score[\"valid_0\"][\"rmse\"])+\" best_learning_rate\"+str(best_learning_rate))        \n ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01e4e1c86cdfa2e79dcda8475c4426c01bc51fa4"},"cell_type":"markdown","source":"transactionRevenue = train_y[\"totals.transactionRevenue\"]\ntransactionRevenue.fillna(0,inplace=True)\ntrain_y = train_y\n"},{"metadata":{"trusted":true,"_uuid":"9eeaaccbb3e7204e247aff972d17f87831e17cd2"},"cell_type":"code","source":"def make_csv(pred,id=test_id,name=\"baseline_lgb.csv\"):\n    sub_df = pd.DataFrame({\"fullVisitorId\":id})\n    pred[pred<0] = 0\n    sub_df[\"PredictedLogRevenue\"] = np.expm1(pred)\n    sub_df = sub_df.groupby(\"fullVisitorId\")[\"PredictedLogRevenue\"].sum().reset_index()\n    sub_df.columns = [\"fullVisitorId\", \"PredictedLogRevenue\"]\n    sub_df[\"PredictedLogRevenue\"] = np.log1p(sub_df[\"PredictedLogRevenue\"])\n    sub_df.to_csv(name, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d54c3abc0996e510d4acd4c40245f7476f3d0e4"},"cell_type":"code","source":"sub_df = pd.DataFrame({\"fullVisitorId\":test_id})\nbestpred_test[bestpred_test<0] = 0\nsub_df[\"PredictedLogRevenue\"] = np.expm1(bestpred_test)\nsub_df = sub_df.groupby(\"fullVisitorId\")[\"PredictedLogRevenue\"].sum().reset_index()\nsub_df.columns = [\"fullVisitorId\", \"PredictedLogRevenue\"]\nsub_df[\"PredictedLogRevenue\"] = np.log1p(sub_df[\"PredictedLogRevenue\"])\nsub_df.to_csv(\"baseline_lgb.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1d5c2d32715d0e260708b748e0d069538a2abf0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74f36b02f26a13178ec09e24a352719687fef7ff","scrolled":true},"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12,18))\nlgb.plot_importance(bestmodel, max_num_features=50, height=0.8, ax=ax)\nax.grid(False)\nplt.title(\"LightGBM - Feature Importance\", fontsize=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c21c534d25c8542adf043b8081f88fdc1aff37b2"},"cell_type":"markdown","source":"#データにどれほどNanがあるかを計算してくれる\n# Function to calculate missing values by column# Funct \ndef missing_values_table(df):\n        # Total missing values\n        mis_val = df.isnull().sum()\n        \n        # Percentage of missing values\n        mis_val_percent = 100 * df.isnull().sum() / len(df)\n        \n        # Make a table with the results\n        mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)\n        \n        # Rename the columns\n        mis_val_table_ren_columns = mis_val_table.rename(\n        columns = {0 : 'Missing Values', 1 : '% of Total Values'})\n        \n        # Sort the table by percentage of missing descending\n        mis_val_table_ren_columns = mis_val_table_ren_columns[\n            mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(\n        '% of Total Values', ascending=False).round(1)\n        \n        # Print some summary information\n        print (\"Your selected dataframe has \" + str(df.shape[1]) + \" columns.\\n\"      \n            \"There are \" + str(mis_val_table_ren_columns.shape[0]) +\n              \" columns that have missing values.\")\n        \n        # Return the dataframe with missing information\n        return mis_val_table_ren_columns"},{"metadata":{"trusted":true,"_uuid":"c801a75a40c7fb81cdc3e8f62552a1998ed2ea78"},"cell_type":"markdown","source":"missing_values =missing_values_table(train_df)\nmissing_values.head()\n"},{"metadata":{"trusted":true,"_uuid":"812d150add4f1a1c6bf321332d4c2a9a77c31d55"},"cell_type":"markdown","source":"missing_values =missing_values_table(test_df)\nmissing_values.head()\n"},{"metadata":{"trusted":true,"_uuid":"9535e16677307f6c617d25c4c64fd8d2f50ffe7d"},"cell_type":"code","source":"train_df[\"totals.pageviews\"].fillna(0,inplace=True)\ntest_df[\"totals.pageviews\"].fillna(0,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4552e709cda76a45f3e8d488e698accad6fa6961"},"cell_type":"markdown","source":"type(bestmodel.feature_name())"},{"metadata":{"trusted":true,"_uuid":"0eae438527334937ac4da7efb4e010230f52a112"},"cell_type":"code","source":"FeatureImportance = pd.DataFrame({\"name\":bestmodel.feature_name(),\n                                  \"importance\":bestmodel.feature_importance()})\nFeatureImportance.sort_values(\"importance\")\nPF_list = FeatureImportance.sort_values(\"importance\",ascending=False)\nPF_list = PF_list[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"620a5b98729935275912a98e267199435281da51"},"cell_type":"code","source":"PF_list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e999f45f5b9b59db8e1ef5f19d28cf5a521ce6af"},"cell_type":"code","source":"# Make a new dataframe for polynomial features\npoly_features_train = train_df[PF_list[\"name\"]]\npoly_features_test = test_df[PF_list[\"name\"]]\n\n# imputer for handling missing values\n#from sklearn.preprocessing import Imputer\n#imputer = Imputer(strategy = 'median')\n\n#poly_target = poly_features['TARGET']\n\n#poly_features = poly_features.drop(columns = ['TARGET'])\n\n# Need to impute missing values\n#poly_features = imputer.fit_transform(poly_features)\n#poly_features_test = imputer.transform(poly_features_test)\n\nfrom sklearn.preprocessing import PolynomialFeatures\n                                  \n# Create the polynomial object with specified degree\npoly_transformer = PolynomialFeatures(degree = 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e45724013ae49c92d1c35286f2e1e51eae1b7c3c"},"cell_type":"code","source":"# Train the polynomial features\npoly_transformer.fit(poly_features_train)\n\n# Transform the features\npoly_features_train = poly_transformer.transform(poly_features_train)\npoly_features_test = poly_transformer.transform(poly_features_test)\nprint('Polynomial Features shape: ', poly_features_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d3ce0be0f7651d8700c148bfa2d0da5ff267ecf"},"cell_type":"code","source":"list(PF_list[\"name\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b68e9aafe89fd3dbc58ddfd129d4b771c45879f"},"cell_type":"code","source":"poly_transformer.get_feature_names(input_features = list(PF_list[\"name\"]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c9fdb3e8a621dd8d202d6d0d592eaaf182310ef"},"cell_type":"code","source":"poly_features_train = pd.DataFrame(poly_features_train,\n                                   columns = poly_transformer.get_feature_names(\n                                       list(PF_list[\"name\"])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29e6e5aff83b4828c26504fa5dbac390f7382a68"},"cell_type":"code","source":"poly_features_test = pd.DataFrame(poly_features_test,columns = poly_transformer.get_feature_names(list(PF_list[\"name\"])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"587531caf268902ccc4c9791a3eec6ff33d87307"},"cell_type":"code","source":"train_df_poly = pd.concat([train_df,poly_features_train],axis=1)\ntrain_df_poly.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df072b720eec3dd31730a1d9fc5c5ed53bdaf735"},"cell_type":"code","source":"test_df_poly = pd.concat([test_df,poly_features_test],axis=1)\ntest_df_poly.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ddc110760c91786e4323a80109edaec3946ac39"},"cell_type":"code","source":"PF_namelist = list(poly_features_train.columns)\nPF_namelist.pop(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af487ade42e43049141650cdcb2e664e8b4933b8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"252b69be1ca14712a869a66188518ad31db9178b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"441389209b111f9b40d192f76f581fbb06fe278d"},"cell_type":"code","source":"PF_namelist = list(set(PF_namelist+cat_cols+num_cols))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1b910b86a9cdb3d2287fc5cf5f447372f782b38"},"cell_type":"code","source":"dev_poly_df = train_df_poly[dev_bool]\nval_poly_df = train_df_poly[val_bool]\ndev_poly_y = train_y[dev_bool]\nval_poly_y = train_y[val_bool]\n\n\ndev_poly_X = dev_poly_df[PF_namelist]\nval_poly_X = val_poly_df[PF_namelist]\ntest_poly_X = test_df_poly[PF_namelist]\ndev_poly_y[\"totals.transactionRevenue\"].fillna(0,inplace=True)\nval_poly_y[\"totals.transactionRevenue\"].fillna(0,inplace=True)\ndev_poly_y = np.log1p(dev_poly_y[\"totals.transactionRevenue\"].values)\nval_poly_y = np.log1p(val_poly_y[\"totals.transactionRevenue\"].values)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f0f15ec27e1ec0eee9e549f6a58fabed48c85ea"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5b334b2d3cae9da7897c9a0b836bb0aca6cf94c"},"cell_type":"code","source":"param_list = [0.01,0.03,0.04,0.05,0.1,0.2,0.25,0.3]\n\nbestscore_poly = 1000\n#best_learning_rate = []\n#bestpred_test = []\nfor learning_rate in param_list:\n    print(\"\\n\"+\"learning_rate =\"+str(learning_rate))\n    pred_test, model = run_lgb(dev_poly_X, dev_poly_y, val_poly_X, val_poly_y, test_poly_X,learning_rate)\n    if bestscore_poly > model.best_score[\"valid_0\"][\"rmse\"]:\n        bestmodel_poly = model\n        bestscore_poly = model.best_score[\"valid_0\"][\"rmse\"]\n        best_learning_rate_poly = learning_rate\n        bestpred_test_poly = pred_test\nprint(\"bestscore=\"+str(model.best_score[\"valid_0\"][\"rmse\"])+\" best_learning_rate\"+str(best_learning_rate))        \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"577615f99c43fb59863b7bd6b7f74bc94809bcb5"},"cell_type":"code","source":"make_csv(pred=bestpred_test_poly,name=\"polynomical_lgb\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1e684851490082df487924649232fed0b71ee17"},"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12,18))\nlgb.plot_importance(bestmodel_poly, max_num_features=50, height=0.8, ax=ax)\nax.grid(False)\nplt.title(\"LightGBM - Feature Importance\", fontsize=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"bec8a499af0606ad91a7318e62b1f20b9650298e"},"cell_type":"code","source":"FeatureImportance = pd.DataFrame({\"name\":bestmodel_poly.feature_name(),\n                                  \"importance\":bestmodel_poly.feature_importance()})\nFeatureImportance.sort_values(\"importance\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ae7a8d26fe7038c9f5ee01db5393abe9161187b"},"cell_type":"code","source":"FeatureImportance2 = FeatureImportance[FeatureImportance[\"importance\"] > 50]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"668c233c91e067e6d23947f5a714d988dd538f74"},"cell_type":"code","source":"PF_namelist2 = list(FeatureImportance2[\"name\"])\nPF_namelist2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"989886781532bba4946b61800fc0538717ea41da"},"cell_type":"code","source":"len(PF_namelist2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"d6c98d237c043bac8f3bae0eb5261b0771170b50"},"cell_type":"code","source":"PF_namelist","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"907d2d924f946f5f2461d1d9db33c88edbeee72d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76f9329318f3befa415dc888bc493420c7227adf"},"cell_type":"code","source":"for i in range(len(PF_namelist2)):\n    PF_namelist2[i] = PF_namelist2[i].replace(\"_\",\" \")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8c3c0ccd50d6c4eaa622d9c161ef607edb7ead5"},"cell_type":"code","source":"PF_namelist2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"170416ba56a17133ae4ed8fa2540f54ac45e4d0f"},"cell_type":"code","source":"PF_namelist","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee29792894c10e4a6fdc38fa8586f83363a70fd8"},"cell_type":"code","source":"dev_poly_df = train_df_poly[dev_bool]\nval_poly_df = train_df_poly[val_bool]\ndev_poly_y = train_y[dev_bool]\nval_poly_y = train_y[val_bool]\n\ndev_poly2_X = dev_poly_df[PF_namelist2]\nval_poly2_X = val_poly_df[PF_namelist2]\ntest_poly2_X = test_df_poly[PF_namelist2]\ndev_poly_y[\"totals.transactionRevenue\"].fillna(0,inplace=True)\nval_poly_y[\"totals.transactionRevenue\"].fillna(0,inplace=True)\ndev_poly_y = np.log1p(dev_poly_y[\"totals.transactionRevenue\"].values)\nval_poly_y = np.log1p(val_poly_y[\"totals.transactionRevenue\"].values)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74bea7d5dc5469078627e5bc96f331bcdb09bb90"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2794bd0195cd1397e17a138b075216f8d7a03a34"},"cell_type":"code","source":"bestscore_poly2 = 1000\n#best_learning_rate = []\n#bestpred_test = []\nfor learning_rate in param_list:\n    print(\"\\n\"+\"learning_rate =\"+str(learning_rate))\n    pred_test, model = run_lgb(dev_poly2_X, dev_poly_y, val_poly2_X, val_poly_y, test_poly2_X,learning_rate)\n    if bestscore_poly2 > model.best_score[\"valid_0\"][\"rmse\"]:\n        bestmodel_poly2 = model\n        bestscore_poly2 = model.best_score[\"valid_0\"][\"rmse\"]\n        best_learning_rate_poly2 = learning_rate\n        bestpred_test_poly2 = pred_test\nprint(\"bestscore=\"+str(model.best_score[\"valid_0\"][\"rmse\"])+\" best_learning_rate\"+str(best_learning_rate))        \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0395cd78e3ac75f229b506778539d95b6e2a76b3"},"cell_type":"code","source":"make_csv(pred=bestpred_test_poly2,name=\"polynomical2_lgb.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6774eeb5a3f73dc7764ec0207299c3fe996d36a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"789abef1cae8d5da67c66a1660e3f8f65f255da0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"115ce600280b2a9a5c820d941878789f76cd17e8"},"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.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}