{"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-08T22:11:02.780430Z","iopub.execute_input":"2023-01-08T22:11:02.781240Z","iopub.status.idle":"2023-01-08T22:11:02.789547Z","shell.execute_reply.started":"2023-01-08T22:11:02.781207Z","shell.execute_reply":"2023-01-08T22:11:02.788435Z"},"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-08T22:11:02.804807Z","iopub.execute_input":"2023-01-08T22:11:02.805681Z","iopub.status.idle":"2023-01-08T22:11:08.387966Z","shell.execute_reply.started":"2023-01-08T22:11:02.805648Z","shell.execute_reply":"2023-01-08T22:11:08.386697Z"},"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-08T22:11:08.389952Z","iopub.execute_input":"2023-01-08T22:11:08.390322Z","iopub.status.idle":"2023-01-08T22:11:08.417695Z","shell.execute_reply.started":"2023-01-08T22:11:08.390286Z","shell.execute_reply":"2023-01-08T22:11:08.416808Z"},"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-08T22:11:08.419080Z","iopub.execute_input":"2023-01-08T22:11:08.419424Z","iopub.status.idle":"2023-01-08T22:11:08.427625Z","shell.execute_reply.started":"2023-01-08T22:11:08.419392Z","shell.execute_reply":"2023-01-08T22:11:08.426590Z"},"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-08T22:11:08.430387Z","iopub.execute_input":"2023-01-08T22:11:08.430786Z","iopub.status.idle":"2023-01-08T22:11:08.543144Z","shell.execute_reply.started":"2023-01-08T22:11:08.430722Z","shell.execute_reply":"2023-01-08T22:11:08.542133Z"},"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-08T22:11:08.544434Z","iopub.execute_input":"2023-01-08T22:11:08.544726Z","iopub.status.idle":"2023-01-08T22:11:08.631523Z","shell.execute_reply.started":"2023-01-08T22:11:08.544698Z","shell.execute_reply":"2023-01-08T22:11:08.630559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = train_df.drop(columns = ['id', 'target'])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:11:08.632728Z","iopub.execute_input":"2023-01-08T22:11:08.633198Z","iopub.status.idle":"2023-01-08T22:11:08.685314Z","shell.execute_reply.started":"2023-01-08T22:11:08.633172Z","shell.execute_reply":"2023-01-08T22:11:08.684415Z"},"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-08T22:11:08.686389Z","iopub.execute_input":"2023-01-08T22:11:08.686636Z","iopub.status.idle":"2023-01-08T22:11:09.173147Z","shell.execute_reply.started":"2023-01-08T22:11:08.686613Z","shell.execute_reply":"2023-01-08T22:11:09.172177Z"},"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-08T22:11:09.174141Z","iopub.execute_input":"2023-01-08T22:11:09.174563Z","iopub.status.idle":"2023-01-08T22:11:10.247449Z","shell.execute_reply.started":"2023-01-08T22:11:09.174536Z","shell.execute_reply":"2023-01-08T22:11:10.246558Z"},"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-08T22:11:10.248779Z","iopub.execute_input":"2023-01-08T22:11:10.249544Z","iopub.status.idle":"2023-01-08T22:11:13.900886Z","shell.execute_reply.started":"2023-01-08T22:11:10.249516Z","shell.execute_reply":"2023-01-08T22:11:13.899805Z"},"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-08T22:11:13.904991Z","iopub.execute_input":"2023-01-08T22:11:13.905254Z","iopub.status.idle":"2023-01-08T22:11:19.519295Z","shell.execute_reply.started":"2023-01-08T22:11:13.905230Z","shell.execute_reply":"2023-01-08T22:11:19.518534Z"},"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-08T22:11:19.520675Z","iopub.execute_input":"2023-01-08T22:11:19.521184Z","iopub.status.idle":"2023-01-08T22:11:20.293958Z","shell.execute_reply.started":"2023-01-08T22:11:19.521149Z","shell.execute_reply":"2023-01-08T22:11:20.292935Z"},"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-08T22:11:20.295444Z","iopub.execute_input":"2023-01-08T22:11:20.296176Z","iopub.status.idle":"2023-01-08T22:11:20.338662Z","shell.execute_reply.started":"2023-01-08T22:11:20.296142Z","shell.execute_reply":"2023-01-08T22:11:20.337926Z"},"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-08T22:11:20.339650Z","iopub.execute_input":"2023-01-08T22:11:20.339939Z","iopub.status.idle":"2023-01-08T22:11:23.358967Z","shell.execute_reply.started":"2023-01-08T22:11:20.339910Z","shell.execute_reply":"2023-01-08T22:11:23.357954Z"},"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-08T22:11:23.360465Z","iopub.execute_input":"2023-01-08T22:11:23.361123Z","iopub.status.idle":"2023-01-08T22:11:23.365654Z","shell.execute_reply.started":"2023-01-08T22:11:23.361083Z","shell.execute_reply":"2023-01-08T22:11:23.364785Z"},"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-08T22:11:23.368067Z","iopub.execute_input":"2023-01-08T22:11:23.369051Z","iopub.status.idle":"2023-01-08T22:11:23.403122Z","shell.execute_reply.started":"2023-01-08T22:11:23.369018Z","shell.execute_reply":"2023-01-08T22:11:23.401951Z"},"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-08T22:11:23.404245Z","iopub.execute_input":"2023-01-08T22:11:23.404838Z","iopub.status.idle":"2023-01-08T22:11:42.980874Z","shell.execute_reply.started":"2023-01-08T22:11:23.404799Z","shell.execute_reply":"2023-01-08T22:11:42.980128Z"},"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-08T22:11:42.983971Z","iopub.execute_input":"2023-01-08T22:11:42.984620Z","iopub.status.idle":"2023-01-08T22:11:43.456591Z","shell.execute_reply.started":"2023-01-08T22:11:42.984589Z","shell.execute_reply":"2023-01-08T22:11:43.455735Z"},"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-08T22:11:43.457882Z","iopub.execute_input":"2023-01-08T22:11:43.458374Z","iopub.status.idle":"2023-01-08T22:11:57.883088Z","shell.execute_reply.started":"2023-01-08T22:11:43.458343Z","shell.execute_reply":"2023-01-08T22:11:57.882229Z"},"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-08T22:11:57.884372Z","iopub.execute_input":"2023-01-08T22:11:57.884887Z","iopub.status.idle":"2023-01-08T22:11:57.963002Z","shell.execute_reply.started":"2023-01-08T22:11:57.884854Z","shell.execute_reply":"2023-01-08T22:11:57.962136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lr.predict(test_x.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:11:57.964376Z","iopub.execute_input":"2023-01-08T22:11:57.964917Z","iopub.status.idle":"2023-01-08T22:11:58.009579Z","shell.execute_reply.started":"2023-01-08T22:11:57.964885Z","shell.execute_reply":"2023-01-08T22:11:58.008806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:11:58.010793Z","iopub.execute_input":"2023-01-08T22:11:58.011282Z","iopub.status.idle":"2023-01-08T22:11:58.020532Z","shell.execute_reply.started":"2023-01-08T22:11:58.011251Z","shell.execute_reply":"2023-01-08T22:11:58.019800Z"},"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-08T22:11:58.021799Z","iopub.execute_input":"2023-01-08T22:11:58.022309Z","iopub.status.idle":"2023-01-08T22:11:58.030149Z","shell.execute_reply.started":"2023-01-08T22:11:58.022276Z","shell.execute_reply":"2023-01-08T22:11:58.029179Z"},"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-08T22:11:58.031165Z","iopub.execute_input":"2023-01-08T22:11:58.031478Z","iopub.status.idle":"2023-01-08T22:11:58.085415Z","shell.execute_reply.started":"2023-01-08T22:11:58.031447Z","shell.execute_reply":"2023-01-08T22:11:58.084619Z"},"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-08T22:11:58.086471Z","iopub.execute_input":"2023-01-08T22:11:58.086951Z","iopub.status.idle":"2023-01-08T22:20:20.091588Z","shell.execute_reply.started":"2023-01-08T22:11:58.086920Z","shell.execute_reply":"2023-01-08T22:20:20.090793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_list = list(train_features.columns)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:20:20.092628Z","iopub.execute_input":"2023-01-08T22:20:20.092913Z","iopub.status.idle":"2023-01-08T22:20:20.096698Z","shell.execute_reply.started":"2023-01-08T22:20:20.092890Z","shell.execute_reply":"2023-01-08T22:20:20.096079Z"},"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-08T22:20:20.097679Z","iopub.execute_input":"2023-01-08T22:20:20.098409Z","iopub.status.idle":"2023-01-08T22:20:20.214113Z","shell.execute_reply.started":"2023-01-08T22:20:20.098384Z","shell.execute_reply":"2023-01-08T22:20:20.213254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_importances[1][0]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:20:20.224463Z","iopub.execute_input":"2023-01-08T22:20:20.224894Z","iopub.status.idle":"2023-01-08T22:20:20.237674Z","shell.execute_reply.started":"2023-01-08T22:20:20.224869Z","shell.execute_reply":"2023-01-08T22:20:20.237077Z"},"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-08T22:20:20.247438Z","iopub.execute_input":"2023-01-08T22:20:20.248172Z","iopub.status.idle":"2023-01-08T22:20:20.255366Z","shell.execute_reply.started":"2023-01-08T22:20:20.248146Z","shell.execute_reply":"2023-01-08T22:20:20.254494Z"},"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-08T22:20:20.256402Z","iopub.execute_input":"2023-01-08T22:20:20.256648Z","iopub.status.idle":"2023-01-08T22:20:20.268935Z","shell.execute_reply.started":"2023-01-08T22:20:20.256624Z","shell.execute_reply":"2023-01-08T22:20:20.268087Z"},"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-08T22:20:20.270258Z","iopub.execute_input":"2023-01-08T22:20:20.270703Z","iopub.status.idle":"2023-01-08T22:23:37.202427Z","shell.execute_reply.started":"2023-01-08T22:20:20.270678Z","shell.execute_reply":"2023-01-08T22:23:37.201442Z"},"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-08T22:23:37.203664Z","iopub.execute_input":"2023-01-08T22:23:37.203979Z","iopub.status.idle":"2023-01-08T22:23:38.018240Z","shell.execute_reply.started":"2023-01-08T22:23:37.203955Z","shell.execute_reply":"2023-01-08T22:23:38.017323Z"},"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-08T22:23:38.019658Z","iopub.execute_input":"2023-01-08T22:23:38.020090Z","iopub.status.idle":"2023-01-08T22:23:38.027781Z","shell.execute_reply.started":"2023-01-08T22:23:38.020058Z","shell.execute_reply":"2023-01-08T22:23:38.026986Z"},"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-08T22:23:38.028878Z","iopub.execute_input":"2023-01-08T22:23:38.029135Z","iopub.status.idle":"2023-01-08T22:23:38.045119Z","shell.execute_reply.started":"2023-01-08T22:23:38.029112Z","shell.execute_reply":"2023-01-08T22:23:38.044274Z"},"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-08T22:23:38.046458Z","iopub.execute_input":"2023-01-08T22:23:38.047053Z","iopub.status.idle":"2023-01-08T22:23:40.574071Z","shell.execute_reply.started":"2023-01-08T22:23:38.047015Z","shell.execute_reply":"2023-01-08T22:23:40.573286Z"},"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-08T22:25:43.545291Z","iopub.execute_input":"2023-01-08T22:25:43.545827Z","iopub.status.idle":"2023-01-08T22:25:44.711936Z","shell.execute_reply.started":"2023-01-08T22:25:43.545797Z","shell.execute_reply":"2023-01-08T22:25:44.711229Z"},"trusted":true},"execution_count":null,"outputs":[]}]}