-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (42 loc) · 1.89 KB
/
app.py
File metadata and controls
54 lines (42 loc) · 1.89 KB
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
import streamlit as st
import pickle
import numpy as np
import pandas as pd
import sklearn
# Load the models
lr = pickle.load(open("lr.pkl", 'rb'))
dt = pickle.load(open("dt.pkl", 'rb'))
rf = pickle.load(open("rf.pkl", 'rb'))
# Streamlit app setup
st.title("Insurance Charge Prediction App")
st.header('Fill the Details to generate the Predicted Insurance Charges')
# Model selection
options = st.sidebar.selectbox('Select ML Model', ['Linear Regression', 'Decision Tree Regression', 'Random Forest Regression'])
# User input
age = st.slider('Age', 18, 64)
sex = st.selectbox('Sex', ['Male', 'Female'])
bmi = st.slider('BMI', 6, 53)
children = st.selectbox('Children', [0, 1, 2, 3, 4, 5])
smoker = st.selectbox('Smoker', ['Yes', 'No'])
region = st.selectbox('Region', ['North-West', 'South-East', 'South-West', 'North-East'])
# Prediction button
if st.button('Predict'):
# Convert categorical variables to numerical and one-hot encoding
sex = 1 if sex == 'Male' else 0
smoker = 1 if smoker == 'Yes' else 0
# One-hot encode region (3 binary features for 4 categories)
region_northwest = 1 if region == 'North-West' else 0
region_northeast = 1 if region == 'North-East' else 0
region_southeast = 1 if region == 'South-East' else 0
# Create the test array with the correct number of features
test = np.array([age, sex, bmi, children, smoker, region_northwest, region_northeast, region_southeast])
test = test.reshape(1, -1) # Use -1 to infer the correct number of columns
# Predict using the selected model
if options == 'Linear Regression':
prediction = lr.predict(test)[0]
elif options == 'Decision Tree Regression':
prediction = dt.predict(test)[0]
else:
prediction = rf.predict(test)[0]
# Display the prediction
st.success(f'The predicted insurance charge is: {prediction}')