File size: 4,653 Bytes
fb3bd84 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import streamlit as st
import joblib
model_path = 'Best_model.joblib'
loaded_model = joblib.load(model_path)
# Preprocess input function
def preprocess_input(input_data):
age = input_data['age']
bmi = input_data.get('bmi', None)
height = input_data.get('height', None)
weight = input_data.get('weight', None)
children = input_data['children']
# Convert height to meters based on the selected unit
height_unit = input_data.get('height_unit', 'meters')
if height is not None and height_unit != 'meters':
if height_unit == 'centimeters':
height /= 100
elif height_unit == 'feet':
height *= 0.3048 # 1 foot = 0.3048 meters
# Calculate BMI if height and weight are provided and height is not zero
if height is not None and height != 0 and weight is not None:
bmi = weight / (height ** 2)
# Convert sex to binary representation
sex_0 = 1 if input_data['sex'] == 'female' else 0
sex_1 = 1 - sex_0
# Convert smoker to binary representation
smoker_0 = 1 if input_data['smoker'] == 'no' else 0
smoker_1 = 1 - smoker_0
# Map region name to numerical representation
region_mapping = {'southeast': 1, 'southwest': 2, 'northwest': 3, 'northeast': 4}
region = region_mapping.get(input_data['region'], 0)
# Create binary representations for each region
region_1 = 1 if region == 1 else 0
region_2 = 1 if region == 2 else 0
region_3 = 1 if region == 3 else 0
region_4 = 1 if region == 4 else 0
# Create the formatted input list with 11 features
formatted_input = [age, bmi, children, sex_0, sex_1, region_1, region_2, region_3, region_4, smoker_0, smoker_1]
return formatted_input
# Input page
def input_page():
st.title('HealthInsure Claim Amount Predictor')
st.write('Please fill in the following details:')
age = None
height = None
weight = None
age_warning = ''
height_warning = ''
weight_warning = ''
age = st.number_input('Age', min_value=0, step=1, value=age)
if age == 0:
age_warning = 'Please enter correct age.'
st.warning(age_warning)
sex = st.radio('Sex', ('male', 'female'))
# Side-by-side input for height unit and height
col1, col2 = st.columns(2)
with col1:
height_unit = st.selectbox('Height Unit', ('meters', 'centimeters', 'feet'))
with col2:
height = st.number_input('Height', min_value=0.0, step=0.01, value=height)
if height == 0:
height_warning = 'Please enter correct height.'
st.warning(height_warning)
weight = st.number_input('Weight (in kg)', min_value=0.0, step=0.1, value=weight)
if weight == 0:
weight_warning = 'Please enter correct weight.'
st.warning(weight_warning)
# Calculate BMI immediately after entering height and weight if height is not zero
bmi = None
if height is not None and height != 0.0 and weight is not None:
# Convert height based on selected height unit
if height_unit != 'meters':
if height_unit == 'centimeters':
height /= 100
elif height_unit == 'feet':
height *= 0.3048 # 1 foot = 0.3048 meters
# Calculate BMI
bmi = weight / (height ** 2)
st.write(f'BMI: {bmi:.2f}')
children = st.number_input('Number of Children', min_value=0, step=1)
smoker = st.selectbox('Smoker', ('yes', 'no'))
region = st.selectbox('Region', ('southeast', 'southwest', 'northwest', 'northeast'))
if st.button('Predict'):
if age_warning or height_warning or weight_warning:
st.error('Please correct the following input errors:')
if age_warning:
st.error(age_warning)
if height_warning:
st.error(height_warning)
if weight_warning:
st.error(weight_warning)
else:
input_data = {'age': age, 'sex': sex, 'height': height, 'weight': weight, 'children': children,
'smoker': smoker, 'region': region, 'bmi': bmi, 'height_unit': height_unit}
processed_input = preprocess_input(input_data)
charges = loaded_model.predict([processed_input])[0]
st.write('## Estimated Claim Amount')
st.write(f'Estimated Claim Amount: {charges:.2f}', unsafe_allow_html=True)
st.write('The following value is estimated based on historical data and predictive modeling techniques and may not represent the exact amount.')
# Main function
def main():
input_page()
if __name__ == '__main__':
main()
|