{"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":"markdown","source":"# Import Packages","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\nfrom sklearn.model_selection import train_test_split\n\nfrom sklearn.preprocessing import OrdinalEncoder, StandardScaler\nfrom sklearn.impute import SimpleImputer\n\nfrom sklearn.feature_selection import SelectKBest, mutual_info_regression\n\nfrom sklearn.linear_model import LinearRegression\n\nfrom sklearn.metrics import r2_score\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-04T15:32:28.030430Z","iopub.execute_input":"2022-08-04T15:32:28.030893Z","iopub.status.idle":"2022-08-04T15:32:28.059338Z","shell.execute_reply.started":"2022-08-04T15:32:28.030856Z","shell.execute_reply":"2022-08-04T15:32:28.057036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Data","metadata":{}},{"cell_type":"markdown","source":"Video Link: https://youtu.be/PpEDks6k88U","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/mercedes-benz-greener-manufacturing/train.csv.zip') \ntest = pd.read_csv('/kaggle/input/mercedes-benz-greener-manufacturing/test.csv.zip') ","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:32:30.556022Z","iopub.execute_input":"2022-08-04T15:32:30.556435Z","iopub.status.idle":"2022-08-04T15:32:30.889242Z","shell.execute_reply.started":"2022-08-04T15:32:30.556402Z","shell.execute_reply":"2022-08-04T15:32:30.887584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check Data","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:32:32.538312Z","iopub.execute_input":"2022-08-04T15:32:32.539223Z","iopub.status.idle":"2022-08-04T15:32:32.577465Z","shell.execute_reply.started":"2022-08-04T15:32:32.539178Z","shell.execute_reply":"2022-08-04T15:32:32.576243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:32:35.430482Z","iopub.execute_input":"2022-08-04T15:32:35.430879Z","iopub.status.idle":"2022-08-04T15:32:35.480935Z","shell.execute_reply.started":"2022-08-04T15:32:35.430848Z","shell.execute_reply":"2022-08-04T15:32:35.480100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Treat Target Column Outliers","metadata":{}},{"cell_type":"code","source":"train['y'].plot.box()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:34:11.650730Z","iopub.execute_input":"2022-08-04T15:34:11.651263Z","iopub.status.idle":"2022-08-04T15:34:11.878185Z","shell.execute_reply.started":"2022-08-04T15:34:11.651218Z","shell.execute_reply":"2022-08-04T15:34:11.877028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Q1 = train['y'].quantile(0.25)\nQ3 = train['y'].quantile(0.75)\n\nIQR = Q3 - Q1\n\nlower_whisker = Q1 - 1.5 * IQR\nupper_whisker = Q3 + 1.5 * IQR\n\nprint(lower_whisker)\nprint(upper_whisker)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:37:45.826475Z","iopub.execute_input":"2022-08-04T15:37:45.826926Z","iopub.status.idle":"2022-08-04T15:37:45.838119Z","shell.execute_reply.started":"2022-08-04T15:37:45.826892Z","shell.execute_reply":"2022-08-04T15:37:45.837104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outlier_index = train[(train.y > upper_whisker)].index\noutlier_index","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:40:57.119516Z","iopub.execute_input":"2022-08-04T15:40:57.119938Z","iopub.status.idle":"2022-08-04T15:40:57.129459Z","shell.execute_reply.started":"2022-08-04T15:40:57.119895Z","shell.execute_reply":"2022-08-04T15:40:57.128509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop Outliers from Train","metadata":{}},{"cell_type":"code","source":"train = train.drop(outlier_index)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:06.138130Z","iopub.execute_input":"2022-08-04T15:41:06.138552Z","iopub.status.idle":"2022-08-04T15:41:06.151563Z","shell.execute_reply.started":"2022-08-04T15:41:06.138515Z","shell.execute_reply":"2022-08-04T15:41:06.150248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:23.796012Z","iopub.execute_input":"2022-08-04T15:41:23.796542Z","iopub.status.idle":"2022-08-04T15:41:23.823222Z","shell.execute_reply.started":"2022-08-04T15:41:23.796496Z","shell.execute_reply":"2022-08-04T15:41:23.822354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Divide Data into X and y","metadata":{}},{"cell_type":"code","source":"y = train['y']\nX = train.drop(['y','ID'], axis = 1)\n\nX_test = test.drop(['ID'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:37.166277Z","iopub.execute_input":"2022-08-04T15:41:37.166675Z","iopub.status.idle":"2022-08-04T15:41:37.187673Z","shell.execute_reply.started":"2022-08-04T15:41:37.166644Z","shell.execute_reply":"2022-08-04T15:41:37.186434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train & Validation Split","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X,y, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:39.133694Z","iopub.execute_input":"2022-08-04T15:41:39.134104Z","iopub.status.idle":"2022-08-04T15:41:39.154628Z","shell.execute_reply.started":"2022-08-04T15:41:39.134073Z","shell.execute_reply":"2022-08-04T15:41:39.153560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get Categorical and Numerical Feature Names","metadata":{}},{"cell_type":"code","source":"numerical_features = X.select_dtypes(include = 'number').columns.values\nnumerical_features","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-08-04T15:41:41.346436Z","iopub.execute_input":"2022-08-04T15:41:41.346822Z","iopub.status.idle":"2022-08-04T15:41:41.363731Z","shell.execute_reply.started":"2022-08-04T15:41:41.346790Z","shell.execute_reply":"2022-08-04T15:41:41.362542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_features = X.select_dtypes(exclude = 'number').columns.values\ncategorical_features","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:49.829003Z","iopub.execute_input":"2022-08-04T15:41:49.829366Z","iopub.status.idle":"2022-08-04T15:41:49.838270Z","shell.execute_reply.started":"2022-08-04T15:41:49.829338Z","shell.execute_reply":"2022-08-04T15:41:49.837243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Categorical Encoding","metadata":{}},{"cell_type":"code","source":"oe = OrdinalEncoder(handle_unknown=\"use_encoded_value\", unknown_value= np.NaN)\noe.fit(X_train[categorical_features])","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:51.993873Z","iopub.execute_input":"2022-08-04T15:41:51.994269Z","iopub.status.idle":"2022-08-04T15:41:52.006983Z","shell.execute_reply.started":"2022-08-04T15:41:51.994239Z","shell.execute_reply":"2022-08-04T15:41:52.006016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform Train\nX_train[categorical_features] = oe.transform(X_train[categorical_features])\n\n# transform Val\nX_val[categorical_features] = oe.transform(X_val[categorical_features])\n\n# transform Test\nX_test[categorical_features] = oe.transform(X_test[categorical_features])","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:53.160650Z","iopub.execute_input":"2022-08-04T15:41:53.161439Z","iopub.status.idle":"2022-08-04T15:41:53.204988Z","shell.execute_reply.started":"2022-08-04T15:41:53.161399Z","shell.execute_reply":"2022-08-04T15:41:53.204085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Missing Value treatment","metadata":{}},{"cell_type":"code","source":"impute = SimpleImputer(strategy = 'median')\nimpute.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:55.228051Z","iopub.execute_input":"2022-08-04T15:41:55.228431Z","iopub.status.idle":"2022-08-04T15:41:55.330158Z","shell.execute_reply.started":"2022-08-04T15:41:55.228401Z","shell.execute_reply":"2022-08-04T15:41:55.329028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform Train\nX_train = impute.transform(X_train)\n\n# transform Val\nX_val = impute.transform(X_val)\n\n# transform Test\nX_test = impute.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:56.381670Z","iopub.execute_input":"2022-08-04T15:41:56.382200Z","iopub.status.idle":"2022-08-04T15:41:56.443022Z","shell.execute_reply.started":"2022-08-04T15:41:56.382148Z","shell.execute_reply":"2022-08-04T15:41:56.441828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transformation","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nscaler.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:41:58.329262Z","iopub.execute_input":"2022-08-04T15:41:58.330127Z","iopub.status.idle":"2022-08-04T15:41:58.349521Z","shell.execute_reply.started":"2022-08-04T15:41:58.330077Z","shell.execute_reply":"2022-08-04T15:41:58.348459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform Train\nX_train = scaler.transform(X_train)\n\n# transform Val\nX_val = scaler.transform(X_val)\n\n# transform Test\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:00.037413Z","iopub.execute_input":"2022-08-04T15:42:00.037809Z","iopub.status.idle":"2022-08-04T15:42:00.059938Z","shell.execute_reply.started":"2022-08-04T15:42:00.037777Z","shell.execute_reply":"2022-08-04T15:42:00.058659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Select top Features","metadata":{}},{"cell_type":"code","source":"sel = SelectKBest(mutual_info_regression, k = 30)\nsel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:01.707192Z","iopub.execute_input":"2022-08-04T15:42:01.707567Z","iopub.status.idle":"2022-08-04T15:42:10.719823Z","shell.execute_reply.started":"2022-08-04T15:42:01.707538Z","shell.execute_reply":"2022-08-04T15:42:10.718695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transform Train\nX_train = sel.transform(X_train)\n\n# transform Val\nX_val = sel.transform(X_val)\n\n# transform Test\nX_test = sel.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:16.081802Z","iopub.execute_input":"2022-08-04T15:42:16.082189Z","iopub.status.idle":"2022-08-04T15:42:16.091721Z","shell.execute_reply.started":"2022-08-04T15:42:16.082159Z","shell.execute_reply":"2022-08-04T15:42:16.090637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model","metadata":{}},{"cell_type":"code","source":"lr = LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:18.678210Z","iopub.execute_input":"2022-08-04T15:42:18.679263Z","iopub.status.idle":"2022-08-04T15:42:18.684537Z","shell.execute_reply.started":"2022-08-04T15:42:18.679208Z","shell.execute_reply":"2022-08-04T15:42:18.683469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:20.715812Z","iopub.execute_input":"2022-08-04T15:42:20.716196Z","iopub.status.idle":"2022-08-04T15:42:20.752204Z","shell.execute_reply.started":"2022-08-04T15:42:20.716165Z","shell.execute_reply":"2022-08-04T15:42:20.750317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict Y","metadata":{}},{"cell_type":"code","source":"y_train_pred = lr.predict(X_train)\ny_val_pred = lr.predict(X_val)\n\ny_test_pred = lr.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:23.318583Z","iopub.execute_input":"2022-08-04T15:42:23.319097Z","iopub.status.idle":"2022-08-04T15:42:23.335019Z","shell.execute_reply.started":"2022-08-04T15:42:23.319051Z","shell.execute_reply":"2022-08-04T15:42:23.333041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check R Squared","metadata":{"execution":{"iopub.status.busy":"2022-08-04T13:40:35.933352Z","iopub.execute_input":"2022-08-04T13:40:35.933787Z","iopub.status.idle":"2022-08-04T13:40:35.940830Z","shell.execute_reply.started":"2022-08-04T13:40:35.933751Z","shell.execute_reply":"2022-08-04T13:40:35.939988Z"}}},{"cell_type":"code","source":"print(r2_score(y_train, y_train_pred))\nprint(r2_score(y_val, y_val_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:25.573378Z","iopub.execute_input":"2022-08-04T15:42:25.573755Z","iopub.status.idle":"2022-08-04T15:42:25.582692Z","shell.execute_reply.started":"2022-08-04T15:42:25.573724Z","shell.execute_reply":"2022-08-04T15:42:25.581213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission ","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ID' : test['ID'],\n    'y' : y_test_pred\n})\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:36.565427Z","iopub.execute_input":"2022-08-04T15:42:36.565816Z","iopub.status.idle":"2022-08-04T15:42:36.577723Z","shell.execute_reply.started":"2022-08-04T15:42:36.565785Z","shell.execute_reply":"2022-08-04T15:42:36.576209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Export ","metadata":{}},{"cell_type":"code","source":"submission.to_csv('Submission_LR_F30.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:42:39.699350Z","iopub.execute_input":"2022-08-04T15:42:39.699739Z","iopub.status.idle":"2022-08-04T15:42:39.717687Z","shell.execute_reply.started":"2022-08-04T15:42:39.699707Z","shell.execute_reply":"2022-08-04T15:42:39.716524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}