{"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":"none","dataSources":[{"sourceId":84253,"databundleVersionId":9421888,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.feature_selection import RFE\nfrom sklearn.model_selection import KFold\nfrom sklearn.linear_model import Lasso","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:05.733112Z","iopub.execute_input":"2024-08-28T02:05:05.733549Z","iopub.status.idle":"2024-08-28T02:05:08.379917Z","shell.execute_reply.started":"2024-08-28T02:05:05.733499Z","shell.execute_reply":"2024-08-28T02:05:08.378706Z"},"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\")\nsample = pd.read_csv(\"/kaggle/input/first-steps-in-data-science-new/sample.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:08.381861Z","iopub.execute_input":"2024-08-28T02:05:08.382410Z","iopub.status.idle":"2024-08-28T02:05:12.337020Z","shell.execute_reply.started":"2024-08-28T02:05:08.382368Z","shell.execute_reply":"2024-08-28T02:05:12.335825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.338297Z","iopub.execute_input":"2024-08-28T02:05:12.338645Z","iopub.status.idle":"2024-08-28T02:05:12.384968Z","shell.execute_reply.started":"2024-08-28T02:05:12.338609Z","shell.execute_reply":"2024-08-28T02:05:12.383372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.388108Z","iopub.execute_input":"2024-08-28T02:05:12.388518Z","iopub.status.idle":"2024-08-28T02:05:12.396508Z","shell.execute_reply.started":"2024-08-28T02:05:12.388478Z","shell.execute_reply":"2024-08-28T02:05:12.395082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.398309Z","iopub.execute_input":"2024-08-28T02:05:12.398799Z","iopub.status.idle":"2024-08-28T02:05:12.880422Z","shell.execute_reply.started":"2024-08-28T02:05:12.398748Z","shell.execute_reply":"2024-08-28T02:05:12.879213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.882543Z","iopub.execute_input":"2024-08-28T02:05:12.883048Z","iopub.status.idle":"2024-08-28T02:05:12.897056Z","shell.execute_reply.started":"2024-08-28T02:05:12.882977Z","shell.execute_reply":"2024-08-28T02:05:12.895678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.898950Z","iopub.execute_input":"2024-08-28T02:05:12.899492Z","iopub.status.idle":"2024-08-28T02:05:12.927461Z","shell.execute_reply.started":"2024-08-28T02:05:12.899448Z","shell.execute_reply":"2024-08-28T02:05:12.926289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.929299Z","iopub.execute_input":"2024-08-28T02:05:12.929696Z","iopub.status.idle":"2024-08-28T02:05:12.944245Z","shell.execute_reply.started":"2024-08-28T02:05:12.929657Z","shell.execute_reply":"2024-08-28T02:05:12.943120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(columns=['target'])\ny = train['target']","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.945621Z","iopub.execute_input":"2024-08-28T02:05:12.945973Z","iopub.status.idle":"2024-08-28T02:05:12.959800Z","shell.execute_reply.started":"2024-08-28T02:05:12.945936Z","shell.execute_reply":"2024-08-28T02:05:12.958275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train-test split\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.963472Z","iopub.execute_input":"2024-08-28T02:05:12.963859Z","iopub.status.idle":"2024-08-28T02:05:12.980615Z","shell.execute_reply.started":"2024-08-28T02:05:12.963820Z","shell.execute_reply":"2024-08-28T02:05:12.978831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Feature scaling\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_val = scaler.transform(X_val)\ntest = scaler.transform(test)","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:12.982269Z","iopub.execute_input":"2024-08-28T02:05:12.982718Z","iopub.status.idle":"2024-08-28T02:05:13.135550Z","shell.execute_reply.started":"2024-08-28T02:05:12.982675Z","shell.execute_reply":"2024-08-28T02:05:13.134158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import Lasso\nfrom sklearn.model_selection import GridSearchCV\n\n# Define the range of alpha values to test\nalpha_values = [0.001, 0.01, 0.1, 1, 10, 100]\n\n# Set up Lasso regression with GridSearchCV\nlasso = Lasso()\nparam_grid = {'alpha': alpha_values}\ngrid_search = GridSearchCV(lasso, param_grid, cv=5)\ngrid_search.fit(X_train, y_train)\n\n# Best alpha value\nprint(f\"Best alpha for Lasso Regression: {grid_search.best_params_['alpha']}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:13.137144Z","iopub.execute_input":"2024-08-28T02:05:13.137566Z","iopub.status.idle":"2024-08-28T02:05:15.183287Z","shell.execute_reply.started":"2024-08-28T02:05:13.137526Z","shell.execute_reply":"2024-08-28T02:05:15.182148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\n# Model training\nmodel = Lasso(0.01)\nmodel.fit(X_train, y_train)\n\n# Validation\ny_pred = model.predict(X_val)\nprint(\"RMSE:\", mean_squared_error(y_val, y_pred, squared=False))\n\n# Prediction on test data\ntest_preds = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:06:06.332486Z","iopub.execute_input":"2024-08-28T02:06:06.333071Z","iopub.status.idle":"2024-08-28T02:06:06.399186Z","shell.execute_reply.started":"2024-08-28T02:06:06.333008Z","shell.execute_reply":"2024-08-28T02:06:06.397533Z"},"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(\"lasso.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-28T02:05:15.736754Z","iopub.status.idle":"2024-08-28T02:05:15.737379Z","shell.execute_reply.started":"2024-08-28T02:05:15.737074Z","shell.execute_reply":"2024-08-28T02:05:15.737105Z"},"trusted":true},"execution_count":null,"outputs":[]}]}