-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboston_evaluation.py
92 lines (70 loc) · 2.85 KB
/
boston_evaluation.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
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
RM_IDX = 4
PTRATIO_IDX = 8
property_stats = features.mean().values.reshape(1, 11)
regr = LinearRegression().fit(features, target)
fitted_vals = regr.predict(features)
# MSE & RMSE
MSE = mean_squared_error(target, regr.predict(features))
RMSE = mean_squared_error(target, regr.predict(features), squared=False)
def get_log_estimate(nr_rooms,
students_per_classroom,
next_to_river=False,
high_confidence=False):
# Configure property
property_stats[0][RM_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 + RMSE
lower_bound = log_estimate - 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 next 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
ZILLOW_MEDIAN_PRICE = 583.3
SCALE_FACTOR = ZILLOW_MEDIAN_PRICE / np.median(boston_dataset.target)
log_est, upper, lower, conf = get_log_estimate(rm, ptratio, chas, large_range)
# Convert to todays dollars
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 values to nearest thousand
rounded_est = np.around(dollar_est, -3)
rounded_hi = np.around(dollar_hi, -3)
rounded_low = np.around(dollar_low, -3)
print(f"The estimated property value is {rounded_est}")
print(f"At {conf}% confidence the valuation range is")
print(f"USD {rounded_low} at the lower end to USD {rounded_hi} at the high end.")