-
Notifications
You must be signed in to change notification settings - Fork 0
/
boston_valuation.py
93 lines (73 loc) · 2.96 KB
/
boston_valuation.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
83
84
85
86
87
88
89
90
91
92
93
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import pandas as pd
import numpy as np
# Gather Data
boston_dataset = load_boston()
data = pd.DataFrame(data=boston_dataset.data,
columns=boston_dataset.feature_names)
features = data.drop(['INDUS', 'AGE'],axis=1)
log_prices = np.log(boston_dataset.target)
target = pd.DataFrame(log_prices, columns=['PRICE'])
CRIME_IDX = 0
ZN_IDX = 1
CHAS_IDX = 2
RN_IDX = 4
PTRATIO_IDX = 8
ZILLOW_MEDIAN_PRICE = 583.3
SCALE_FACTOR = ZILLOW_MEDIAN_PRICE/np.median(boston_dataset.target)
property_stats = features.mean().values.reshape(1,11)
regr = LinearRegression().fit(features, target)
fitted_vals = regr.predict(features)
# Calculate the MSE and RMSE using sklearn
MSE = mean_squared_error(target, fitted_vals)
RMSE = np.sqrt(MSE)
def get_log_estimate(nr_rooms,
students_per_classroom,
next_to_river=False,
high_confidence=True):
# Configure property
property_stats[0][RN_IDX] = nr_rooms
property_stats[0][PTRATIO_IDX] = students_per_classroom
if next_to_river:
property_stats[0][CHAS_IDX] = 1
else:
property_stats[0][CHAS_IDX] = 0
# make prediction
log_estimate = regr.predict(property_stats)[0][0]
# Calc Range
if high_confidence:
upper_bound = log_estimate + 2*RMSE
lower_bound = log_estimate - 2*RMSE
interval = 95
else:
upper_bound = log_estimate + 1*RMSE
lower_bound = log_estimate - 1*RMSE
interval = 68
return log_estimate, upper_bound, lower_bound, interval
def get_dollar_estimate(rm, ptratio, chas=False, large_range=True):
"""Estimate the price of a property in Boston
Keyword arguments:
rm -- number of rooms in the property
ptratio -- number of students per teacher in the classroom for the school in the area
chas -- True if the property is nexet to the river; false otherwise
large_range -- True for a 95% prediction interval, False for a 68% interval
"""
if rm < 1 or ptratio <1:
print('That is unrealistic. Try again')
return
log_est, upper, lower, conf = get_log_estimate(rm, students_per_classroom=ptratio,
next_to_river=chas,
high_confidence=large_range)
# convert to today's $$
dollar_est = np.e**log_est*1000*SCALE_FACTOR
dollar_hi = np.e**upper*1000*SCALE_FACTOR
dollar_low = np.e**lower*1000*SCALE_FACTOR
# round the dollar value to the neareset $000
rounded_est = np.around(dollar_est, -3)
rounded_high = np.around(dollar_hi, -3)
rounded_low = np.around(dollar_low, -3)
print(f'The estimated propery value is {rounded_est}.')
print(f'At {conf}% range the valuation range is:')
print(f'USD {rounded_low} at the lower end and {rounded_high} at the higher end')