Commit ·
d301534
1
Parent(s): 347fdb7
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,18 @@ from sklearn.preprocessing import LabelEncoder
|
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
-
|
| 8 |
# Load the data
|
| 9 |
df = pd.read_csv('insurance_data.csv')
|
| 10 |
|
| 11 |
# Convert categorical columns to numeric using Label Encoding
|
| 12 |
-
|
| 13 |
-
df['sex'] =
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Split the data into features (X) and target (y)
|
| 18 |
X = df.drop('expenses', axis=1)
|
|
@@ -33,9 +36,9 @@ def categorize_expense(expense):
|
|
| 33 |
|
| 34 |
# Define prediction function
|
| 35 |
def predict_risk(age, sex, bmi, children, smoker, region):
|
| 36 |
-
sex =
|
| 37 |
-
smoker =
|
| 38 |
-
region =
|
| 39 |
expense = model.predict(np.array([age, sex, bmi, children, smoker, region]).reshape(1, -1))[0]
|
| 40 |
return categorize_expense(expense)
|
| 41 |
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
|
|
|
|
| 7 |
# Load the data
|
| 8 |
df = pd.read_csv('insurance_data.csv')
|
| 9 |
|
| 10 |
# Convert categorical columns to numeric using Label Encoding
|
| 11 |
+
le_sex = LabelEncoder()
|
| 12 |
+
df['sex'] = le_sex.fit_transform(df['sex'])
|
| 13 |
+
|
| 14 |
+
le_smoker = LabelEncoder()
|
| 15 |
+
df['smoker'] = le_smoker.fit_transform(df['smoker'])
|
| 16 |
+
|
| 17 |
+
le_region = LabelEncoder()
|
| 18 |
+
df['region'] = le_region.fit_transform(df['region'])
|
| 19 |
|
| 20 |
# Split the data into features (X) and target (y)
|
| 21 |
X = df.drop('expenses', axis=1)
|
|
|
|
| 36 |
|
| 37 |
# Define prediction function
|
| 38 |
def predict_risk(age, sex, bmi, children, smoker, region):
|
| 39 |
+
sex = le_sex.transform([sex])[0] # encode 'sex'
|
| 40 |
+
smoker = le_smoker.transform([smoker])[0] # encode 'smoker'
|
| 41 |
+
region = le_region.transform([region])[0] # encode 'region'
|
| 42 |
expense = model.predict(np.array([age, sex, bmi, children, smoker, region]).reshape(1, -1))[0]
|
| 43 |
return categorize_expense(expense)
|
| 44 |
|