-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (41 loc) · 2.05 KB
/
app.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
import streamlit as st
import pandas as pd
import pickle
# from sklearn.preprocessing import StandardScaler, OrdinalEncoder
def prepare_data(df1):
scaler = pickle.load(open('standard_scaler.pkl', 'rb'))
encoder = pickle.load(open('ordinal_encoder.pkl', 'rb'))
cat_cols = df1.columns[df1.dtypes == 'object']
num_cols = df1.columns[df1.dtypes != 'object']
num_features = df1[num_cols]
num_features = scaler.transform(num_features.values)
df1[num_cols] = num_features
cat_features = df1[cat_cols]
cat_features = encoder.transform(cat_features.values)
df1[cat_cols] = cat_features
return df
model = pickle.load(open('model.pkl', 'rb'))
st.title('Diamond Price Predictor')
carat = st.number_input("Carat", value=None, placeholder="Type a number")
cut = st.selectbox("Cut", options=("Fair", "Good", "Ideal", "Premium", "Very Good"), index=None,
placeholder="Choose an option")
color = st.selectbox("Color", options=("D", "E", "F", "G", "H", "I", "J"), index=None, placeholder="Choose an option")
clarity = st.selectbox("Clarity", options=("I1", "IF", "SI1", "SI2", "VS1", "VS2", "VVS1", "VVS2"),
index=None, placeholder="Choose an option")
depth = st.number_input("Depth", value=None, placeholder="Type a number")
table = st.number_input("Table", value=None, placeholder="Type a number")
x = st.number_input("x", value=None, placeholder="Type a number")
y = st.number_input("y", value=None, placeholder="Type a number")
z = st.number_input("z", value=None, placeholder="Type a number")
data = [[carat, cut, color, clarity, depth, table, x, y, z]]
df = pd.DataFrame(data, columns=['carat', 'cut', 'color', 'clarity', 'depth', 'table', 'x', 'y', 'z'])
if st.button('Predict'):
# preprocess
df = prepare_data(df)
# predict
result = model.predict(df)
# display
st.metric(label="Predicted Price (in USD) ", value=result)
def reset():
st.session_state.selection = 'Please Select'
st.button('Reset', on_click=reset)