mjaramillo commited on
Commit
7226e13
1 Parent(s): 0c2752c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import sklearn
4
+ import gradio as gr
5
+ from sklearn import preprocessing
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.ensemble import RandomForestClassifier
8
+ from sklearn.metrics import accuracy_score
9
+
10
+ data = pd.read_csv('https://raw.githubusercontent.com/gradio-app/titanic/master/train.csv')
11
+ data.head()
12
+
13
+ def encode_ages(df): # Binning ages
14
+ df.Age = df.Age.fillna(-0.5)
15
+ bins = (-1, 0, 5, 12, 18, 25, 35, 60, 120)
16
+ categories = pd.cut(df.Age, bins, labels=False)
17
+ df.Age = categories
18
+ return df
19
+
20
+ def encode_fares(df): # Binning fares
21
+ df.Fare = df.Fare.fillna(-0.5)
22
+ bins = (-1, 0, 8, 15, 31, 1000)
23
+ categories = pd.cut(df.Fare, bins, labels=False)
24
+ df.Fare = categories
25
+ return df
26
+
27
+ def encode_sex(df):
28
+ mapping = {"male": 0, "female": 1}
29
+ return df.replace({'Sex': mapping})
30
+
31
+ def transform_features(df):
32
+ df = encode_ages(df)
33
+ df = encode_fares(df)
34
+ df = encode_sex(df)
35
+ return df
36
+
37
+ train = data[['PassengerId', 'Fare', 'Age', 'Sex', 'Survived']]
38
+ train = transform_features(train)
39
+ train.head()
40
+
41
+
42
+ X_all = train.drop(['Survived', 'PassengerId'], axis=1)
43
+ y_all = train['Survived']
44
+
45
+ num_test = 0.20
46
+ X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test, random_state=23)
47
+
48
+ clf = RandomForestClassifier()
49
+ clf.fit(X_train, y_train)
50
+ predictions = clf.predict(X_test)
51
+
52
+ def predict_survival(sex, age, fare):
53
+ df = pd.DataFrame.from_dict({'Sex': [sex], 'Age': [age], 'Fare': [fare]})
54
+ df = encode_sex(df)
55
+ df = encode_fares(df)
56
+ df = encode_ages(df)
57
+ pred = clf.predict_proba(df)[0]
58
+ return {'Muere': float(pred[0]), 'Sobrevive': float(pred[1])}
59
+
60
+ sex = gr.inputs.Radio(['Mujer', 'Varon'], label="Genero")
61
+ age = gr.inputs.Slider(minimum=0, maximum=120, default=22, label="Edad")
62
+ fare = gr.inputs.Slider(minimum=0, maximum=200, default=100, label="Tarifa (british pounds)")
63
+
64
+ 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,
65
+ title="Sobreviviendo al Titanic", description="Cual es la probabilidad de sobrevivir al famoso accidente del titanic? Depende de indicadores demograficos como se muestra a continuacion.").launch();