Spaces:
Sleeping
Sleeping
File size: 1,480 Bytes
a822fa8 a2466a9 362698c a2466a9 42a186f a2466a9 430820d 68f838a a2466a9 42a186f a2466a9 362698c a2466a9 362698c a2466a9 362698c a2466a9 430820d a2466a9 430820d 362698c a2466a9 |
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 |
import pandas as pd
import numpy as np
import gradio as gr
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Load dataset
df = pd.read_csv("titanic.csv")
expected_cols = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Survived"]
available_cols = [col for col in expected_cols if col in df.columns]
df = df[available_cols].dropna()
# Encode 'Sex'
df["Sex"] = LabelEncoder().fit_transform(df["Sex"]) # male=1, female=0
# Features & target
X = df.drop("Survived", axis=1)
y = df["Survived"]
# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)
# Prediction function
def predict_survival(pclass, sex, age, fare):
sex_encoded = 1 if sex == "male" else 0
input_data = np.array([[pclass, sex_encoded, age, fare]])
prediction = model.predict(input_data)[0]
return "✅ Survived" if prediction == 1 else "❌ Did not survive"
# Gradio interface
iface = gr.Interface(
fn=predict_survival,
inputs=[
gr.Dropdown([1, 2, 3], label="Passenger Class"),
gr.Radio(["male", "female"], label="Sex"),
gr.Slider(0, 80, step=1, label="Age"),
gr.Slider(0, 500, step=1, label="Fare"),
],
outputs="text",
title="🚢 Titanic Survival Predictor",
description="Enter passenger details to predict their survival on the Titanic."
)
if __name__ == "__main__":
iface.launch()
|