{"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-05T22:32:12.359736Z","iopub.execute_input":"2022-07-05T22:32:12.360067Z","iopub.status.idle":"2022-07-05T22:32:12.367614Z","shell.execute_reply.started":"2022-07-05T22:32:12.360041Z","shell.execute_reply":"2022-07-05T22:32:12.366220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**In many times we use the neural network models to classify images for example but what about using neural networks in machine learning problems like regression tasks.In this notebook we will see how to implement neural network  model to predict the SalePrcie of houses SO i m not going to fucus on data visulization the main objective is to show how to implement neural network model in a regression problem  **","metadata":{}},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np \nimport matplotlib.pyplot as plt \n%matplotlib inline \nimport seaborn as sns \nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import r2_score, mean_squared_error\nfrom sklearn import metrics\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.371846Z","iopub.execute_input":"2022-07-05T22:32:12.372199Z","iopub.status.idle":"2022-07-05T22:32:12.388110Z","shell.execute_reply.started":"2022-07-05T22:32:12.372148Z","shell.execute_reply":"2022-07-05T22:32:12.387230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntrain.drop('Id',axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.788330Z","iopub.execute_input":"2022-07-05T22:32:12.789063Z","iopub.status.idle":"2022-07-05T22:32:12.826358Z","shell.execute_reply.started":"2022-07-05T22:32:12.789022Z","shell.execute_reply":"2022-07-05T22:32:12.825225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')\ntest.drop('Id',axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.828686Z","iopub.execute_input":"2022-07-05T22:32:12.829745Z","iopub.status.idle":"2022-07-05T22:32:12.867369Z","shell.execute_reply.started":"2022-07-05T22:32:12.829702Z","shell.execute_reply":"2022-07-05T22:32:12.866190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.869199Z","iopub.execute_input":"2022-07-05T22:32:12.869916Z","iopub.status.idle":"2022-07-05T22:32:12.877388Z","shell.execute_reply.started":"2022-07-05T22:32:12.869875Z","shell.execute_reply":"2022-07-05T22:32:12.876168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.878893Z","iopub.execute_input":"2022-07-05T22:32:12.879855Z","iopub.status.idle":"2022-07-05T22:32:12.911208Z","shell.execute_reply.started":"2022-07-05T22:32:12.879794Z","shell.execute_reply":"2022-07-05T22:32:12.910143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing=train.isnull().sum().sort_values(ascending=False)\nmissing=missing.drop(missing[missing==0].index)\nmissing","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.915360Z","iopub.execute_input":"2022-07-05T22:32:12.915740Z","iopub.status.idle":"2022-07-05T22:32:12.935461Z","shell.execute_reply.started":"2022-07-05T22:32:12.915711Z","shell.execute_reply":"2022-07-05T22:32:12.934225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def filling_missing_values(df):\n    \n    df['PoolQC']      =df['PoolQC'].fillna('no')\n    df['MiscFeature'] =df['MiscFeature'].fillna('no')\n    df['Alley']       =df['Alley'].fillna('no')\n    df['Fence']       =df['Fence'].fillna('no')\n    df['FireplaceQu'] =df['FireplaceQu'].fillna('no')\n    df['GarageCond']  =df['GarageCond'].fillna('no')\n    df['GarageQual']  =df['GarageQual'].fillna('no')\n    df['GarageFinish']=df['GarageFinish'].fillna('no')\n    df['BsmtExposure']=df['BsmtExposure'].fillna('no')\n    df['BsmtCond']    =df['BsmtCond'].fillna('no')\n    df['BsmtQual']    =df['BsmtQual'].fillna('no')\n    df['BsmtFinType2']=df['BsmtFinType2'].fillna('no')\n    df['BsmtFinType1']=df['BsmtFinType1'].fillna('no')\n    df['Fence']       =df['Fence'].fillna('no')\n    df['MasVnrType']  =df['MasVnrType'].fillna('no')\n    df['GarageYrBlt'] =df['GarageYrBlt'].fillna(0)\n    df['GarageType']  =df['GarageType'].fillna(0)\n    df['GarageArea']  =df['GarageArea'].fillna(0)\n    df['GarageCars']  =df['GarageCars'].fillna(0)\n    df['BsmtFinSF1']  =df['BsmtFinSF1'].fillna(0)\n    df['BsmtFinSF2']  =df['BsmtFinSF2'].fillna(0)\n    df['MasVnrArea']  =df['MasVnrArea'].fillna(0)\n    df['BsmtFullBath']=df['BsmtFullBath'].fillna(0)\n    df['BsmtHalfBath']=df['BsmtHalfBath'].fillna(0)\n    df['BsmtUnfSF']   =df['BsmtUnfSF'].fillna(0)\n    df['TotalBsmtSF'] =df['TotalBsmtSF'].fillna(0)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.936969Z","iopub.execute_input":"2022-07-05T22:32:12.937447Z","iopub.status.idle":"2022-07-05T22:32:12.952536Z","shell.execute_reply.started":"2022-07-05T22:32:12.937414Z","shell.execute_reply":"2022-07-05T22:32:12.951190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=filling_missing_values(train)\ntest=filling_missing_values(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.954010Z","iopub.execute_input":"2022-07-05T22:32:12.954443Z","iopub.status.idle":"2022-07-05T22:32:12.987226Z","shell.execute_reply.started":"2022-07-05T22:32:12.954408Z","shell.execute_reply":"2022-07-05T22:32:12.986121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop('LotFrontage',axis=1, inplace=True)\ntrain.drop('Electrical',axis=1, inplace=True)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.988713Z","iopub.execute_input":"2022-07-05T22:32:12.989110Z","iopub.status.idle":"2022-07-05T22:32:12.998813Z","shell.execute_reply.started":"2022-07-05T22:32:12.989031Z","shell.execute_reply":"2022-07-05T22:32:12.998135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop('LotFrontage',axis=1, inplace=True)\ntest.drop('Electrical',axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:12.999917Z","iopub.execute_input":"2022-07-05T22:32:13.000942Z","iopub.status.idle":"2022-07-05T22:32:13.014234Z","shell.execute_reply.started":"2022-07-05T22:32:13.000914Z","shell.execute_reply":"2022-07-05T22:32:13.013198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_train=train.select_dtypes(include='object')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.015851Z","iopub.execute_input":"2022-07-05T22:32:13.016780Z","iopub.status.idle":"2022-07-05T22:32:13.024995Z","shell.execute_reply.started":"2022-07-05T22:32:13.016723Z","shell.execute_reply":"2022-07-05T22:32:13.024210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_test=test.select_dtypes(include='object')","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.026181Z","iopub.execute_input":"2022-07-05T22:32:13.027441Z","iopub.status.idle":"2022-07-05T22:32:13.035947Z","shell.execute_reply.started":"2022-07-05T22:32:13.027402Z","shell.execute_reply":"2022-07-05T22:32:13.035108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from category_encoders import CountEncoder\nenc = CountEncoder(normalize=True, cols=cat_train.columns)\ntrain = enc.fit_transform(train)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.037790Z","iopub.execute_input":"2022-07-05T22:32:13.038399Z","iopub.status.idle":"2022-07-05T22:32:13.512549Z","shell.execute_reply.started":"2022-07-05T22:32:13.038367Z","shell.execute_reply":"2022-07-05T22:32:13.511146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc2= CountEncoder(normalize=True, cols=cat_test.columns)\ntest = enc2.fit_transform(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.514123Z","iopub.execute_input":"2022-07-05T22:32:13.514443Z","iopub.status.idle":"2022-07-05T22:32:13.868990Z","shell.execute_reply.started":"2022-07-05T22:32:13.514410Z","shell.execute_reply":"2022-07-05T22:32:13.868079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training=train.drop('SalePrice',axis=1)\ntarget_train=train['SalePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.873233Z","iopub.execute_input":"2022-07-05T22:32:13.874304Z","iopub.status.idle":"2022-07-05T22:32:13.881869Z","shell.execute_reply.started":"2022-07-05T22:32:13.874276Z","shell.execute_reply":"2022-07-05T22:32:13.881064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(training, target_train, test_size=0.33, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.882861Z","iopub.execute_input":"2022-07-05T22:32:13.883526Z","iopub.status.idle":"2022-07-05T22:32:13.898061Z","shell.execute_reply.started":"2022-07-05T22:32:13.883501Z","shell.execute_reply":"2022-07-05T22:32:13.896999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\nX_train=scaler.fit_transform(X_train)\nX_test=scaler.transform(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.899387Z","iopub.execute_input":"2022-07-05T22:32:13.899691Z","iopub.status.idle":"2022-07-05T22:32:13.919637Z","shell.execute_reply.started":"2022-07-05T22:32:13.899663Z","shell.execute_reply":"2022-07-05T22:32:13.918054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# building a neural network ","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"Unlike classification, regression problems cannot be evaluated by accuracy. Here I will use Root Mean Squared Error as the evaluation metric.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf \nfrom tensorflow import keras \nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout\nfrom keras import metrics","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.921758Z","iopub.execute_input":"2022-07-05T22:32:13.922523Z","iopub.status.idle":"2022-07-05T22:32:13.927685Z","shell.execute_reply.started":"2022-07-05T22:32:13.922489Z","shell.execute_reply":"2022-07-05T22:32:13.926993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"        model = Sequential()\n        model.add(Dense(10, input_dim=X_train.shape[1], activation='relu'))\n        model.add(Dense(30, activation='relu'))\n        model.add(Dense(40, activation='relu'))\n        model.add(Dense(1))\n        model.compile(optimizer ='adam', loss = 'mean_squared_error', \n              metrics =[metrics.mae])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.928692Z","iopub.execute_input":"2022-07-05T22:32:13.929104Z","iopub.status.idle":"2022-07-05T22:32:13.973730Z","shell.execute_reply.started":"2022-07-05T22:32:13.929080Z","shell.execute_reply":"2022-07-05T22:32:13.972989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.975029Z","iopub.execute_input":"2022-07-05T22:32:13.975342Z","iopub.status.idle":"2022-07-05T22:32:13.983346Z","shell.execute_reply.started":"2022-07-05T22:32:13.975314Z","shell.execute_reply":"2022-07-05T22:32:13.982034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, validation_data=(X_test,y_test), epochs=100, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:13.984856Z","iopub.execute_input":"2022-07-05T22:32:13.985194Z","iopub.status.idle":"2022-07-05T22:32:27.545481Z","shell.execute_reply.started":"2022-07-05T22:32:13.985165Z","shell.execute_reply":"2022-07-05T22:32:27.543190Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"I also tried another DNN model and as we see this model gave me better result in mean squared error than the pervious models","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"model2= Sequential([\n    tf.keras.layers.Input(shape = X_train.shape[1:]),\n    Dense(300, activation='tanh'),\n    Dense(300, activation='tanh'),\n    Dense(300, activation='tanh'),\n    Dense(300, activation='tanh'),\n    Dense(300, activation='tanh'),\n    Dense(300, activation='tanh'),\n    Dense(1)\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:27.547669Z","iopub.execute_input":"2022-07-05T22:32:27.548370Z","iopub.status.idle":"2022-07-05T22:32:27.610071Z","shell.execute_reply.started":"2022-07-05T22:32:27.548332Z","shell.execute_reply":"2022-07-05T22:32:27.608925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" model2.compile(optimizer ='adam', loss = 'mean_squared_error', \n              metrics =[metrics.mae])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:27.611249Z","iopub.execute_input":"2022-07-05T22:32:27.611485Z","iopub.status.idle":"2022-07-05T22:32:27.622785Z","shell.execute_reply.started":"2022-07-05T22:32:27.611463Z","shell.execute_reply":"2022-07-05T22:32:27.621914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model.fit(X_train, y_train, validation_data=(X_test,y_test), epochs=100, batch_size=32)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-05T22:32:27.624475Z","iopub.execute_input":"2022-07-05T22:32:27.625247Z","iopub.status.idle":"2022-07-05T22:32:39.560707Z","shell.execute_reply.started":"2022-07-05T22:32:27.625219Z","shell.execute_reply":"2022-07-05T22:32:39.560058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input = tf.keras.layers.Input(shape = X_train.shape[1:])\nhidden1 = tf.keras.layers.Dense(300, activation='relu')(input)\nhidden2 = tf.keras.layers.Dense(300, activation='relu')(hidden1)\nhidden3 = tf.keras.layers.Dense(300, activation='relu')(hidden2)\nhidden4 = keras.layers.Concatenate()([input, hidden3])\nhidden5 = tf.keras.layers.Dense(300, activation='relu')(hidden4)\nconcat = keras.layers.Concatenate()([input, hidden5])\noutput = keras.layers.Dense(1)(concat)\nmodel3 = keras.models.Model(inputs=[input], outputs=[output])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:39.561856Z","iopub.execute_input":"2022-07-05T22:32:39.562901Z","iopub.status.idle":"2022-07-05T22:32:39.621914Z","shell.execute_reply.started":"2022-07-05T22:32:39.562851Z","shell.execute_reply":"2022-07-05T22:32:39.621060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" model2.compile(optimizer ='adam', loss = 'mean_squared_error', \n              metrics =[metrics.mae])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T22:32:39.623111Z","iopub.execute_input":"2022-07-05T22:32:39.623589Z","iopub.status.idle":"2022-07-05T22:32:39.632918Z","shell.execute_reply.started":"2022-07-05T22:32:39.623562Z","shell.execute_reply":"2022-07-05T22:32:39.632058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history3 = model.fit(X_train, y_train, validation_data=(X_test,y_test), epochs=100, batch_size=32)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-05T22:32:39.633973Z","iopub.execute_input":"2022-07-05T22:32:39.634571Z","iopub.status.idle":"2022-07-05T22:32:51.274996Z","shell.execute_reply.started":"2022-07-05T22:32:39.634545Z","shell.execute_reply":"2022-07-05T22:32:51.274288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Well this model looks like it doesn't add much to the reduction of the mean squared error but it performs better then model number 2","metadata":{}},{"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":[]},{"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":[]},{"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":[]},{"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":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}