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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 9 |
+
|
| 10 |
+
import tensorflow_probability as tfp
|
| 11 |
+
tfd = tfp.distributions
|
| 12 |
+
|
| 13 |
+
from pickle import dump
|
| 14 |
+
from pickle import load
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
scaler = MinMaxScaler()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# load trained model
|
| 22 |
+
lgbm_base = pickle.load(open('lgbm_base.pkl', 'rb'))
|
| 23 |
+
lgbm_opt = pickle.load(open('lgbm_optimized.pkl', 'rb'))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
tf.random.set_seed(42)
|
| 27 |
+
|
| 28 |
+
np.random.seed(42)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
st.markdown("<body style ='color:#E2E0D9;'></body>", unsafe_allow_html=True)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
st.markdown("<h4 style='text-align: center; color: #1B9E91;'>House Price Prediction in Ames,Iowa</h4>", unsafe_allow_html=True)
|
| 36 |
+
|
| 37 |
+
st.markdown("<h5 style='text-align: center; color: #1B9E91;'>A multi-step process is used to estimate the range of house prices based on your selection. </h5>", unsafe_allow_html=True)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
name_list = [
|
| 41 |
+
'OverallQual',
|
| 42 |
+
'YearBuilt',
|
| 43 |
+
'TotalBsmtSF',
|
| 44 |
+
'GrLivArea',
|
| 45 |
+
'MasVnrArea',
|
| 46 |
+
'BsmtFinType1',
|
| 47 |
+
'Neighborhood',
|
| 48 |
+
'GarageType',
|
| 49 |
+
'SaleCondition',
|
| 50 |
+
'BsmtExposure']
|
| 51 |
+
|
| 52 |
+
name_list_train = [
|
| 53 |
+
'OverallQual',
|
| 54 |
+
'YearBuilt',
|
| 55 |
+
'TotalBsmtSF',
|
| 56 |
+
'GrLivArea',
|
| 57 |
+
'MasVnrArea',
|
| 58 |
+
'BsmtFinType1',
|
| 59 |
+
'Neighborhood',
|
| 60 |
+
'GarageType',
|
| 61 |
+
'SaleCondition',
|
| 62 |
+
'BsmtExposure']
|
| 63 |
+
|
| 64 |
+
data = pd.read_csv('train.csv')
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
data = data[name_list_train].values
|
| 68 |
+
|
| 69 |
+
scaler.fit(data)
|
| 70 |
+
|
| 71 |
+
description_list = [
|
| 72 |
+
'What is the Overall material and finish quality?',
|
| 73 |
+
'In which year was the Original construction date?',
|
| 74 |
+
'What is the Total square feet of basement area?',
|
| 75 |
+
'What is the Above grade (ground) living area in square feet?',
|
| 76 |
+
'What is the Masonry veneer area in square feet??',
|
| 77 |
+
'What is the Quality of basement finished area?',
|
| 78 |
+
'Where is the physical locations within Ames city limits?',
|
| 79 |
+
'Where is the location of the Garage?',
|
| 80 |
+
'What is the condition of the sale?',
|
| 81 |
+
'Does the house have walkout or garden-level basement walls?'
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
min_list = [1.0,
|
| 85 |
+
1950.0,
|
| 86 |
+
0.0,
|
| 87 |
+
0.0,
|
| 88 |
+
334.0,
|
| 89 |
+
0.0,
|
| 90 |
+
0.0,
|
| 91 |
+
0.0,
|
| 92 |
+
0.0,
|
| 93 |
+
0.0
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
max_list = [
|
| 97 |
+
10.0,
|
| 98 |
+
2010.0,
|
| 99 |
+
2336.0,
|
| 100 |
+
6110.0,
|
| 101 |
+
4692.0,
|
| 102 |
+
10.0,
|
| 103 |
+
10.0,
|
| 104 |
+
3.0,
|
| 105 |
+
10.0,
|
| 106 |
+
1.0,
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
count = 0
|
| 110 |
+
|
| 111 |
+
with st.sidebar:
|
| 112 |
+
|
| 113 |
+
for i in range(len(name_list)):
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
variable_name = name_list[i]
|
| 118 |
+
globals()[variable_name] = st.slider(description_list[i] ,min_value=int(min_list[i]), max_value =int(max_list[i]),step=1)
|
| 119 |
+
|
| 120 |
+
st.write("[Kaggle Link to Data Set](https://www.kaggle.com/competitions/house-prices-advanced-regression-techniques)")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
data_df = {
|
| 126 |
+
|
| 127 |
+
'OverallQual': [OverallQual],
|
| 128 |
+
'YearBuilt': [YearBuilt],
|
| 129 |
+
'TotalBsmtSF': [TotalBsmtSF],
|
| 130 |
+
'GrLivArea':[GrLivArea],
|
| 131 |
+
'MasVnrArea': [MasVnrArea]
|
| 132 |
+
'BsmtFinType1': [BsmtFinType1]
|
| 133 |
+
'Neighborhood': [Neighborhood]
|
| 134 |
+
'GarageType': [GarageType]
|
| 135 |
+
'SaleCondition': [SaleCondition]
|
| 136 |
+
'BsmtExposure': [BsmtExposure]
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
#negloglik = lambda y, p_y: -p_y.log_prob(y) # note this
|
| 140 |
+
|
| 141 |
+
data_df = pd.DataFrame.from_dict(data_df)
|
| 142 |
+
|
| 143 |
+
data_df_normal = scaler.transform(data_df)
|
| 144 |
+
|
| 145 |
+
y_pred_base = lgbm_base.predict(data_df)
|
| 146 |
+
y_pred_optimized = lgbm_opt.predict(data_df)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
col1, col2, col3 , col4, col5 = st.columns(5)
|
| 151 |
+
|
| 152 |
+
with col1:
|
| 153 |
+
pass
|
| 154 |
+
with col2:
|
| 155 |
+
pass
|
| 156 |
+
with col4:
|
| 157 |
+
pass
|
| 158 |
+
with col5:
|
| 159 |
+
pass
|
| 160 |
+
with col3 :
|
| 161 |
+
center_button = st.button('Calculate range of house price')
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
if center_button:
|
| 166 |
+
|
| 167 |
+
import time
|
| 168 |
+
|
| 169 |
+
#my_bar = st.progress(0)
|
| 170 |
+
|
| 171 |
+
with st.spinner('Calculating....'):
|
| 172 |
+
time.sleep(2)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
st.markdown("<h5 style='text-align: center; color: #1B9E91;'>The price range of your house is between:</h5>", unsafe_allow_html=True)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
col1, col2 = st.columns([3, 3])
|
| 180 |
+
|
| 181 |
+
lower_number = "{:,.2f}".format(int(yhat.mean().numpy()-1.95*yhat.stddev().numpy()))
|
| 182 |
+
higher_number = "{:,.2f}".format(int(yhat.mean().numpy()+1.95*yhat.stddev().numpy()))
|
| 183 |
+
|
| 184 |
+
col1, col2, col3 = st.columns(3)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
with col1:
|
| 189 |
+
st.write("")
|
| 190 |
+
|
| 191 |
+
with col2:
|
| 192 |
+
st.subheader("USD "+ str(lower_number))
|
| 193 |
+
st.subheader(" AND ")
|
| 194 |
+
|
| 195 |
+
st.subheader(" USD "+str(higher_number))
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
with col3:
|
| 199 |
+
st.write("")
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
import base64
|
| 206 |
+
|
| 207 |
+
file_ = open("kramer_gif.gif", "rb")
|
| 208 |
+
contents = file_.read()
|
| 209 |
+
data_url = base64.b64encode(contents).decode("utf-8")
|
| 210 |
+
file_.close()
|
| 211 |
+
|
| 212 |
+
st.markdown(
|
| 213 |
+
f'<center><img src="data:image/gif;base64,{data_url}" alt="cat gif"></center>',
|
| 214 |
+
unsafe_allow_html=True,
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
|