{"cells":[{"metadata":{"trusted":true,"_uuid":"0561618567c0157422e767b7d528231e569500de"},"cell_type":"code","source":"import sys\nprint(sys.executable)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8107a4e7ba1d7c6bfbc1025115e796d3fcffddfc"},"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\nimport json\nfrom pandas.io.json import json_normalize\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport os\nimport json\nimport datetime\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom pandas.io.json import json_normalize\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, BatchNormalization, Activation, Dropout\nfrom keras import optimizers\n\n%matplotlib inline\npd.options.display.max_columns = 999","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8e9c80f3eff981e2a551f47d9a1ab2d4b90a75f"},"cell_type":"code","source":"def load_df(csv_path, nrows=None):\n    USE_COLUMNS = [\n        'channelGrouping', 'date', 'device', 'fullVisitorId', 'geoNetwork',\n        'socialEngagementType', 'totals', 'trafficSource', 'visitId',\n        'visitNumber', 'visitStartTime', 'customDimensions'\n        #'hits'\n    ]\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, usecols=USE_COLUMNS)\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":"87c87fbe3bd2c262e47d2fa4a58eae2db92983b3"},"cell_type":"code","source":"%%time\ntrain = load_df(\"../input/train_v2.csv\")\ntest = load_df(\"../input/test_v2.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca78c8a5269231f6c6c969b995cc55b242c66c6f"},"cell_type":"code","source":"\ndef add_time_features(df):\n    df['date'] = pd.to_datetime(df['date'], format='%Y%m%d', errors='ignore')\n    df['year'] = df['date'].apply(lambda x: x.year)\n    df['month'] = df['date'].apply(lambda x: x.month)\n    df['day'] = df['date'].apply(lambda x: x.day)\n    df['weekday'] = df['date'].apply(lambda x: x.weekday())\n    \n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"70379f7d5b74e5be340bdfa2a6e6104d293a440f"},"cell_type":"code","source":"train_df = train.copy()\ntest_df = test.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17a0c84d81a3b745117e508a7bebf55c7b970abd"},"cell_type":"code","source":"\nprint('TRAIN SET')\nprint('Rows: %s' % train.shape[0])\nprint('Columns: %s' % train.shape[1])\nprint('Features: %s' % train.columns.values)\nprint()\nprint('TEST SET')\nprint('Rows: %s' % test.shape[0])\nprint('Columns: %s' % test.shape[1])\nprint('Features: %s' % test.columns.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29191067626c84534d1cadd6b511a6416736884a"},"cell_type":"code","source":"train = add_time_features(train)\ntest = add_time_features(test)\n# Convert target feature to 'float' type.\ntrain['totals.transactionRevenue'] = train[\"totals.transactionRevenue\"].astype('float')\ntrain['totals.pageviews'] = train['totals.pageviews'].astype(float)\ntrain['totals.hits'] = train['totals.hits'].astype(float)\ntest['totals.pageviews'] = test['totals.pageviews'].astype(float)\ntest['totals.transactionRevenue'] = test[\"totals.transactionRevenue\"].astype('float')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f11a663f17911f12d6ac1f03d4ed4c58ff33ccb"},"cell_type":"code","source":"test['totals.hits'] = test['totals.hits'].astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fafe4f30e33ac730549b9b6ad19cdd8c171d9478"},"cell_type":"code","source":"# Train\ngp_fullVisitorId_train = train.groupby(['fullVisitorId']).agg('sum')\ngp_fullVisitorId_train['fullVisitorId'] = gp_fullVisitorId_train.index\ngp_fullVisitorId_train['mean_hits_per_day'] = gp_fullVisitorId_train.groupby(['day'])['totals.hits'].transform('mean')\ngp_fullVisitorId_train['mean_pageviews_per_day'] = gp_fullVisitorId_train.groupby(['day'])['totals.pageviews'].transform('mean')\ngp_fullVisitorId_train['sum_hits_per_day'] = gp_fullVisitorId_train.groupby(['day'])['totals.hits'].transform('sum')\ngp_fullVisitorId_train['sum_pageviews_per_day'] = gp_fullVisitorId_train.groupby(['day'])['totals.pageviews'].transform('sum')\ngp_fullVisitorId_train = gp_fullVisitorId_train[['fullVisitorId', 'mean_hits_per_day', 'mean_pageviews_per_day', 'sum_hits_per_day', 'sum_pageviews_per_day']]\ntrain = train.join(gp_fullVisitorId_train, on='fullVisitorId', how='inner', rsuffix='_')\ntrain.drop(['fullVisitorId_'], axis=1, inplace=True)\n\n# Test\ngp_fullVisitorId_test = test.groupby(['fullVisitorId']).agg('sum')\ngp_fullVisitorId_test['fullVisitorId'] = gp_fullVisitorId_test.index\ngp_fullVisitorId_test['mean_hits_per_day'] = gp_fullVisitorId_test.groupby(['day'])['totals.hits'].transform('mean')\ngp_fullVisitorId_test['mean_pageviews_per_day'] = gp_fullVisitorId_test.groupby(['day'])['totals.pageviews'].transform('mean')\ngp_fullVisitorId_test['sum_hits_per_day'] = gp_fullVisitorId_test.groupby(['day'])['totals.hits'].transform('sum')\ngp_fullVisitorId_test['sum_pageviews_per_day'] = gp_fullVisitorId_test.groupby(['day'])['totals.pageviews'].transform('sum')\ngp_fullVisitorId_test = gp_fullVisitorId_test[['fullVisitorId', 'mean_hits_per_day', 'mean_pageviews_per_day', 'sum_hits_per_day', 'sum_pageviews_per_day']]\ntest = test.join(gp_fullVisitorId_test, on='fullVisitorId', how='inner', rsuffix='_')\ntest.drop(['fullVisitorId_'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79127b563cc86fd519053b247556bd2e91a7c3e2"},"cell_type":"code","source":"time_agg = train.groupby('date')['totals.transactionRevenue'].agg(['count', 'sum'])\nyear_agg = train.groupby('year')['totals.transactionRevenue'].agg(['sum'])\nmonth_agg = train.groupby('month')['totals.transactionRevenue'].agg(['sum'])\nday_agg = train.groupby('day')['totals.transactionRevenue'].agg(['sum'])\nweekday_agg = train.groupby('weekday')['totals.transactionRevenue'].agg(['count','sum'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b037bdf6f21d106962e155d47d3125e9961bd7fe"},"cell_type":"code","source":"plt.figure(figsize=(20,7))\nplt.ticklabel_format(axis='y', style='plain')\nplt.ylabel('Sum transactionRevenue', fontsize=12)\nplt.xlabel('Date', fontsize=12)\nplt.scatter(time_agg.index.values, time_agg['sum'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c885ba82193067575f17d182e98f463d15be92c"},"cell_type":"code","source":"plt.figure(figsize=(20,7))\nplt.ticklabel_format(axis='y', style='plain')\nplt.ylabel('Frequency', fontsize=12)\nplt.xlabel('Date', fontsize=12)\nplt.scatter(time_agg.index.values, time_agg['count'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6c12da555ba76ed5e259a58593ef6e65b63ac44"},"cell_type":"code","source":"fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(20,7))\nax1.scatter(year_agg.index.values, year_agg['sum'])\nax1.locator_params(nbins=2)\nax1.ticklabel_format(axis='y', style='plain')\nax1.set_xlabel('Year', fontsize=12)\n\nax2.scatter(month_agg.index.values, month_agg['sum'])\nax2.locator_params(nbins=12)\nax2.ticklabel_format(axis='y', style='plain')\nax2.set_xlabel('Month', fontsize=12)\n\nax3.scatter(day_agg.index.values, day_agg['sum'])\nax3.locator_params(nbins=10)\nax3.ticklabel_format(axis='y', style='plain')\nax3.set_xlabel('Day', fontsize=12)\n\nax4.scatter(weekday_agg.index.values, weekday_agg['sum'])\nax4.locator_params(nbins=7)\nax4.ticklabel_format(axis='y', style='plain')\nax4.set_xlabel('Weekday', fontsize=12)\n\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e67554a3eeb67c1bbeeaccf8068da69c735a62f"},"cell_type":"code","source":"\ntrain[\"totals.transactionRevenue\"].fillna(0, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0615e7b21584c24f9e09a688de9c91940f1580fd"},"cell_type":"code","source":"train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a48a08b1a35d8492c22456f698ec7aed1016ddf0"},"cell_type":"code","source":"# dropiing the unwanted columns\nunwanted_columns = ['customDimensions', 'day']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16813a71d9a567e1fdd624f566b3945d96d5ff43"},"cell_type":"code","source":"train = train.drop(unwanted_columns, axis=1)\ntest = test.drop(unwanted_columns, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"faf3dc7ed28897272742c70ee74cc933420e8c4d"},"cell_type":"code","source":"constant_columns = [c for c in train.columns if train[c].nunique()<=1]\nprint('Columns with constant values: ', constant_columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3bf8b250506ad3ec06b3cc154aa4813b699c0700"},"cell_type":"code","source":"train = train.drop(constant_columns, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"225563f60e903ef4344c724bbd575a43eebe362d"},"cell_type":"code","source":"constant_columns = [c for c in test.columns if test[c].nunique()<=1]\nprint('Columns with constant values: ', constant_columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c4af4457bc5a621745e6ede273e564729fa973e"},"cell_type":"code","source":"test = test.drop(constant_columns, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2df075ed4e27a3a44866959a5b7893c269cac068"},"cell_type":"code","source":"train = train.drop('year', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"167da5bf30f537c3d47e306170eeef361245ecc9"},"cell_type":"code","source":"print('TRAIN SET')\nprint('Rows: %s' % train.shape[0])\nprint('Columns: %s' % train.shape[1])\nprint('Features: %s' % train.columns.values)\nprint()\nprint('TEST SET')\nprint('Rows: %s' % test.shape[0])\nprint('Columns: %s' % test.shape[1])\nprint('Features: %s' % test.columns.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20b293b1025b286daac546389ce7511b434e3ec1"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db7ccfb01e8eabfc930b05f24fd0a51f5b48428"},"cell_type":"code","source":"categorical_features = ['device.isMobile', 'month', 'weekday']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33a1bd33e666ed8db2267dac4cf52de4e0783931"},"cell_type":"code","source":"train = pd.get_dummies(train, columns=categorical_features)\ntest = pd.get_dummies(test, columns=categorical_features)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac9e2b504f84217a7d4d170507e16745145218c2"},"cell_type":"code","source":"train, test = train.align(test, join='outer', axis=1)\n\n# replace the nan values added by align for 0\ntrain.replace(to_replace=np.nan, value=0, inplace=True)\ntest.replace(to_replace=np.nan, value=0, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34419823673c535ebff87ab228fc569aab253b5a"},"cell_type":"code","source":"#creating test and validation set\nX_train = train[train['date']<=datetime.date(2017, 12, 31)]\nX_val = train[train['date']>datetime.date(2017, 12, 31)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c40e136280366c19464d660be4d860429c7c88ab"},"cell_type":"code","source":"# Get labels\nY_train = X_train['totals.transactionRevenue'].values\nY_val = X_val['totals.transactionRevenue'].values\nX_train = X_train.drop(['totals.transactionRevenue'], axis=1)\nX_val = X_val.drop(['totals.transactionRevenue'], axis=1)\ntest = test.drop(['totals.transactionRevenue'], axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d4ff4b30d74fe18df0b84d4389358ce5d2923e7"},"cell_type":"code","source":"a = ['channelGrouping', 'date', 'device.browser', 'device.deviceCategory',\n       'device.operatingSystem', 'fullVisitorId', 'geoNetwork.city',\n       'geoNetwork.continent', 'geoNetwork.country', 'geoNetwork.metro',\n       'geoNetwork.networkDomain', 'geoNetwork.region',\n       'geoNetwork.subContinent', 'totals.sessionQualityDim',\n       'totals.timeOnSite', 'totals.totalTransactionRevenue',\n       'totals.transactions', 'trafficSource.adContent',\n       'trafficSource.adwordsClickInfo.adNetworkType',\n       'trafficSource.adwordsClickInfo.gclId',\n       'trafficSource.adwordsClickInfo.page',\n       'trafficSource.adwordsClickInfo.slot', 'trafficSource.campaign',\n       'trafficSource.keyword', 'trafficSource.medium',\n       'trafficSource.referralPath', 'trafficSource.source', 'visitId','visitStartTime']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c24e242097bdc8144cc954fead0496ecee52227"},"cell_type":"code","source":"reduce_features = a\nX_train = X_train.drop(reduce_features, axis=1)\nX_val = X_val.drop(reduce_features, axis=1)\ntest = test.drop(reduce_features, axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3984a3775f5c0268ad83eb71d67cf1321cd5a5fa"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"856691278d79ec6e14b758d3fca6fb3ee7da4ebf"},"cell_type":"code","source":"normalized_features = ['visitNumber', 'totals.hits', 'totals.pageviews', \n                       'mean_hits_per_day', 'mean_pageviews_per_day', \n                       'sum_hits_per_day', 'sum_pageviews_per_day']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b71dd0a60e9047209ae68bf769dd12906acd6f47"},"cell_type":"code","source":"# Normalize using Min-Max scaling\nscaler = preprocessing.MinMaxScaler()\nX_train[normalized_features] = scaler.fit_transform(X_train[normalized_features])\nX_val[normalized_features] = scaler.transform(X_val[normalized_features])\ntest[normalized_features] = scaler.transform(test[normalized_features])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67e11dc4971c8a1a5914fed290798d42766fb04b"},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true,"_uuid":"923f8c256c5a5fa8e2175e0d08bf54d0095cb6a5"},"cell_type":"code","source":"print(X_train.dtypes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c20c245d7f56351be6d458aed37bb03d2749489"},"cell_type":"code","source":"Y_train = np.log1p(Y_train)\nY_val = np.log1p(Y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db73a644f3b09cbd6e2b6175738d21bee48e004d"},"cell_type":"code","source":"Y_train.max()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db552e86d7998b4c7b73e15565ad0eb14a2c1737"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8563b532fbe9a3f85d40d63f2024cb74f8ee2db8"},"cell_type":"code","source":"# model random forest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8698fe8a7ee484fa21abd53d8e57fdcbac35ada9"},"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\nfrom math import sqrt\n\nx = X_train\ny = Y_train\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d67c161e6f7a48e1709ab968bd7e35cfba0396fe"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nmodel = RandomForestRegressor(criterion='mse')\nmodel.fit(x, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51aa84e5b6ce763d8b095e3366799fc332f591d1"},"cell_type":"code","source":"predictions = model.predict(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"490cbb0e47feaa58419e8e91f1ef01da3f2f70c1"},"cell_type":"code","source":"\nms = mean_squared_error(Y_val, predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40b12b8e8c3925e7305db9fe781b1cdc3076bd42"},"cell_type":"code","source":"\nprint(ms)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab2f3fecdcebc22e13bf4902756179dbe7427da7"},"cell_type":"code","source":"from math import sqrt\n\nrms = sqrt(ms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fc8e4e966bc71b20a85f6bfdde2ca3cc7fe07b1"},"cell_type":"code","source":"print(rms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c695066cd0edaf9b0a311ec7a424cc6f58d4c7d"},"cell_type":"code","source":"# model for ada boost","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c2c5b756fc4716ff40d8957de86c47aabe0f681"},"cell_type":"code","source":"from sklearn.ensemble import AdaBoostRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0eacaf7775b6af88b4b4fd392e64986206bc66f"},"cell_type":"code","source":"model2 = AdaBoostRegressor(n_estimators=300)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"011547f8afed6cebff8c5071f55858a74ae8970d"},"cell_type":"code","source":"model2.fit(X_train, Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7312db3c93b231746847c2519f213852334bbf2"},"cell_type":"code","source":"predictions2 = model2.predict(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"57b7bd38ab04b8c24e12de4a732d227256e298f4"},"cell_type":"code","source":"ms2 = mean_squared_error(Y_val, predictions2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac7273656ffb98b0f304bc2521a58546cc175db3"},"cell_type":"code","source":"print(ms2)\nprint(sqrt(ms2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2931ba6cc7bdaaf1c295b0c7085360931792d843"},"cell_type":"code","source":"# model for linear regression","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5676f2acbc429690310cd685efb522c2b303cb78"},"cell_type":"code","source":"from sklearn.linear_model import LinearRegression","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9854c997cf9bf30b8ad1dcf4c0e0bddf3f2f32ce"},"cell_type":"code","source":"model3 = LinearRegression()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47d11b86feccc7b64930c59546e289a060ad43bc"},"cell_type":"code","source":"model3.fit(X_train, Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70e40d3da3100630845f82fcb203df2697173e6c"},"cell_type":"code","source":"predictions3 = model3.predict(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b15713a06a04b1be4bd893e2d2edafd932c84ced"},"cell_type":"code","source":"ms3 = mean_squared_error(Y_val, predictions3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"679768f4e378f103c19e00541349fc7f4a5974e0"},"cell_type":"code","source":"print(ms3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe033d7d170b70070b20a459be400919fd7e3b50"},"cell_type":"code","source":"from math import sqrt\nprint(sqrt(ms3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab88af24b21ce481317858439b6e20dd6adb1b63"},"cell_type":"code","source":"# model for decsision trees\nfrom sklearn.tree import DecisionTreeRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee1300edff6856a77f31035684a2688b916e0c39"},"cell_type":"code","source":"model4 = DecisionTreeRegressor()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2002e5d020d1ed38c9b24409a9cf75240801de0"},"cell_type":"code","source":"model4.fit(X_train, Y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b2185c85e31d3bcb327c1dc91c851169ee990d4"},"cell_type":"code","source":"predictions4 = model4.predict(X_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"378c6d82003fa1f7a79991c0935617b39b886d7e"},"cell_type":"code","source":"ms4 = mean_squared_error(Y_val, predictions4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd82947f1bab1de8dd30f4199e7b609e44c0a7ff"},"cell_type":"code","source":"print(ms4)\nprint(sqrt(ms4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20f597f93e30246b1b82d53b7e3b6083f66ab57d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"d17b133b31e077215acc3ce3e93682b69875eb9f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"5a8a30e12ae655ba6a033fdc5d613a59660caeca"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"267b4fbf5da9b1f6efbd1b434adedb629d890ad1"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e19019109bfa429b19a61cf14643aebb534afcd5"},"cell_type":"code","source":"# MODEL DEEP LEARNING ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c46b52d04be1fac08655fed58417dc45af7d0fed"},"cell_type":"code","source":"BATCH_SIZE = 128\nEPOCHS = 50\nLEARNING_RATE = 0.0003","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54bb1741218f25f52f7cbb60ad87b182eb54a861"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(256, kernel_initializer='glorot_normal', activation='relu', input_dim=X_train.shape[1]))\nmodel.add(Dense(128, kernel_initializer='glorot_normal', activation='relu'))\nmodel.add(Dense(1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39c81bb985b6494a2217b6cd2cca286bcf641689"},"cell_type":"code","source":"adam = optimizers.adam(lr=LEARNING_RATE)\nmodel.compile(loss='mse', optimizer=adam)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d0fd31a73f0f89281756449de54ada4bff58b99"},"cell_type":"code","source":"print('Dataset size: %s' % X_train.shape[0])\nprint('Epochs: %s' % EPOCHS)\nprint('Learning rate: %s' % LEARNING_RATE)\nprint('Batch size: %s' % BATCH_SIZE)\nprint('Input dimension: %s' % X_train.shape[1])\nprint('Features used: %s' % X_train.columns.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7741f6b90240af1375797288ff739d80cd26ce2e"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71911df7e6512ffe3079d740012b3668257c422c"},"cell_type":"code","source":"history = model.fit(x=X_train.values, y=Y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, \n                    verbose=1, validation_data=(X_val.values, Y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c49aaf7676d976276d68005a3559fc0cdae72fc7"},"cell_type":"code","source":"val_predictions = model.predict(X_val)\nmse = mean_squared_error(val_predictions, Y_val)\nrmse = np.sqrt(mean_squared_error(val_predictions, Y_val))\n\nprint('Model validation metrics')\nprint('MSE: %.2f' % mse)\nprint('RMSE: %.2f' % rmse)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b25f94f04d6d6cafe35e7666185b1751c862ede","scrolled":true},"cell_type":"code","source":"\n\nplot_metrics(history.history['loss'], history.history['val_loss'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53ef69ada3a6995e3e7761b0816050105d92f99e"},"cell_type":"code","source":"plot(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"266dbed88d1d5ebb8b1300751c93df90f6cfcc1d"},"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}