-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhousing_regress.py
82 lines (66 loc) · 2.57 KB
/
housing_regress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
QMIND - Google Street View Appraiser
Levi Stringer, Colin Cumming, Jacob Laframboise, Nick Merz
This file uses a multilayer perceptron on numerical house data
to estimate their value.
"""
# imports
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import numpy as np
import locale
import os
import matplotlib.pyplot as plt
# local folder imports
from pyimagesearch import attributeProcessing
from pyimagesearch import models
# paths
thisModulePath = os.path.abspath(__file__)
parentFolder = os.path.dirname(thisModulePath)
dataFolder = os.path.join(parentFolder, 'data')
inputPath = os.path.join(dataFolder, 'FINALDATASET.csv')
# load and preprocess data
print("[INFO] loading numerical house data...")
df = attributeProcessing.get_house_attributes(inputPath)
print("[INFO] processing data...")
(train, test) = train_test_split(df, test_size=0.25, random_state=42)
# normalize data
maxPrice = train["price"].max()
trainY = train["price"] / maxPrice
testY = test["price"] / maxPrice
(trainX, testX) = attributeProcessing.process_house_attributes(df, train, test)
# convert to numpy arrays from pandas Series
trainY = trainY.to_numpy()
testY = testY.to_numpy()
# create model and compile
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)
print("[INFO] training model...")
history = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=200, batch_size=8)
print("[INFO] predicting house prices...")
preds = model.predict(testX)
# graph loss over training
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Learning Curve')
plt.ylabel('Mean Absolute Percentage Error')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper right')
plt.show(block=True)
# 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))