{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# House prices | Regression model\n\n* Rodrigo Castillo González","metadata":{}},{"cell_type":"markdown","source":"## 1 - Load data","metadata":{}},{"cell_type":"code","source":"# packages\nlibrary(tidyverse)\nlibrary(keras)\nlibrary(tfdatasets)\nlibrary(caret)\n\n# load data\ntrain_path <- \"../input/house-prices-advanced-regression-techniques/train.csv\"\ntest_path <- \"../input/house-prices-advanced-regression-techniques/test.csv\"\n\ntrain_df <- read.csv( train_path, header = TRUE )\ntest_df <- read.csv( test_path, header = TRUE)\n\ntrain_df <- filter( train_df, !is.na(SalePrice) )","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-07-13T18:17:45.111027Z","iopub.execute_input":"2022-07-13T18:17:45.148218Z","iopub.status.idle":"2022-07-13T18:17:45.270459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2 - Explore data","metadata":{}},{"cell_type":"code","source":"head(train_df, 5)\ntail(train_df, 5)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:17:50.152087Z","iopub.execute_input":"2022-07-13T18:17:50.153538Z","iopub.status.idle":"2022-07-13T18:17:50.215267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  data_n : elements\ndata_n <- dim( train_df )[1]\n# data_d : features\ndata_d <- dim( train_df )[2]\n\nprint(\"Training data set size:\")\nprint(data_n)\n\nprint(\"Features:\")\nprint(data_d)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:17:53.031535Z","iopub.execute_input":"2022-07-13T18:17:53.033113Z","iopub.status.idle":"2022-07-13T18:17:53.063682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary( train_df )","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:17:56.107330Z","iopub.execute_input":"2022-07-13T18:17:56.108825Z","iopub.status.idle":"2022-07-13T18:17:56.148143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3 - Visualization","metadata":{}},{"cell_type":"code","source":"# price distribution\nggplot( train_df, aes( x = SalePrice)) + \n  geom_histogram( binwidth = 10000, color = \"black\", fill=\"white\") +\n  geom_vline( xintercept = mean( train_df$SalePrice ), color = 'red' ) +\n  ggtitle(\"Price distribution\") + \n  xlab(\"Sale Price\") + ylab(\"Count\") + \n  theme( title = element_text(size = 16),\n         axis.title.y = element_text(size = 11),\n         axis.text.y = element_text(size = 11),\n         axis.title.x = element_text(size = 11),\n         axis.text.x = element_text(size = 11),\n         plot.margin = margin(t = 5, r = 15, b = 5, l = 15),\n         plot.caption = element_text(size = 10),\n         aspect.ratio = 0.8)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:00.408749Z","iopub.execute_input":"2022-07-13T18:18:00.410271Z","iopub.status.idle":"2022-07-13T18:18:00.952111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4 - Preprocessing","metadata":{}},{"cell_type":"code","source":"features <-  colnames(train_df)\nfeatures <- intersect(colnames(train_df %>% select_if(~ !any(is.na(.)))),\n                            colnames(test_df %>% select_if(~ !any(is.na(.)))))\nprint(features)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:03.370843Z","iopub.execute_input":"2022-07-13T18:18:03.372356Z","iopub.status.idle":"2022-07-13T18:18:03.574796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For this activity we're only considering numerical and nominal data","metadata":{}},{"cell_type":"markdown","source":"Features to consider\n* LotArea: numerical\n* Street: \n    - Grvl/Gravel = 0, Pave/Paved = 1\n* LotShape:\n    - IR3 = 0, IR2 = 0.33, IR1 = 0.67, Regular = 1\n* OverallQual: numerical\n* OverallCond: numerical\n* YearBuilt: numerical\n* ExterQual:\n    - Po = 0, Fa = 0.25, TA = 0.5, Gd = 0.75, Ex = 1\n* ExterCond:\n    - Po = 0, Fa = 0.25, TA = 0.5, Gd = 0.75, Ex = 1\n* HeatingQC:\n    - Po = 0, Fa = 0.25, TA = 0.5, Gd = 0.75, Ex = 1\n* CentralAir:\n    - N = 0, Y = 1\n* X1stFlrSF: numerical\n* X2ndFlrSF: numerical\n* LowQualFinSF: numerical\n* GrLivArea: numerical\n* FullBath: numerical\n* HalfBath: numerical\n* BedroomAbvGr: numerical\n* KitchenAbvGr: numerical\n* TotRmsAbvGrd: numerical\n* WoodDeckSF: numerical\n* OpenPorchSF: numerical\n* EnclosedPorch: numerical\n* X3SsnPorch: numerical\n* ScreenPorch: numerical\n* PoolArea: numerical\n* Year Sold: numerical","metadata":{"execution":{"iopub.status.busy":"2022-07-12T22:43:01.039790Z","iopub.execute_input":"2022-07-12T22:43:01.043128Z","iopub.status.idle":"2022-07-12T22:43:01.065223Z"}}},{"cell_type":"code","source":"features <- c( \"LotArea\", \"Street\", \"LotShape\", \"OverallQual\", \"OverallCond\", \"YearBuilt\", \"ExterQual\",\n               \"ExterCond\", \"HeatingQC\", \"CentralAir\", \"X1stFlrSF\", \"X2ndFlrSF\", \"LowQualFinSF\",\"GrLivArea\",\n               \"FullBath\", \"HalfBath\", \"BedroomAbvGr\", \"KitchenAbvGr\", \"TotRmsAbvGrd\", \"WoodDeckSF\",\n               \"OpenPorchSF\", \"EnclosedPorch\", \"X3SsnPorch\", \"ScreenPorch\", \"PoolArea\", \"YrSold\" )\n\ntrain_y <- train_df$SalePrice\n\ntrain_x <- train_df[,features]\n\n\ntrain_n <- dim( train_x )[1]\ntrain_d <- dim( train_x )[2]\n\n# Street\ntrain_x$Street[ train_x$Street == 'Grvl' ] <- 0\ntrain_x$Street[ train_x$Street == 'Gravel' ] <- 0\ntrain_x$Street[ train_x$Street == 'Pave' ] <- 1\ntrain_x$Street[ train_x$Street == 'Paved' ] <- 1\n\n# Lot shape\ntrain_x$LotShape[ train_x$LotShape == 'IR3' ] <- 0\ntrain_x$LotShape[ train_x$LotShape == 'IR2' ] <- 0.33\ntrain_x$LotShape[ train_x$LotShape == 'IR1' ] <- 0.67\ntrain_x$LotShape[ train_x$LotShape == 'Reg' ] <- 1.0\n\n# ExterQual, ExterCond, HeatingQC\ntrain_x[ train_x == 'Po'] <- 0\ntrain_x[ train_x == 'Fa'] <- 0.25\ntrain_x[ train_x == 'TA'] <- 0.5\ntrain_x[ train_x == 'Gd'] <- 0.75\ntrain_x[ train_x == 'Ex'] <- 1\n\n# Central Air\ntrain_x$CentralAir[ train_x$CentralAir == 'N' ] <- 0\ntrain_x$CentralAir[ train_x$CentralAir == 'Y' ] <- 1\n\n# to numerical\ntrain_x = as.data.frame( sapply( train_x, as.numeric ) )\ntrain_y = as.data.frame( sapply( train_y, as.numeric ) )\n\nprocess <- preProcess(train_x, method = c(\"range\") )\ntrain_x <- predict(process, train_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:08.002171Z","iopub.execute_input":"2022-07-13T18:18:08.003674Z","iopub.status.idle":"2022-07-13T18:18:08.135478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Training data set size:\")\nprint(train_n)\n\nprint(\"Features:\")\nprint(train_d)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:12.738005Z","iopub.execute_input":"2022-07-13T18:18:12.739478Z","iopub.status.idle":"2022-07-13T18:18:12.766082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"head(train_x,5)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:14.731643Z","iopub.execute_input":"2022-07-13T18:18:14.733318Z","iopub.status.idle":"2022-07-13T18:18:14.762523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5 - Regrssion model","metadata":{}},{"cell_type":"code","source":"model <- keras_model_sequential() %>%\nlayer_dense( units = 64,\n             activation=\"relu\",\n             input_shape = c( train_d ) ) %>% \nlayer_dense( units=64, \n             activation = \"relu\" ) %>%\nlayer_dense(units=1, , activation=\"linear\")\n\n\n\nmodel %>% \n  compile(\n    loss = \"mse\",\n    optimizer =  \"rmsprop\" ,\n    metrics = list( c(\"mean_absolute_error\") )\n  )","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:17.532637Z","iopub.execute_input":"2022-07-13T18:18:17.534103Z","iopub.status.idle":"2022-07-13T18:18:27.528362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model %>% summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:29.321194Z","iopub.execute_input":"2022-07-13T18:18:29.322910Z","iopub.status.idle":"2022-07-13T18:18:29.346863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_dot_callback <- callback_lambda(\n  on_epoch_end = function(epoch, logs) {\n      cat(\".\")\n  }\n)\n\nhistory <- model %>% fit(\n  x = as.matrix(train_x),\n  y = as.matrix(train_y),\n  epochs = 100,\n  validation_split = 0.2,\n  batch_size = 32,\n  verbose = 2\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:37.677852Z","iopub.execute_input":"2022-07-13T18:18:37.679470Z","iopub.status.idle":"2022-07-13T18:18:47.294569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot( history )","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:47.298311Z","iopub.execute_input":"2022-07-13T18:18:47.300017Z","iopub.status.idle":"2022-07-13T18:18:47.857764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Another option to build your model\n\n#build_model <- function() {\n  ## Define the input (all 13 features)\n  #input <- layer_input( shape = c(train_d) )\n    \n  ## Here we define the sequence of layers that produces the output\n  #output <- input %>%\n  #  layer_dense(units = 64,  activation = \"relu\") %>%\n  #  layer_dense(units = 64,  activation = \"relu\") %>%\n  #  layer_dense(units = 1)\n    \n  ## Form the model\n  #regression_model <- keras_model(inputs = input, outputs = output)\n\n  #summary(regression_model)\n  \n  #regression_model %>% \n  #compile(\n  #  loss = \"mse\",\n  #  optimizer = optimizer_rmsprop(learning_rate = 1e-3),\n  #  metrics = list(\"mean_absolute_error\")\n  #)\n  \n  #regression_model\n#}\n\n#model <- build_model()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:13:28.525688Z","iopub.execute_input":"2022-07-13T18:13:28.527181Z","iopub.status.idle":"2022-07-13T18:13:28.548896Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x <- test_df[,features]\n\n\n# Street\ntest_x$Street[ test_x$Street == 'Grvl' ] <- 0\ntest_x$Street[ test_x$Street == 'Gravel' ] <- 0\ntest_x$Street[ test_x$Street == 'Pave' ] <- 1\ntest_x$Street[ test_x$Street == 'Paved' ] <- 1\n\n# Lot shape\ntest_x$LotShape[ test_x$LotShape == 'IR3' ] <- 0\ntest_x$LotShape[ test_x$LotShape == 'IR2' ] <- 0.33\ntest_x$LotShape[ test_x$LotShape == 'IR1' ] <- 0.67\ntest_x$LotShape[ test_x$LotShape == 'Reg' ] <- 1.0\n\n# ExterQual, ExterCond, HeatingQC\ntest_x[ test_x == 'Po'] <- 0\ntest_x[ test_x == 'Fa'] <- 0.25\ntest_x[ test_x == 'TA'] <- 0.5\ntest_x[ test_x == 'Gd'] <- 0.75\ntest_x[ test_x == 'Ex'] <- 1\n\n# Central Air\ntest_x$CentralAir[ test_x$CentralAir == 'N' ] <- 0\ntest_x$CentralAir[ test_x$CentralAir == 'Y' ] <- 1\n\n# to numerical\ntest_x = as.data.frame( sapply( test_x, as.numeric ) )\n\nprocess <- preProcess(test_x, method = c(\"range\") )\ntest_x <- predict(process, test_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:53.169644Z","iopub.execute_input":"2022-07-13T18:18:53.171133Z","iopub.status.idle":"2022-07-13T18:18:53.282197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions <- model %>% predict( as.matrix(test_x) )\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:57.263302Z","iopub.execute_input":"2022-07-13T18:18:57.264730Z","iopub.status.idle":"2022-07-13T18:18:57.451651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solution_df <- data.frame( test_df$Id, predictions ) %>% setNames( c(\"Id\", \"SalePrice\") )\n\n# price distribution\nggplot( solution_df, aes( x = SalePrice)) + \n  geom_histogram( binwidth = 5000, color = \"black\", fill=\"white\") +\n  geom_vline( xintercept = mean( solution_df$SalePrice ), color = 'red' ) +\n  ggtitle(\"Price distribution\") + \n  xlab(\"Sale Price\") + ylab(\"Count\") + \n  theme( title = element_text(size = 16),\n         axis.title.y = element_text(size = 11),\n         axis.text.y = element_text(size = 11),\n         axis.title.x = element_text(size = 11),\n         axis.text.x = element_text(size = 11),\n         plot.margin = margin(t = 5, r = 15, b = 5, l = 15),\n         plot.caption = element_text(size = 10),\n         aspect.ratio = 0.8)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:18:58.221814Z","iopub.execute_input":"2022-07-13T18:18:58.223367Z","iopub.status.idle":"2022-07-13T18:18:58.537089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if you want to submit your model to the competition\nhead(solution_df)\nwrite.csv( solution_df, 'submission.csv', row.names = FALSE )","metadata":{"execution":{"iopub.status.busy":"2022-07-13T18:19:01.766132Z","iopub.execute_input":"2022-07-13T18:19:01.767675Z","iopub.status.idle":"2022-07-13T18:19:01.795142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}