|
import json |
|
import requests |
|
from misc import nearest_mrt |
|
import pickle |
|
import os |
|
import pandas as pd |
|
|
|
|
|
|
|
|
|
def main_fn(Postal_,age_,town_,storey_,room_): |
|
|
|
filename = 'finalized_model.sav' |
|
|
|
if os.path.exists("./finalized_model.sav"): |
|
model = pickle.load(open(filename, 'rb')) |
|
print('loaded model') |
|
else: |
|
print('failed loading model') |
|
|
|
|
|
feature_names = model.feature_names |
|
input = [0]*len(feature_names) |
|
|
|
|
|
|
|
mrt_name = [] |
|
mrt_loc = [] |
|
with open('data/mrt_list.json', 'r') as file: |
|
for line in file: |
|
item = json.loads(line) |
|
mrt_name.append(item['MRT']) |
|
loc = tuple([float(i) for i in item['location']]) |
|
mrt_loc.append(loc) |
|
|
|
|
|
|
|
|
|
Postal_input = Postal_ |
|
|
|
input[feature_names.index('Postal')] = int(Postal_input) |
|
|
|
|
|
search_term = Postal_input |
|
query_string='https://developers.onemap.sg/commonapi/search?searchVal={}&returnGeom=Y&getAddrDetails=Y&pageNum=1'.format(search_term) |
|
resp = requests.get(query_string) |
|
data = json.loads(resp.content) |
|
chosen_result = data['results'][0] |
|
|
|
|
|
distance_km, nearest_mr = nearest_mrt(chosen_result['LATITUDE'], chosen_result['LONGITUDE'], mrt_name, mrt_loc) |
|
input[feature_names.index('distance_mrt')] = distance_km |
|
|
|
|
|
|
|
height_input = storey_ |
|
|
|
Height = (height_input+2)//3 |
|
input[feature_names.index('storey_height')] = Height |
|
|
|
|
|
town_input = town_ |
|
|
|
input[feature_names.index("town_"+town_input)] = 1 |
|
|
|
|
|
room_input = room_ |
|
|
|
input[feature_names.index("flat_num_"+room_input)] = 1 |
|
|
|
|
|
age_input = age_ |
|
|
|
input[feature_names.index('age_transation')] = age_input |
|
input[feature_names.index('transaction_yr')] = 2022 |
|
|
|
|
|
|
|
Actual = dict(zip(feature_names,input)) |
|
Actual_df = pd.DataFrame(Actual, index=[0]) |
|
|
|
resale_adj_price = model.predict(Actual_df)[0] |
|
|
|
|
|
resale_index = 170 |
|
price = resale_adj_price*resale_index/133.9 |
|
print(Actual_df) |
|
|
|
return price |
|
|
|
if __name__ == "__main__": |
|
Postal_,age_,town_,storey_,room_ = 680705, 30, 'CHOA CHU KANG', 12, '5 ROOM' |
|
price = main_fn(Postal_,age_,town_,storey_,room_) |
|
print(price) |