{"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)\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":"2022-07-18T14:06:14.002874Z","iopub.execute_input":"2022-07-18T14:06:14.003200Z","iopub.status.idle":"2022-07-18T14:06:14.037913Z","shell.execute_reply.started":"2022-07-18T14:06:14.003117Z","shell.execute_reply":"2022-07-18T14:06:14.037027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport sklearn.model_selection as model_selection\n\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.compose import make_column_transformer\n\n\n#For the missing values \nfrom sklearn.impute import KNNImputer","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:14.040141Z","iopub.execute_input":"2022-07-18T14:06:14.041337Z","iopub.status.idle":"2022-07-18T14:06:15.209896Z","shell.execute_reply.started":"2022-07-18T14:06:14.041294Z","shell.execute_reply":"2022-07-18T14:06:15.208999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Binning and creating Dummy Variables\nfrom sklearn.feature_selection import SelectKBest, chi2\nfrom sklearn.preprocessing import KBinsDiscretizer","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.211360Z","iopub.execute_input":"2022-07-18T14:06:15.211701Z","iopub.status.idle":"2022-07-18T14:06:15.227655Z","shell.execute_reply.started":"2022-07-18T14:06:15.211657Z","shell.execute_reply":"2022-07-18T14:06:15.226995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.230494Z","iopub.execute_input":"2022-07-18T14:06:15.230835Z","iopub.status.idle":"2022-07-18T14:06:15.235120Z","shell.execute_reply.started":"2022-07-18T14:06:15.230800Z","shell.execute_reply":"2022-07-18T14:06:15.234252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')\nsub_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.236525Z","iopub.execute_input":"2022-07-18T14:06:15.237083Z","iopub.status.idle":"2022-07-18T14:06:15.327212Z","shell.execute_reply.started":"2022-07-18T14:06:15.237034Z","shell.execute_reply":"2022-07-18T14:06:15.326461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.328594Z","iopub.execute_input":"2022-07-18T14:06:15.329056Z","iopub.status.idle":"2022-07-18T14:06:15.366821Z","shell.execute_reply.started":"2022-07-18T14:06:15.329024Z","shell.execute_reply":"2022-07-18T14:06:15.366165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.368013Z","iopub.execute_input":"2022-07-18T14:06:15.368661Z","iopub.status.idle":"2022-07-18T14:06:15.397304Z","shell.execute_reply.started":"2022-07-18T14:06:15.368628Z","shell.execute_reply":"2022-07-18T14:06:15.396401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have to split the data into train and test ","metadata":{"execution":{"iopub.status.busy":"2021-11-05T12:30:13.978635Z","iopub.execute_input":"2021-11-05T12:30:13.978909Z","iopub.status.idle":"2021-11-05T12:30:13.984415Z","shell.execute_reply.started":"2021-11-05T12:30:13.978881Z","shell.execute_reply":"2021-11-05T12:30:13.983439Z"}}},{"cell_type":"code","source":"y = train['SalePrice']\nX = train.drop(['SalePrice','Id'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.399197Z","iopub.execute_input":"2022-07-18T14:06:15.399659Z","iopub.status.idle":"2022-07-18T14:06:15.416193Z","shell.execute_reply.started":"2022-07-18T14:06:15.399602Z","shell.execute_reply":"2022-07-18T14:06:15.415256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = model_selection.train_test_split(X,y,test_size=0.2, random_state = 200)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.417623Z","iopub.execute_input":"2022-07-18T14:06:15.418061Z","iopub.status.idle":"2022-07-18T14:06:15.434430Z","shell.execute_reply.started":"2022-07-18T14:06:15.418015Z","shell.execute_reply":"2022-07-18T14:06:15.433414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_features = [ c for c,dtype in zip(X.columns, X.dtypes) if dtype.kind in ['i','f']]\n\nprint('Numerical : ' + str(numerical_features))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.437415Z","iopub.execute_input":"2022-07-18T14:06:15.437746Z","iopub.status.idle":"2022-07-18T14:06:15.445236Z","shell.execute_reply.started":"2022-07-18T14:06:15.437697Z","shell.execute_reply":"2022-07-18T14:06:15.444453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building Pipeline","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.447685Z","iopub.execute_input":"2022-07-18T14:06:15.448404Z","iopub.status.idle":"2022-07-18T14:06:15.458019Z","shell.execute_reply.started":"2022-07-18T14:06:15.448366Z","shell.execute_reply":"2022-07-18T14:06:15.457039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocessor = make_column_transformer(\n    (make_pipeline(\n    KNNImputer(n_neighbors=10),\n    KBinsDiscretizer(n_bins=6),\n    SelectKBest(chi2, k=15),\n    ), numerical_features)\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.461469Z","iopub.execute_input":"2022-07-18T14:06:15.461710Z","iopub.status.idle":"2022-07-18T14:06:15.471856Z","shell.execute_reply.started":"2022-07-18T14:06:15.461682Z","shell.execute_reply":"2022-07-18T14:06:15.471129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regModel = make_pipeline(preprocessor, LinearRegression())","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.472835Z","iopub.execute_input":"2022-07-18T14:06:15.473242Z","iopub.status.idle":"2022-07-18T14:06:15.489454Z","shell.execute_reply.started":"2022-07-18T14:06:15.473209Z","shell.execute_reply":"2022-07-18T14:06:15.488751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit Model","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.490551Z","iopub.execute_input":"2022-07-18T14:06:15.490914Z","iopub.status.idle":"2022-07-18T14:06:15.500107Z","shell.execute_reply.started":"2022-07-18T14:06:15.490882Z","shell.execute_reply":"2022-07-18T14:06:15.499265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regModel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.501505Z","iopub.execute_input":"2022-07-18T14:06:15.501913Z","iopub.status.idle":"2022-07-18T14:06:15.777176Z","shell.execute_reply.started":"2022-07-18T14:06:15.501872Z","shell.execute_reply":"2022-07-18T14:06:15.776256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check Accuracy","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.778356Z","iopub.execute_input":"2022-07-18T14:06:15.778664Z","iopub.status.idle":"2022-07-18T14:06:15.783873Z","shell.execute_reply.started":"2022-07-18T14:06:15.778624Z","shell.execute_reply":"2022-07-18T14:06:15.782731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = regModel.predict(X_train)\ny_test_pred = regModel.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.785690Z","iopub.execute_input":"2022-07-18T14:06:15.786376Z","iopub.status.idle":"2022-07-18T14:06:15.893257Z","shell.execute_reply.started":"2022-07-18T14:06:15.786339Z","shell.execute_reply":"2022-07-18T14:06:15.892279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Train RMSE : {np.sqrt(mean_squared_error(y_train,y_train_pred)):.0f}')\nprint(f'Test RMSE : {np.sqrt(mean_squared_error(y_test,y_test_pred)):.0f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.899755Z","iopub.execute_input":"2022-07-18T14:06:15.903053Z","iopub.status.idle":"2022-07-18T14:06:15.913697Z","shell.execute_reply.started":"2022-07-18T14:06:15.902986Z","shell.execute_reply":"2022-07-18T14:06:15.912881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make submission","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.915155Z","iopub.execute_input":"2022-07-18T14:06:15.915642Z","iopub.status.idle":"2022-07-18T14:06:15.922314Z","shell.execute_reply.started":"2022-07-18T14:06:15.915600Z","shell.execute_reply":"2022-07-18T14:06:15.921500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_sub_pred = regModel.predict(sub_test.drop(['Id'], axis = 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:15.923904Z","iopub.execute_input":"2022-07-18T14:06:15.924299Z","iopub.status.idle":"2022-07-18T14:06:16.021913Z","shell.execute_reply.started":"2022-07-18T14:06:15.924250Z","shell.execute_reply":"2022-07-18T14:06:16.020864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'Id':sub_test['Id'], 'SalePrice': y_sub_pred})","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:16.023275Z","iopub.execute_input":"2022-07-18T14:06:16.023793Z","iopub.status.idle":"2022-07-18T14:06:16.030089Z","shell.execute_reply.started":"2022-07-18T14:06:16.023731Z","shell.execute_reply":"2022-07-18T14:06:16.029193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('Reg_Model_pipline.csv', index= False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:16.031614Z","iopub.execute_input":"2022-07-18T14:06:16.032103Z","iopub.status.idle":"2022-07-18T14:06:16.178321Z","shell.execute_reply.started":"2022-07-18T14:06:16.032060Z","shell.execute_reply":"2022-07-18T14:06:16.177274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T14:06:16.179552Z","iopub.execute_input":"2022-07-18T14:06:16.179806Z","iopub.status.idle":"2022-07-18T14:06:16.188811Z","shell.execute_reply.started":"2022-07-18T14:06:16.179754Z","shell.execute_reply":"2022-07-18T14:06:16.188203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}