{"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 \nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\nfrom sklearn.feature_selection import SelectKBest, mutual_info_regression, VarianceThreshold\nfrom sklearn.preprocessing import StandardScaler, OrdinalEncoder\nfrom sklearn.impute import SimpleImputer\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-03T15:56:30.310542Z","iopub.execute_input":"2022-08-03T15:56:30.310977Z","iopub.status.idle":"2022-08-03T15:56:30.321316Z","shell.execute_reply.started":"2022-08-03T15:56:30.310945Z","shell.execute_reply":"2022-08-03T15:56:30.320205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/mercedes-benz-greener-manufacturing/train.csv.zip')\ntest = pd.read_csv('../input/mercedes-benz-greener-manufacturing/test.csv.zip')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:30.652383Z","iopub.execute_input":"2022-08-03T15:56:30.653055Z","iopub.status.idle":"2022-08-03T15:56:30.943075Z","shell.execute_reply.started":"2022-08-03T15:56:30.653019Z","shell.execute_reply":"2022-08-03T15:56:30.942100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check Data","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:30.944764Z","iopub.execute_input":"2022-08-03T15:56:30.945265Z","iopub.status.idle":"2022-08-03T15:56:30.973870Z","shell.execute_reply.started":"2022-08-03T15:56:30.945233Z","shell.execute_reply":"2022-08-03T15:56:30.972923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:31.122937Z","iopub.execute_input":"2022-08-03T15:56:31.124645Z","iopub.status.idle":"2022-08-03T15:56:31.147542Z","shell.execute_reply.started":"2022-08-03T15:56:31.124603Z","shell.execute_reply":"2022-08-03T15:56:31.146273Z"},"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(['ID','y'], axis = 1)\n\nX_test = test.drop(['ID'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:31.317036Z","iopub.execute_input":"2022-08-03T15:56:31.317499Z","iopub.status.idle":"2022-08-03T15:56:31.336136Z","shell.execute_reply.started":"2022-08-03T15:56:31.317465Z","shell.execute_reply":"2022-08-03T15:56:31.334887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get Numerical and Categorical Features ","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-03T15:56:31.504086Z","iopub.execute_input":"2022-08-03T15:56:31.505549Z","iopub.status.idle":"2022-08-03T15:56:31.520959Z","shell.execute_reply.started":"2022-08-03T15:56:31.505507Z","shell.execute_reply":"2022-08-03T15:56:31.520196Z"},"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-03T15:56:31.687904Z","iopub.execute_input":"2022-08-03T15:56:31.689084Z","iopub.status.idle":"2022-08-03T15:56:31.697180Z","shell.execute_reply.started":"2022-08-03T15:56:31.689044Z","shell.execute_reply":"2022-08-03T15:56:31.696453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Test 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 = 35)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:31.872292Z","iopub.execute_input":"2022-08-03T15:56:31.873596Z","iopub.status.idle":"2022-08-03T15:56:31.891835Z","shell.execute_reply.started":"2022-08-03T15:56:31.873540Z","shell.execute_reply":"2022-08-03T15:56:31.890746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Ordinal Encoding","metadata":{}},{"cell_type":"code","source":"enc_oe = OrdinalEncoder(handle_unknown = 'use_encoded_value', unknown_value = np.NaN)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:32.031257Z","iopub.execute_input":"2022-08-03T15:56:32.031705Z","iopub.status.idle":"2022-08-03T15:56:32.037158Z","shell.execute_reply.started":"2022-08-03T15:56:32.031672Z","shell.execute_reply":"2022-08-03T15:56:32.036204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc_oe.fit(X_train[categorical_features])\n\n#tranform Train Data\nX_train[categorical_features] = enc_oe.transform(X_train[categorical_features])\n\n#tranform Val Data\nX_test[categorical_features] = enc_oe.transform(X_test[categorical_features])\n\n#tranform Val Data\nX_val[categorical_features] = enc_oe.transform(X_val[categorical_features])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:32.172953Z","iopub.execute_input":"2022-08-03T15:56:32.174073Z","iopub.status.idle":"2022-08-03T15:56:32.225204Z","shell.execute_reply.started":"2022-08-03T15:56:32.174005Z","shell.execute_reply":"2022-08-03T15:56:32.223999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Missing Values Treatment","metadata":{}},{"cell_type":"code","source":"imp = SimpleImputer(strategy = 'median')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:32.405558Z","iopub.execute_input":"2022-08-03T15:56:32.406630Z","iopub.status.idle":"2022-08-03T15:56:32.411430Z","shell.execute_reply.started":"2022-08-03T15:56:32.406593Z","shell.execute_reply":"2022-08-03T15:56:32.410277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp.fit(X_train)\n\n#tranform Train Data\nX_train = imp.transform(X_train)\n\n#tranform Val Data\nX_test = imp.transform(X_test)\n\n#tranform Val Data\nX_val = imp.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:32.614472Z","iopub.execute_input":"2022-08-03T15:56:32.615091Z","iopub.status.idle":"2022-08-03T15:56:32.751458Z","shell.execute_reply.started":"2022-08-03T15:56:32.615056Z","shell.execute_reply":"2022-08-03T15:56:32.750263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Select High Variance Features","metadata":{}},{"cell_type":"code","source":"selector = SelectKBest(mutual_info_regression, k = 30)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:32.757765Z","iopub.execute_input":"2022-08-03T15:56:32.758115Z","iopub.status.idle":"2022-08-03T15:56:32.763463Z","shell.execute_reply.started":"2022-08-03T15:56:32.758085Z","shell.execute_reply":"2022-08-03T15:56:32.762146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selector.fit(X_train, y_train)\n\n#tranform Train Data\nX_train = selector.transform(X_train)\n\n#tranform Val Data\nX_test = selector.transform(X_test)\n\n#tranform Val Data\nX_val = selector.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:32.906192Z","iopub.execute_input":"2022-08-03T15:56:32.907599Z","iopub.status.idle":"2022-08-03T15:56:41.247767Z","shell.execute_reply.started":"2022-08-03T15:56:32.907527Z","shell.execute_reply":"2022-08-03T15:56:41.246597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Normalize","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.249868Z","iopub.execute_input":"2022-08-03T15:56:41.250166Z","iopub.status.idle":"2022-08-03T15:56:41.254706Z","shell.execute_reply.started":"2022-08-03T15:56:41.250138Z","shell.execute_reply":"2022-08-03T15:56:41.253905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler.fit(X_train, y_train)\n\n#tranform Train Data\nX_train = scaler.transform(X_train)\n\n#tranform Val Data\nX_test = scaler.transform(X_test)\n\n#tranform Val Data\nX_val = scaler.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.256085Z","iopub.execute_input":"2022-08-03T15:56:41.256678Z","iopub.status.idle":"2022-08-03T15:56:41.269873Z","shell.execute_reply.started":"2022-08-03T15:56:41.256646Z","shell.execute_reply":"2022-08-03T15:56:41.268613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Regression Model","metadata":{}},{"cell_type":"code","source":"lr = LinearRegression()","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.272606Z","iopub.execute_input":"2022-08-03T15:56:41.273131Z","iopub.status.idle":"2022-08-03T15:56:41.277732Z","shell.execute_reply.started":"2022-08-03T15:56:41.273099Z","shell.execute_reply":"2022-08-03T15:56:41.276631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.279336Z","iopub.execute_input":"2022-08-03T15:56:41.279958Z","iopub.status.idle":"2022-08-03T15:56:41.302544Z","shell.execute_reply.started":"2022-08-03T15:56:41.279915Z","shell.execute_reply":"2022-08-03T15:56:41.301215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make Predictions","metadata":{}},{"cell_type":"code","source":"y_train_pred = lr.predict(X_train)\ny_val_pred = lr.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.304542Z","iopub.execute_input":"2022-08-03T15:56:41.305257Z","iopub.status.idle":"2022-08-03T15:56:41.334887Z","shell.execute_reply.started":"2022-08-03T15:56:41.305211Z","shell.execute_reply":"2022-08-03T15:56:41.331770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check R Squared","metadata":{}},{"cell_type":"markdown","source":"### Train and Validation R Squared","metadata":{}},{"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-03T15:56:41.339668Z","iopub.execute_input":"2022-08-03T15:56:41.341367Z","iopub.status.idle":"2022-08-03T15:56:41.366211Z","shell.execute_reply.started":"2022-08-03T15:56:41.341295Z","shell.execute_reply":"2022-08-03T15:56:41.363351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Submission Data","metadata":{}},{"cell_type":"code","source":"y_test_pred = lr.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.369937Z","iopub.execute_input":"2022-08-03T15:56:41.371591Z","iopub.status.idle":"2022-08-03T15:56:41.383136Z","shell.execute_reply.started":"2022-08-03T15:56:41.371544Z","shell.execute_reply":"2022-08-03T15:56:41.381226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-03T15:56:41.385945Z","iopub.execute_input":"2022-08-03T15:56:41.387856Z","iopub.status.idle":"2022-08-03T15:56:41.420632Z","shell.execute_reply.started":"2022-08-03T15:56:41.387813Z","shell.execute_reply":"2022-08-03T15:56:41.419248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Export Submission","metadata":{}},{"cell_type":"code","source":"submission.to_csv('lr_basic_preprocessing_k30.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T15:56:41.424626Z","iopub.execute_input":"2022-08-03T15:56:41.425532Z","iopub.status.idle":"2022-08-03T15:56:41.462319Z","shell.execute_reply.started":"2022-08-03T15:56:41.425485Z","shell.execute_reply":"2022-08-03T15:56:41.460543Z"},"trusted":true},"execution_count":null,"outputs":[]}]}