{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-07T23:19:03.709516Z","iopub.execute_input":"2023-01-07T23:19:03.709943Z","iopub.status.idle":"2023-01-07T23:19:04.318990Z","shell.execute_reply.started":"2023-01-07T23:19:03.709910Z","shell.execute_reply":"2023-01-07T23:19:04.317759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')\ntest_df = pd.read_csv('/kaggle/input/emsi-5iir-ds/test.csv')\nsubm = pd.read_csv('/kaggle/input/emsi-5iir-ds/sample_submission.csv')\n\nprint(\"The Training dataset is made of {} rows and {} columns.\".format(len(train_df), len(train_df.columns)))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:20:11.864008Z","iopub.execute_input":"2023-01-07T23:20:11.864399Z","iopub.status.idle":"2023-01-07T23:20:27.568087Z","shell.execute_reply.started":"2023-01-07T23:20:11.864370Z","shell.execute_reply":"2023-01-07T23:20:27.566862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns = train_df.shape[1]\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:20:33.113749Z","iopub.execute_input":"2023-01-07T23:20:33.114917Z","iopub.status.idle":"2023-01-07T23:20:33.151372Z","shell.execute_reply.started":"2023-01-07T23:20:33.114869Z","shell.execute_reply":"2023-01-07T23:20:33.150205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = train_df.dtypes\n\nfor elem in range(len(columns.index)):\n    print(\"- {}: type {} \\n\".format(columns.index[elem], columns.values[elem]))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:21:02.317807Z","iopub.execute_input":"2023-01-07T23:21:02.318432Z","iopub.status.idle":"2023-01-07T23:21:02.327499Z","shell.execute_reply.started":"2023-01-07T23:21:02.318394Z","shell.execute_reply":"2023-01-07T23:21:02.326049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counting = train_df['target'].value_counts()\nlbl = []\nfor elem in counting.index:\n    lbl.append('Target {}'.format(counting.index.values[elem]))\n\nplt.figure(figsize=(15,8))\nfont = {'family' : 'serif',\n        'weight' : 'bold',\n        'size'   : 12}\n\nmatplotlib.rc('font', **font)\n\ncolors = sns.color_palette(\"husl\", 2)\nplt.pie(counting, labels = lbl, colors = colors, autopct='%.0f%%', explode=(0, 0.1),\n        shadow=True, startangle=90\n       )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:21:23.331289Z","iopub.execute_input":"2023-01-07T23:21:23.332534Z","iopub.status.idle":"2023-01-07T23:21:23.545308Z","shell.execute_reply.started":"2023-01-07T23:21:23.332488Z","shell.execute_reply":"2023-01-07T23:21:23.543805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The number of missing values in the training set is equal to: {}.\".format(train_df.isnull().sum().sum()))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:21:36.926463Z","iopub.execute_input":"2023-01-07T23:21:36.926853Z","iopub.status.idle":"2023-01-07T23:21:37.021081Z","shell.execute_reply.started":"2023-01-07T23:21:36.926822Z","shell.execute_reply":"2023-01-07T23:21:37.019981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = train_df.drop(columns = ['id', 'target'])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:21:53.563248Z","iopub.execute_input":"2023-01-07T23:21:53.564201Z","iopub.status.idle":"2023-01-07T23:21:53.692133Z","shell.execute_reply.started":"2023-01-07T23:21:53.564166Z","shell.execute_reply":"2023-01-07T23:21:53.691049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_float = train_df.select_dtypes('float64')\nx_float.describe()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:22:03.490870Z","iopub.execute_input":"2023-01-07T23:22:03.491255Z","iopub.status.idle":"2023-01-07T23:22:04.254454Z","shell.execute_reply.started":"2023-01-07T23:22:03.491226Z","shell.execute_reply":"2023-01-07T23:22:04.253179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.color_palette(\"husl\", 8)\nplt.figure(figsize=(15,8))\nax = sns.boxplot(data=x_float, orient=\"h\")","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:22:14.852775Z","iopub.execute_input":"2023-01-07T23:22:14.854102Z","iopub.status.idle":"2023-01-07T23:22:16.305435Z","shell.execute_reply.started":"2023-01-07T23:22:14.854051Z","shell.execute_reply":"2023-01-07T23:22:16.302840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfloat_and_tgt=pd.concat([x_float,train_df['target']], axis=1)\ntitles=['Feature {}'.format(i.split('_')[-1]) for i in x_float]\nfig, ax = plt.subplots(4,4, figsize=(14,24))\nrow=0\ncol=[0,1,2,3]*4\nfor i, column in enumerate(float_and_tgt.columns[:-1]):\n    if (i!=0) & (i%4==0):\n        row+=1\n    color='#2CB4CF'\n    rgb=matplotlib.colors.to_rgba(color,0.2)\n    ax[row,col[i]].boxplot(float_and_tgt[float_and_tgt['target']==0][column], positions=[0],\n                           widths=0.7, patch_artist=True,\n                           boxprops=dict(color=color, facecolor=rgb, linewidth=1.5))\n    color='#EAB4DE'\n    rgb=matplotlib.colors.to_rgba(color,0.2)\n    ax[row,col[i]].boxplot(float_and_tgt[float_and_tgt['target']==1][column], positions=[1],\n                           widths=0.7, patch_artist=True,\n                           boxprops=dict(color=color, facecolor=rgb, linewidth=1.5))\n    ax[row,col[i]].grid(visible=True, which='major', axis='y', color='#F2F2F2')\n    ax[row,col[i]].tick_params(left=False,bottom=False)\n    ax[row,col[i]].set_title('\\n\\n{}'.format(titles[i]))\nsns.despine(bottom=True, trim=True)\nplt.suptitle('Distributions of Numerical Variables',fontsize=16)\nplt.tight_layout(rect=[0, 0.2, 1, 0.99])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:22:41.624662Z","iopub.execute_input":"2023-01-07T23:22:41.625152Z","iopub.status.idle":"2023-01-07T23:22:46.209503Z","shell.execute_reply.started":"2023-01-07T23:22:41.625095Z","shell.execute_reply":"2023-01-07T23:22:46.208408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"float_and_tgt=pd.concat([x_float,train_df['target']], axis=1)\ntitles=['Feature {}'.format(i.split('_')[-1]) for i in x_float]\nfig, ax = plt.subplots(4,4, figsize=(14,24))\nrow=0\ncol=[0,1,2,3]*4\nfor i, column in enumerate(float_and_tgt.columns[:-1]):\n    if (i!=0) & (i%4==0):\n        row+=1\n    color='#2CB4CF'\n    rgb=matplotlib.colors.to_rgba(color,0.3)\n    ax[row,col[i]].hist(float_and_tgt[float_and_tgt['target']==0][column],\n                        color=rgb, density=True, bins=40)\n    color='#EAB4DE'\n    rgb=matplotlib.colors.to_rgba(color,0.3)\n    ax[row,col[i]].hist(float_and_tgt[float_and_tgt['target']==1][column],\n                       color=rgb, density=True, bins=40)\n    #ax[row,col[i]].grid(visible=True, which='major', axis='y', color='#F2F2F2')\n    ax[row,col[i]].tick_params(left=False,bottom=False)\n    ax[row,col[i]].set_title('\\n\\n{}'.format(titles[i]))\nsns.despine(bottom=True, trim=True)\nplt.suptitle('Distributions of Numerical Variables',fontsize=16)\nplt.tight_layout(rect=[0, 0.2, 1, 0.99])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:23:16.259206Z","iopub.execute_input":"2023-01-07T23:23:16.259683Z","iopub.status.idle":"2023-01-07T23:23:23.336618Z","shell.execute_reply.started":"2023-01-07T23:23:16.259651Z","shell.execute_reply":"2023-01-07T23:23:23.335484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = x_float.corr()\n\n# Generate a mask for the upper triangle\nmask = np.triu(np.ones_like(corr, dtype=bool))\nf, ax = plt.subplots(figsize=(14, 24))\n\n# Generate a custom diverging colormap\ncmap = sns.diverging_palette(145, 300, s=60, as_cmap=True)\n\n# Draw the heatmap with the mask and correct aspect ratio\nsns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,\n            square=True, linewidths=.5, cbar_kws={\"shrink\": .5})","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:23:39.748381Z","iopub.execute_input":"2023-01-07T23:23:39.748793Z","iopub.status.idle":"2023-01-07T23:23:40.936941Z","shell.execute_reply.started":"2023-01-07T23:23:39.748757Z","shell.execute_reply":"2023-01-07T23:23:40.935927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_int = train_df.select_dtypes('int64')\nx_int = x_int.drop('id', axis=1)\n#x_int_tgt = pd.concat([x_int,train_df['target']], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:23:56.424179Z","iopub.execute_input":"2023-01-07T23:23:56.424605Z","iopub.status.idle":"2023-01-07T23:23:56.525084Z","shell.execute_reply.started":"2023-01-07T23:23:56.424570Z","shell.execute_reply":"2023-01-07T23:23:56.523779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_titles=['Feature {}'.format(i.split('_')[-1]) for i in x_int.columns[:-1]]\n\nfig, ax = plt.subplots(4,4, figsize=(14,24))\n\nfor i, f in enumerate(x_int.columns[:-1]):\n    plt.subplot(4, 4, i+1)\n    ax = plt.gca()\n    color='#2CB4CF'\n    rgb=matplotlib.colors.to_rgba(color,0.3)\n    \n    vc_0 = x_int[x_int['target']==0][f].value_counts()\n    ax.bar(vc_0.index, vc_0, color=rgb)\n    \n    color='#EAB4DE'\n    rgb=matplotlib.colors.to_rgba(color,0.3)\n    vc_1 = x_int[x_int['target']==1][f].value_counts()\n    ax.bar(vc_1.index, vc_1, color=rgb)\n    #ax.hist(train[f], density=False, bins=(train[f].max()-train[f].min()+1))\n    #ax.set_xlabel(f'Feature {f}')\n    ax.set_title('\\n\\n{}'.format(sub_titles[i]))\n    #ax.xaxis.set_major_locator(MaxNLocator(integer=True)) # only integer labels\nsns.despine(bottom=True, trim=True)\nplt.suptitle('Distributions of Categorical Variables',fontsize=16)\nplt.tight_layout(rect=[0, 0.2, 1, 0.99])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:24:16.917740Z","iopub.execute_input":"2023-01-07T23:24:16.918112Z","iopub.status.idle":"2023-01-07T23:24:20.670300Z","shell.execute_reply.started":"2023-01-07T23:24:16.918083Z","shell.execute_reply":"2023-01-07T23:24:20.669207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection  import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:24:37.743165Z","iopub.execute_input":"2023-01-07T23:24:37.743612Z","iopub.status.idle":"2023-01-07T23:24:37.748408Z","shell.execute_reply.started":"2023-01-07T23:24:37.743576Z","shell.execute_reply":"2023-01-07T23:24:37.747302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_features = pd.concat([x_float,x_int], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:24:46.709000Z","iopub.execute_input":"2023-01-07T23:24:46.710182Z","iopub.status.idle":"2023-01-07T23:24:46.819737Z","shell.execute_reply.started":"2023-01-07T23:24:46.710141Z","shell.execute_reply":"2023-01-07T23:24:46.818544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x, cv_x, y_train, y_cv  = train_test_split(x_features,train_df['target'],\n                                                 stratify=train_df['target'])\n\ntrain_x.drop(['target'],axis=1,inplace=True)\ncv_x.drop([\"target\"],axis=1,inplace=True)\n\nlr = LogisticRegression(max_iter=500)\nlr.fit(train_x.values, y_train.values)\npred = lr.predict(train_x.values)\nprint(\"The train accuracy of the Logistic Regression is \",accuracy_score(y_train.values,pred))\npred  = lr.predict(cv_x.values)\nprint(\"The cv accuracy of the Logistic Regression is \",accuracy_score(y_cv.values, pred))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:25:04.114599Z","iopub.execute_input":"2023-01-07T23:25:04.115066Z","iopub.status.idle":"2023-01-07T23:25:22.708225Z","shell.execute_reply.started":"2023-01-07T23:25:04.115030Z","shell.execute_reply":"2023-01-07T23:25:22.703874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import stats\nx_float_no = x_float[(np.abs(stats.zscore(x_float)) < 3).all(axis=1)]\nindex_list_no = x_float_no.index.to_list()\nx_int_no = x_int.iloc[index_list_no]\ntarget_no = x_int_no['target']\nx_int_no.drop('target', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:27:13.415615Z","iopub.execute_input":"2023-01-07T23:27:13.416003Z","iopub.status.idle":"2023-01-07T23:27:14.138819Z","shell.execute_reply.started":"2023-01-07T23:27:13.415974Z","shell.execute_reply":"2023-01-07T23:27:14.137873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_features_no = pd.concat([x_float_no,x_int_no], axis=1)\n\ntrain_x, cv_x, y_train, y_cv  = train_test_split(x_features_no,target_no,\n                                                 stratify=target_no)\n\n#train_x.drop(['target'],axis=1,inplace=True)\n#cv_x.drop([\"target\"],axis=1,inplace=True)\n\nlr = LogisticRegression(max_iter=500)\nlr.fit(train_x.values, y_train.values)\npred = lr.predict(train_x.values)\nprint(\"The train accuracy of the Logistic Regression without outliers is \",accuracy_score(y_train.values,pred))\npred  = lr.predict(cv_x.values)\nprint(\"The cv accuracy of the Logistic Regression without outliers is \",accuracy_score(y_cv.values, pred))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:27:42.596379Z","iopub.execute_input":"2023-01-07T23:27:42.596780Z","iopub.status.idle":"2023-01-07T23:28:41.119531Z","shell.execute_reply.started":"2023-01-07T23:27:42.596745Z","shell.execute_reply":"2023-01-07T23:28:41.117913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x = test_df.select_dtypes([\"int\",\"float\"])\ntest_id = test_x['id'].values\ntest_x.drop(\"id\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:34:14.900314Z","iopub.execute_input":"2023-01-07T23:34:14.900723Z","iopub.status.idle":"2023-01-07T23:34:15.034129Z","shell.execute_reply.started":"2023-01-07T23:34:14.900674Z","shell.execute_reply":"2023-01-07T23:34:15.032938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lr.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:34:32.463761Z","iopub.execute_input":"2023-01-07T23:34:32.464137Z","iopub.status.idle":"2023-01-07T23:34:32.556487Z","shell.execute_reply.started":"2023-01-07T23:34:32.464108Z","shell.execute_reply":"2023-01-07T23:34:32.554645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:34:48.974857Z","iopub.execute_input":"2023-01-07T23:34:48.975251Z","iopub.status.idle":"2023-01-07T23:34:48.985858Z","shell.execute_reply.started":"2023-01-07T23:34:48.975221Z","shell.execute_reply":"2023-01-07T23:34:48.984800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x_features_no.drop('target', axis=1, inplace=True)\nfrom sklearn.ensemble import RandomForestRegressor, RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:37:03.259867Z","iopub.execute_input":"2023-01-07T23:37:03.260300Z","iopub.status.idle":"2023-01-07T23:37:03.265524Z","shell.execute_reply.started":"2023-01-07T23:37:03.260258Z","shell.execute_reply":"2023-01-07T23:37:03.264390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_features_notgt = x_features.drop('target', axis=1)\ntarget = x_features['target']","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:37:33.145675Z","iopub.execute_input":"2023-01-07T23:37:33.146077Z","iopub.status.idle":"2023-01-07T23:37:33.235231Z","shell.execute_reply.started":"2023-01-07T23:37:33.146047Z","shell.execute_reply":"2023-01-07T23:37:33.234219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features, test_features, train_labels, test_labels = train_test_split(x_features_no,target_no, test_size = 0.25, random_state = 42)\n\nrf = RandomForestRegressor(n_estimators = 100,\n                            min_samples_leaf = 5,\n                            max_depth = 15,\n                            n_jobs = -1,\n                            random_state = 42)\nrf.fit(train_features, train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:47:54.508054Z","iopub.execute_input":"2023-01-07T23:47:54.508520Z","iopub.status.idle":"2023-01-07T23:58:49.044050Z","shell.execute_reply.started":"2023-01-07T23:47:54.508481Z","shell.execute_reply":"2023-01-07T23:58:49.042860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_list = list(train_features.columns)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:59:24.459961Z","iopub.execute_input":"2023-01-07T23:59:24.460367Z","iopub.status.idle":"2023-01-07T23:59:24.465167Z","shell.execute_reply.started":"2023-01-07T23:59:24.460335Z","shell.execute_reply":"2023-01-07T23:59:24.464239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get numerical feature importances\nimportances = list(rf.feature_importances_)\n# List of tuples with variable and importance\nfeature_importances = [(feature, round(importance, 2)) for feature, importance in zip(feature_list, importances)]\n# Sort the feature importances by most important first\nfeature_importances = sorted(feature_importances, key = lambda x: x[1], reverse = True)\n# Print out the feature and importances \n[print('Variable: {} Importance: {}'.format(*pair)) for pair in feature_importances]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T23:59:35.347901Z","iopub.execute_input":"2023-01-07T23:59:35.348310Z","iopub.status.idle":"2023-01-07T23:59:35.461362Z","shell.execute_reply.started":"2023-01-07T23:59:35.348276Z","shell.execute_reply":"2023-01-07T23:59:35.460278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_importances[1][0]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:00:02.757115Z","iopub.execute_input":"2023-01-08T00:00:02.757596Z","iopub.status.idle":"2023-01-08T00:00:02.765952Z","shell.execute_reply.started":"2023-01-08T00:00:02.757559Z","shell.execute_reply":"2023-01-08T00:00:02.764727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_8_features = []\nfor row in range(8):\n    top_8_features.append(feature_importances[row][0])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:00:38.645073Z","iopub.execute_input":"2023-01-08T00:00:38.645457Z","iopub.status.idle":"2023-01-08T00:00:38.650880Z","shell.execute_reply.started":"2023-01-08T00:00:38.645428Z","shell.execute_reply":"2023-01-08T00:00:38.649725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_top_8 = x_features_no[top_8_features]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:00:49.841349Z","iopub.execute_input":"2023-01-08T00:00:49.841751Z","iopub.status.idle":"2023-01-08T00:00:49.860192Z","shell.execute_reply.started":"2023-01-08T00:00:49.841707Z","shell.execute_reply":"2023-01-08T00:00:49.859048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features, test_features, train_labels, test_labels = train_test_split(x_top_8,target_no, test_size = 0.25, random_state = 42)\n\nrf_top8 = RandomForestRegressor(n_estimators = 100,\n                            min_samples_leaf = 5,\n                            max_depth = 15,\n                            n_jobs = -1,\n                            random_state = 42)\nrf_top8.fit(train_features, train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:01:21.026292Z","iopub.execute_input":"2023-01-08T00:01:21.026741Z","iopub.status.idle":"2023-01-08T00:05:45.376267Z","shell.execute_reply.started":"2023-01-08T00:01:21.026705Z","shell.execute_reply":"2023-01-08T00:05:45.374880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the forest's predict method on the test data\npredictions = rf_top8.predict(test_features)\n# Calculate the absolute errors\nerrors = abs(predictions - test_labels)\n# Print out the mean absolute error (mae)\nprint('Mean Absolute Error:', round(np.mean(errors), 2), 'degrees.')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:18:57.286181Z","iopub.execute_input":"2023-01-08T00:18:57.286610Z","iopub.status.idle":"2023-01-08T00:18:58.509569Z","shell.execute_reply.started":"2023-01-08T00:18:57.286575Z","shell.execute_reply":"2023-01-08T00:18:58.508431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate mean absolute percentage error (MAPE)\nmape = 100 * (errors / test_labels)\n# Calculate and display accuracy\naccuracy = 100 - np.mean(mape)\nprint('Accuracy:', round(accuracy, 2), '%.')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:19:09.117411Z","iopub.execute_input":"2023-01-08T00:19:09.117865Z","iopub.status.idle":"2023-01-08T00:19:09.127602Z","shell.execute_reply.started":"2023-01-08T00:19:09.117826Z","shell.execute_reply":"2023-01-08T00:19:09.126283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x = test_df[top_8_features]\ntest_id = test_df['id'].values\n#test_x.drop(\"id\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:19:59.242906Z","iopub.execute_input":"2023-01-08T00:19:59.243302Z","iopub.status.idle":"2023-01-08T00:19:59.266153Z","shell.execute_reply.started":"2023-01-08T00:19:59.243268Z","shell.execute_reply":"2023-01-08T00:19:59.265047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = rf_top8.predict(test_x.values)\npred","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:20:03.761293Z","iopub.execute_input":"2023-01-08T00:20:03.761728Z","iopub.status.idle":"2023-01-08T00:20:07.602777Z","shell.execute_reply.started":"2023-01-08T00:20:03.761677Z","shell.execute_reply":"2023-01-08T00:20:07.601509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    \"id\" : test_id,\n    \"target\": pred\n})\nsubmission_df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T00:20:24.318786Z","iopub.execute_input":"2023-01-08T00:20:24.319176Z","iopub.status.idle":"2023-01-08T00:20:25.917999Z","shell.execute_reply.started":"2023-01-08T00:20:24.319144Z","shell.execute_reply":"2023-01-08T00:20:25.917053Z"},"trusted":true},"execution_count":null,"outputs":[]}]}