Update BaseModel.py
Browse files- BaseModel.py +31 -11
BaseModel.py
CHANGED
|
@@ -1,13 +1,35 @@
|
|
| 1 |
-
from typing import
|
| 2 |
import datetime
|
| 3 |
|
| 4 |
-
class BaseModel:
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def __init__(self):
|
| 8 |
self._load_model()
|
| 9 |
|
| 10 |
-
|
| 11 |
def _load_model(self):
|
| 12 |
"""
|
| 13 |
Perform any actions needed to load the model.
|
|
@@ -17,30 +39,28 @@ class BaseModel:
|
|
| 17 |
# Optional model loading
|
| 18 |
print("Model loaded.")
|
| 19 |
|
| 20 |
-
|
| 21 |
-
def _sale_date_predictor(self, daysOnMarket: int):
|
| 22 |
"""
|
| 23 |
Calculate the expected sale date based on the national average
|
| 24 |
-
:param
|
| 25 |
:return: the predicted sale date, based on the national average of 34 days
|
| 26 |
"""
|
| 27 |
national_average = 34
|
| 28 |
-
if
|
| 29 |
-
days_until_sale = national_average -
|
| 30 |
sale_date = datetime.date.today() + datetime.timedelta(days=days_until_sale)
|
| 31 |
return sale_date
|
| 32 |
else:
|
| 33 |
return datetime.date.today() + datetime.timedelta(days=1)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
def run_inference(self, input_data: dict[str, Union[str, int, float]]) -> Tuple[float, str]:
|
| 37 |
"""
|
| 38 |
Predict the sale price and sale date for the house represented by `input_data`
|
| 39 |
:param input_data: a formatted Synapse from the validator, representing a currently listed house
|
| 40 |
:return: the predicted sale price and predicted sale date for this home
|
| 41 |
"""
|
| 42 |
predicted_sale_price = float(input_data['price']) if ('price' in input_data) else 1.0
|
| 43 |
-
predicted_sale_date = self._sale_date_predictor(input_data['
|
| 44 |
predicted_sale_date = predicted_sale_date.strftime("%Y-%m-%d")
|
| 45 |
print(f"Predicted sale price: {predicted_sale_price}")
|
| 46 |
print(f"Predicted sale date: {predicted_sale_date}")
|
|
|
|
| 1 |
+
from typing import Tuple, TypedDict, Optional
|
| 2 |
import datetime
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
+
class ProcessedSynapse(TypedDict):
|
| 6 |
+
id: Optional[str]
|
| 7 |
+
property_id: Optional[str]
|
| 8 |
+
listing_id: Optional[str]
|
| 9 |
+
address: Optional[str]
|
| 10 |
+
city: Optional[str]
|
| 11 |
+
state: Optional[str]
|
| 12 |
+
zip: Optional[str]
|
| 13 |
+
price: Optional[float]
|
| 14 |
+
beds: Optional[int]
|
| 15 |
+
baths: Optional[float]
|
| 16 |
+
sqft: Optional[int]
|
| 17 |
+
lot_size: Optional[int]
|
| 18 |
+
year_built: Optional[int]
|
| 19 |
+
days_on_market: Optional[int]
|
| 20 |
+
latitude: Optional[float]
|
| 21 |
+
longitude: Optional[float]
|
| 22 |
+
property_type: Optional[str]
|
| 23 |
+
last_sale_date: Optional[str]
|
| 24 |
+
hoa_dues: Optional[float]
|
| 25 |
+
query_date: Optional[str]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class BaseModel:
|
| 29 |
|
| 30 |
def __init__(self):
|
| 31 |
self._load_model()
|
| 32 |
|
|
|
|
| 33 |
def _load_model(self):
|
| 34 |
"""
|
| 35 |
Perform any actions needed to load the model.
|
|
|
|
| 39 |
# Optional model loading
|
| 40 |
print("Model loaded.")
|
| 41 |
|
| 42 |
+
def _sale_date_predictor(self, days_on_market: int):
|
|
|
|
| 43 |
"""
|
| 44 |
Calculate the expected sale date based on the national average
|
| 45 |
+
:param days_on_market: number of days this house has been on the market
|
| 46 |
:return: the predicted sale date, based on the national average of 34 days
|
| 47 |
"""
|
| 48 |
national_average = 34
|
| 49 |
+
if days_on_market < national_average:
|
| 50 |
+
days_until_sale = national_average - days_on_market
|
| 51 |
sale_date = datetime.date.today() + datetime.timedelta(days=days_until_sale)
|
| 52 |
return sale_date
|
| 53 |
else:
|
| 54 |
return datetime.date.today() + datetime.timedelta(days=1)
|
| 55 |
|
| 56 |
+
def run_inference(self, input_data: ProcessedSynapse) -> Tuple[float, str]:
|
|
|
|
| 57 |
"""
|
| 58 |
Predict the sale price and sale date for the house represented by `input_data`
|
| 59 |
:param input_data: a formatted Synapse from the validator, representing a currently listed house
|
| 60 |
:return: the predicted sale price and predicted sale date for this home
|
| 61 |
"""
|
| 62 |
predicted_sale_price = float(input_data['price']) if ('price' in input_data) else 1.0
|
| 63 |
+
predicted_sale_date = self._sale_date_predictor(input_data['days_on_market']) if ('days_on_market' in input_data) else datetime.date.today() + datetime.timedelta(days=1)
|
| 64 |
predicted_sale_date = predicted_sale_date.strftime("%Y-%m-%d")
|
| 65 |
print(f"Predicted sale price: {predicted_sale_price}")
|
| 66 |
print(f"Predicted sale date: {predicted_sale_date}")
|