|
| 1 | +import streamlit as st |
| 2 | +import pandas as pd |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import seaborn as sb |
| 5 | +import plotly.express as px |
| 6 | + |
| 7 | +st.title("Predict Medical Charges") |
| 8 | +st.header("Machine Learning: Regression") |
| 9 | +st.write("Provided Medical Charges dataset, we shall predict price using Regression ML Algorithms") |
| 10 | + |
| 11 | +st.sidebar.markdown("[Anime Vyuh](https://animevyuh.org/)") |
| 12 | +st.sidebar.markdown("[Check Tutorial](https://animevyuh.org/build-machine-learning-model-using-streamlit/)") |
| 13 | + |
| 14 | +st.header("Analyse The Data") |
| 15 | +data = pd.read_csv('medical.csv') |
| 16 | +st.write("100 Sample Data:") |
| 17 | +st.dataframe(data.sample(100)) |
| 18 | + |
| 19 | +st.markdown("### Description Of Data") |
| 20 | +st.write(data.describe()) |
| 21 | + |
| 22 | +st.markdown("### Check for Empty Data") |
| 23 | +st.write(data.isnull().sum()) |
| 24 | + |
| 25 | +st.markdown("### Correlation Of Features Based on Label(charges)") |
| 26 | +st.write(data.corr()['charges'].sort_values()) |
| 27 | +st.write("Here age,bmi, children are numeric feature") |
| 28 | +st.write("We shall later encode the category(object) data type") |
| 29 | + |
| 30 | +st.header("Visualize The Data") |
| 31 | + |
| 32 | +fig = px.histogram(data,marginal='box',x="age",color="smoker") |
| 33 | +fig.update_layout(bargap=0.3) |
| 34 | +st.plotly_chart(fig) |
| 35 | + |
| 36 | +fig,axes = plt.subplots() |
| 37 | +sb.heatmap(data.corr(),annot=True) |
| 38 | +st.pyplot(fig) |
| 39 | + |
| 40 | +fig2,axes2 = plt.subplots() |
| 41 | +plt.pie(data['region'].value_counts(),labels=['southwest','southeast','northwest','northeast'],autopct="%0.2f%%",colors=["purple","blue",'green','red'],explode=[0.01,0.02,0,0]) |
| 42 | +st.pyplot(fig2) |
| 43 | + |
| 44 | +from sklearn.ensemble import AdaBoostRegressor |
| 45 | +from sklearn.model_selection import train_test_split |
| 46 | +from sklearn.preprocessing import LabelEncoder |
| 47 | +from sklearn.preprocessing import MinMaxScaler |
| 48 | +from sklearn.metrics import mean_absolute_error,mean_squared_error |
| 49 | + |
| 50 | +st.header("Prediction Of Our Model") |
| 51 | +features = ['age','bmi','smoker'] |
| 52 | +x = data[features] |
| 53 | + |
| 54 | +category = list() |
| 55 | +numerical = list() |
| 56 | +for features in x.columns: |
| 57 | + if x[features].dtype == 'O': |
| 58 | + category.append(features) |
| 59 | + else: |
| 60 | + numerical.append(features) |
| 61 | + |
| 62 | +scale = MinMaxScaler() |
| 63 | +x[numerical] = scale.fit_transform(x[numerical]) |
| 64 | + |
| 65 | +encode = LabelEncoder() |
| 66 | +x[category] = encode.fit_transform(x[category]) |
| 67 | + |
| 68 | +x = x[numerical+category] |
| 69 | +y = data['charges'] |
| 70 | + |
| 71 | +st.markdown("### After scaling, encoding and Feature Selection") |
| 72 | +st.markdown("#### Features") |
| 73 | +st.dataframe(x) |
| 74 | + |
| 75 | +st.markdown("#### Label") |
| 76 | +st.dataframe(y) |
| 77 | + |
| 78 | +x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=42) |
| 79 | + |
| 80 | +st.markdown("#### Change Learning Rate And Notice the Error And Score") |
| 81 | +lr = st.slider("Learning rate",min_value=0.03,max_value=0.1,step=0.01) |
| 82 | +model = AdaBoostRegressor(n_estimators=1000,random_state=42,learning_rate=lr) |
| 83 | +model = model.fit(x_train,y_train) |
| 84 | +predict = model.predict(x_test) |
| 85 | + |
| 86 | +st.write(f"MAE: {mean_absolute_error(predict,y_test)}") |
| 87 | +st.write(f"RMSE: {mean_squared_error(predict,y_test,squared=False)}") |
| 88 | +st.write(f"Score:{model.score(x_train,y_train)*100}%") |
| 89 | + |
| 90 | +st.markdown("<h1 style='text-align: center; color: red;'>Thank You</h1>", unsafe_allow_html=True) |
| 91 | +st.markdown("<h3 style='text-align: center; color:blue;'><a href='https://www.buymeacoffee.com/trjtarun'>Contribute</a></h3>", unsafe_allow_html=True) |
0 commit comments