# USAGE
# python mlp_regression.py --dataset Houses-dataset/Houses\ Dataset/

# The text version of the tutorial is available at the following address
# https://www.pyimagesearch.com/2019/01/21/regression-with-keras/

# This tutorial will show students how to perform regression using Keras and Deep Learning
# Most of the tutorials thus far have focused on classification and how to predict the
# correct classification. This tutorial focuses on how to train a Keras neural network for
# regression and continuous value prediction and it makes use of house prices as the example.
# Regression differs from classification as it allows prediction of continuous values in this
# case house prices. The latest state of the art deep learning networks make use of both the
# classification and regression methods in the calculations for example bounding box calculations
# could not be classification as it would involve a separate classification for every x, y value.
# In many different situations regression is the best technique to use in order to give more
# accurate predictions

# The dataset that is being used comes from the following url - https://github.com/emanhamed/Houses-dataset
# It includes both numerical/categorical attributes along with images for 535 data points which makes
# the dataset a very good one for studying. There are 4 numerical and catagorical attributes, number 
# of bedrooms, number of bathrooms, area (in terms of size) and zip code, labels for categorisation would
# be things such as cheap, affordable, apartment, house etc. The csv containing the dataset is more than
# a basic file as it contains images of homes, bedrooms etc. The houses dataset can be cloned down from
# GitHub using the following command - git clone https://github.com/emanhamed/Houses-dataset, each house
# will have 4 images per house kitchen, bedroom, bathroom and frontal, there is also a text file which
# matches text features to the images.

# The datasets.py file is used to load the dataset, in the file the minmaxscaler import is used to scale
# images to a value between 0 and 1 which has been the standard preprocessing procedure. When initialising
# the column names remember that there are potentially inifite combinations of numbers for bedrooms,
# bathrooms and area. 

# The models.py is where models are imported, there are only 2 used in this particular tutorial, dense and
# sequential, the others are imported with an eye on the other tutorials in the series. Linear activation
# is a standard approach when looking at regression, the single layer used represents the target value, in
# this case the house price. 


# import the necessary packages
# Adam optimiser trains the model
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from pyimagesearch import datasets
from pyimagesearch import models
import numpy as np
import argparse
# Displays nicely formatted prices
import locale
import os


# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", type=str, required=True,
	help="path to input dataset of house images")
args = vars(ap.parse_args())

# construct the path to the input .txt file that contains information
# on each house in the dataset and then load the dataset
print("[INFO] loading house attributes...")
inputPath = os.path.sep.join([args["dataset"], "HousesInfo.txt"])
df = datasets.load_house_attributes(inputPath)

# construct a training and testing split with 75% of the data used
# for training and the remaining 25% for evaluation
print("[INFO] constructing training/testing split...")
(train, test) = train_test_split(df, test_size=0.25, random_state=42)

# find the largest house price in the training set and use it to
# scale our house prices to the range [0, 1] (this will lead to
# better training and convergence)
maxPrice = train["price"].max()
trainY = train["price"] / maxPrice
testY = test["price"] / maxPrice

# process the house attributes data by performing min-max scaling
# on continuous features, one-hot encoding on categorical features,
# and then finally concatenating them together
print("[INFO] processing data...")
(trainX, testX) = datasets.process_house_attributes(df, train, test)

# create our MLP and then compile the model using mean absolute
# percentage error as our loss, implying that we seek to minimize
# the absolute percentage difference between our price *predictions*
# and the *actual prices*
model = models.create_mlp(trainX.shape[1], regress=True)
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)

# train the model
print("[INFO] training model...")
model.fit(x=trainX, y=trainY, 
	validation_data=(testX, testY),
	epochs=200, batch_size=8)

# make predictions on the testing data
print("[INFO] predicting house prices...")
preds = model.predict(testX)

# compute the difference between the *predicted* house prices and the
# *actual* house prices, then compute the percentage difference and
# the absolute percentage difference
diff = preds.flatten() - testY
percentDiff = (diff / testY) * 100
absPercentDiff = np.abs(percentDiff)

# compute the mean and standard deviation of the absolute percentage
# difference
mean = np.mean(absPercentDiff)
std = np.std(absPercentDiff)

# finally, show some statistics on our model
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
print("[INFO] avg. house price: {}, std house price: {}".format(
	locale.currency(df["price"].mean(), grouping=True),
	locale.currency(df["price"].std(), grouping=True)))
print("[INFO] mean: {:.2f}%, std: {:.2f}%".format(mean, std))
