demosobrevivir / app.py
cesar's picture
Update app.py
e803569 verified
# This is a small and fast sklearn model, so the run-gradio script trains a model and deploys it
import pandas as pd
import numpy as np
import sklearn
import gradio as gr
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Cargar los datos
data = pd.read_csv('https://raw.githubusercontent.com/gradio-app/titanic/master/train.csv')
data.head()
# Funci贸n para binning de edades
def encode_ages(df):
df.loc[:, 'Age'] = df['Age'].fillna(-0.5)
bins = (-1, 0, 5, 12, 18, 25, 35, 60, 120)
categories = pd.cut(df['Age'], bins, labels=False)
df.loc[:, 'Age'] = categories
return df
# Funci贸n para binning de tarifas
def encode_fares(df):
df.loc[:, 'Fare'] = df['Fare'].fillna(-0.5)
bins = (-1, 0, 8, 15, 31, 1000)
categories = pd.cut(df['Fare'], bins, labels=False)
df.loc[:, 'Fare'] = categories
return df
# Funci贸n para codificar el sexo
def encode_sex(df):
mapping = {"male": 0, "female": 1}
df.loc[:, 'Sex'] = df['Sex'].map(mapping)
return df
# Funci贸n para transformar todas las caracter铆sticas
def transform_features(df):
df = encode_ages(df)
df = encode_fares(df)
df = encode_sex(df)
return df
# Selecci贸n de columnas y transformaci贸n de datos
train = data[['PassengerId', 'Fare', 'Age', 'Sex', 'Survived']]
train = transform_features(train)
train.head()
# Separaci贸n en caracter铆sticas (X) y etiqueta (y)
X_all = train.drop(['Survived', 'PassengerId'], axis=1)
y_all = train['Survived']
# Divisi贸n en conjunto de entrenamiento y prueba
num_test = 0.20
X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test, random_state=23)
# Entrenamiento del modelo
clf = RandomForestClassifier()
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)
# Funci贸n de predicci贸n para Gradio
def predict_survival(sex, age, fare):
df = pd.DataFrame.from_dict({'Sex': [sex], 'Age': [age], 'Fare': [fare]})
df = encode_sex(df)
df = encode_fares(df)
df = encode_ages(df)
pred = clf.predict_proba(df)[0]
return {'Perishes': float(pred[0]), 'Survives': float(pred[1])}
# Definir la interfaz de Gradio
sex = gr.Radio(['female', 'male'], label="Sex", value="male")
age = gr.Slider(minimum=0, maximum=100, value=22, label="Age")
fare = gr.Slider(minimum=0, maximum=200, value=100, label="Fare (british pounds)")
gr.Interface(predict_survival, [sex, age, fare], "label", live=True, thumbnail="https://raw.githubusercontent.com/gradio-app/hub-titanic/master/thumbnail.png", analytics_enabled=False,
theme="soft", title="Demo Titanic", description="驴Cu谩l es la probabilidad de que un pasajero sobreviva?").launch();