{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84253,"databundleVersionId":9421888,"sourceType":"competition"}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Importing libraries**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_selection import RFE\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.ensemble import RandomForestRegressor\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:03.891359Z","iopub.execute_input":"2024-08-25T06:17:03.891727Z","iopub.status.idle":"2024-08-25T06:17:04.724453Z","shell.execute_reply.started":"2024-08-25T06:17:03.891691Z","shell.execute_reply":"2024-08-25T06:17:04.723582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/first-steps-in-data-science-new/train (7).csv\") \ntest = pd.read_csv(\"/kaggle/input/first-steps-in-data-science-new/test (4).csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:04.726295Z","iopub.execute_input":"2024-08-25T06:17:04.727130Z","iopub.status.idle":"2024-08-25T06:17:07.362034Z","shell.execute_reply.started":"2024-08-25T06:17:04.727082Z","shell.execute_reply":"2024-08-25T06:17:07.361201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Cleaning**","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.363231Z","iopub.execute_input":"2024-08-25T06:17:07.363573Z","iopub.status.idle":"2024-08-25T06:17:07.409240Z","shell.execute_reply.started":"2024-08-25T06:17:07.363541Z","shell.execute_reply":"2024-08-25T06:17:07.408231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train= train.drop(train.columns[0], axis=1)\ntest= test.drop(test.columns[0], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.411445Z","iopub.execute_input":"2024-08-25T06:17:07.411753Z","iopub.status.idle":"2024-08-25T06:17:07.432043Z","shell.execute_reply.started":"2024-08-25T06:17:07.411720Z","shell.execute_reply":"2024-08-25T06:17:07.431311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.433093Z","iopub.execute_input":"2024-08-25T06:17:07.433411Z","iopub.status.idle":"2024-08-25T06:17:07.462233Z","shell.execute_reply.started":"2024-08-25T06:17:07.433381Z","shell.execute_reply":"2024-08-25T06:17:07.461398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Scaling**","metadata":{}},{"cell_type":"code","source":"train = train.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.463423Z","iopub.execute_input":"2024-08-25T06:17:07.463723Z","iopub.status.idle":"2024-08-25T06:17:07.472809Z","shell.execute_reply.started":"2024-08-25T06:17:07.463691Z","shell.execute_reply":"2024-08-25T06:17:07.471752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(columns=['target'])\ny = train['target']\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.473862Z","iopub.execute_input":"2024-08-25T06:17:07.474158Z","iopub.status.idle":"2024-08-25T06:17:07.483929Z","shell.execute_reply.started":"2024-08-25T06:17:07.474126Z","shell.execute_reply":"2024-08-25T06:17:07.483099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = MinMaxScaler(feature_range=(-1, 1))\nX = pd.DataFrame(scaler.fit_transform(X), columns=X.columns)\ntest_final = pd.DataFrame(scaler.transform(test), columns = test.columns)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.484888Z","iopub.execute_input":"2024-08-25T06:17:07.485149Z","iopub.status.idle":"2024-08-25T06:17:07.536374Z","shell.execute_reply.started":"2024-08-25T06:17:07.485120Z","shell.execute_reply":"2024-08-25T06:17:07.535504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.537483Z","iopub.execute_input":"2024-08-25T06:17:07.537761Z","iopub.status.idle":"2024-08-25T06:17:07.554318Z","shell.execute_reply.started":"2024-08-25T06:17:07.537730Z","shell.execute_reply":"2024-08-25T06:17:07.553293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,X_test.shape,y_train.shape,y_test.shape\nX_train.dtypes,X_test.dtypes,y_train.dtypes,y_test.dtypes\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.557951Z","iopub.execute_input":"2024-08-25T06:17:07.558309Z","iopub.status.idle":"2024-08-25T06:17:07.568074Z","shell.execute_reply.started":"2024-08-25T06:17:07.558251Z","shell.execute_reply":"2024-08-25T06:17:07.567172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Basic Forest Regressor**","metadata":{}},{"cell_type":"code","source":"rfr= RandomForestRegressor(n_estimators=100,n_jobs=-1)\n\nrfr.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:07.569275Z","iopub.execute_input":"2024-08-25T06:17:07.569592Z","iopub.status.idle":"2024-08-25T06:17:32.884832Z","shell.execute_reply.started":"2024-08-25T06:17:07.569557Z","shell.execute_reply":"2024-08-25T06:17:32.883850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accu = mean_squared_error(y_test, rfr.predict(X_test))\n\naccu","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:32.885914Z","iopub.execute_input":"2024-08-25T06:17:32.886210Z","iopub.status.idle":"2024-08-25T06:17:32.943499Z","shell.execute_reply.started":"2024-08-25T06:17:32.886177Z","shell.execute_reply":"2024-08-25T06:17:32.942613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **selecting the most relevent features**","metadata":{}},{"cell_type":"code","source":"\nselector = RFE(rfr, n_features_to_select=150, step=1)\n\nselector.fit(X_train,y_train)\n\nX_train = X_train.iloc[:, selector.support_]\n\nX_test = X_test.iloc[:, selector.support_]\n\ntest_final = test_final.iloc[:, selector.support_]\n\nrfr.fit( X_train,y_train )\n","metadata":{"execution":{"iopub.status.busy":"2024-08-25T06:17:32.944568Z","iopub.execute_input":"2024-08-25T06:17:32.944867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accu = mean_squared_error(y_test, rfr.predict(X_test))\n\naccu","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  **Changing parameters while using same 150 features**","metadata":{}},{"cell_type":"code","source":"rfr= RandomForestRegressor(n_estimators=150,n_jobs=-1,max_depth=10)\n\nrfr.fit(X_train,y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accu = mean_squared_error(y_test, rfr.predict(X_test))\n\naccu","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generating Submission File and Conclusion","metadata":{}},{"cell_type":"code","source":"X_train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds= rfr.predict(test_final)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/first-steps-in-data-science-new/sample.csv\")\nsubmission['target'] = test_preds\nsubmission.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}