crudcook/Medical_Insurance_Cost_Prediction
0
1import streamlit as st
2import pandas as pd
3import numpy as np
4from sklearn.model_selection import train_test_split
5from sklearn.preprocessing import StandardScaler, LabelEncoder
6from sklearn.ensemble import RandomForestRegressor
7from sklearn.metrics import mean_squared_error, r2_score
8from tensorflow.keras.models import Sequential
9from tensorflow.keras.layers import Dense, Dropout
10from tensorflow.keras.optimizers import Adam
11import matplotlib.pyplot as plt
12
13# Load and display dataset
14@st.cache_data
15def load_data():
16 data = pd.read_csv("insurance.csv") # Ensure insurance.csv is in the same directory
17 return data
18
19data = load_data()
20st.title("Medical Insurance Cost Prediction with Hybrid Model")
21st.write("Dataset preview:")
22st.write(data.head())
23
24# Preprocessing and Feature Engineering
25st.subheader("Data Preprocessing and Feature Engineering")
26data['age_smoker'] = data['age'] * data['smoker'].apply(lambda x: 1 if x == 'yes' else 0)
27data['bmi_smoker'] = data['bmi'] * data['smoker'].apply(lambda x: 1 if x == 'yes' else 0)
28
29# Encode categorical variables
30label_encoder = LabelEncoder()
31data['sex'] = label_encoder.fit_transform(data['sex'])
32data['smoker'] = label_encoder.fit_transform(data['smoker'])
33data['region'] = label_encoder.fit_transform(data['region'])
34
35# Select features
36X = data[['age', 'sex', 'bmi', 'children', 'smoker', 'region', 'age_smoker', 'bmi_smoker']]
37y = data['charges']
38
39# Standardize numerical features
40scaler = StandardScaler()
41X[['age', 'bmi', 'children', 'age_smoker', 'bmi_smoker']] = scaler.fit_transform(X[['age', 'bmi', 'children', 'age_smoker', 'bmi_smoker']])
42
43# Split data
44X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
45
46# Define the neural network model
47def create_neural_network():
48 model = Sequential([
49 Dense(128, activation='relu', input_shape=(X_train.shape[1],)),
50 Dropout(0.3),
51 Dense(64, activation='relu'),
52 Dense(1)
53 ])
54 model.compile(optimizer=Adam(learning_rate=0.001), loss='mse')
55 return model
56
57st.subheader("Training the Neural Network")
58nn_model = create_neural_network()
59nn_model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, verbose=1)
60
61# Generate predictions from the neural network for train and test sets
62nn_train_pred = nn_model.predict(X_train).flatten()
63nn_test_pred = nn_model.predict(X_test).flatten()
64
65# Add NN predictions as a new feature for Random Forest
66X_train_rf = X_train.copy()
67X_test_rf = X_test.copy()
68X_train_rf['nn_pred'] = nn_train_pred
69X_test_rf['nn_pred'] = nn_test_pred
70
71# Train a Random Forest on this new feature set
72st.subheader("Training the Random Forest with Neural Network Predictions")
73rf_model = RandomForestRegressor(n_estimators=200, max_depth=12, random_state=42)
74rf_model.fit(X_train_rf, y_train)
75final_predictions = rf_model.predict(X_test_rf)
76
77# Model evaluation
78rmse = np.sqrt(mean_squared_error(y_test, final_predictions))
79r2 = r2_score(y_test, final_predictions) * 100
80st.write(f"RMSE (Root Mean Squared Error): {rmse:.2f}")
81st.write(f"R² (Accuracy): {r2:.2f}%")
82
83# Plot actual vs predicted values
84st.subheader("Actual vs Predicted Values")
85plt.figure(figsize=(10, 5))
86plt.plot(y_test.values, label="Actual Values", color='blue')
87plt.plot(final_predictions, label="Predicted Values", color='orange')
88plt.xlabel("Sample Index")
89plt.ylabel("Insurance Charges")
90plt.legend()
91st.pyplot(plt)
92
93# Prediction on new data
94st.subheader("Predict on New Data")
95input_data = {col: st.number_input(f"Enter {col}:", value=float(X[col].mean())) for col in X.columns}
96
97if st.button("Predict Insurance Charge"):
98 input_df = pd.DataFrame([input_data])
99 input_df[['age', 'bmi', 'children', 'age_smoker', 'bmi_smoker']] = scaler.transform(
100 input_df[['age', 'bmi', 'children', 'age_smoker', 'bmi_smoker']])
101
102 # Use neural network to predict intermediate feature
103 nn_feature = nn_model.predict(input_df).flatten()
104 input_df['nn_pred'] = nn_feature
105
106 # Predict final charge using Random Forest
107 final_prediction = rf_model.predict(input_df)
108 st.write(f"Predicted Insurance Charge: ${final_prediction[0]:.2f}")
109 