cesar commited on
Commit
e803569
verified
1 Parent(s): f08cabf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -9,48 +9,58 @@ from sklearn.model_selection import train_test_split
9
  from sklearn.ensemble import RandomForestClassifier
10
  from sklearn.metrics import accuracy_score
11
 
 
12
  data = pd.read_csv('https://raw.githubusercontent.com/gradio-app/titanic/master/train.csv')
13
  data.head()
14
 
15
- def encode_ages(df): # Binning ages
16
- df.Age = df.Age.fillna(-0.5)
 
17
  bins = (-1, 0, 5, 12, 18, 25, 35, 60, 120)
18
- categories = pd.cut(df.Age, bins, labels=False)
19
- df.Age = categories
20
  return df
21
 
22
- def encode_fares(df): # Binning fares
23
- df.Fare = df.Fare.fillna(-0.5)
 
24
  bins = (-1, 0, 8, 15, 31, 1000)
25
- categories = pd.cut(df.Fare, bins, labels=False)
26
- df.Fare = categories
27
  return df
28
 
 
29
  def encode_sex(df):
30
  mapping = {"male": 0, "female": 1}
31
- return df.replace({'Sex': mapping})
 
32
 
 
33
  def transform_features(df):
34
  df = encode_ages(df)
35
  df = encode_fares(df)
36
  df = encode_sex(df)
37
  return df
38
 
 
39
  train = data[['PassengerId', 'Fare', 'Age', 'Sex', 'Survived']]
40
  train = transform_features(train)
41
  train.head()
42
 
43
-
44
  X_all = train.drop(['Survived', 'PassengerId'], axis=1)
45
  y_all = train['Survived']
46
 
 
47
  num_test = 0.20
48
  X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test, random_state=23)
49
 
 
50
  clf = RandomForestClassifier()
51
  clf.fit(X_train, y_train)
52
  predictions = clf.predict(X_test)
53
 
 
54
  def predict_survival(sex, age, fare):
55
  df = pd.DataFrame.from_dict({'Sex': [sex], 'Age': [age], 'Fare': [fare]})
56
  df = encode_sex(df)
@@ -59,9 +69,10 @@ def predict_survival(sex, age, fare):
59
  pred = clf.predict_proba(df)[0]
60
  return {'Perishes': float(pred[0]), 'Survives': float(pred[1])}
61
 
 
62
  sex = gr.Radio(['female', 'male'], label="Sex", value="male")
63
- age = gr.Slider(minimum=0, maximum=100, default=22, label="Age")
64
- fare = gr.Slider(minimum=0, maximum=200, default=100, label="Fare (british pounds)")
65
 
66
  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,
67
- theme="soft", title="Demo Titanic", description="Cual es la probabilidad que un pasajero sobreviva.").launch();
 
9
  from sklearn.ensemble import RandomForestClassifier
10
  from sklearn.metrics import accuracy_score
11
 
12
+ # Cargar los datos
13
  data = pd.read_csv('https://raw.githubusercontent.com/gradio-app/titanic/master/train.csv')
14
  data.head()
15
 
16
+ # Funci贸n para binning de edades
17
+ def encode_ages(df):
18
+ df.loc[:, 'Age'] = df['Age'].fillna(-0.5)
19
  bins = (-1, 0, 5, 12, 18, 25, 35, 60, 120)
20
+ categories = pd.cut(df['Age'], bins, labels=False)
21
+ df.loc[:, 'Age'] = categories
22
  return df
23
 
24
+ # Funci贸n para binning de tarifas
25
+ def encode_fares(df):
26
+ df.loc[:, 'Fare'] = df['Fare'].fillna(-0.5)
27
  bins = (-1, 0, 8, 15, 31, 1000)
28
+ categories = pd.cut(df['Fare'], bins, labels=False)
29
+ df.loc[:, 'Fare'] = categories
30
  return df
31
 
32
+ # Funci贸n para codificar el sexo
33
  def encode_sex(df):
34
  mapping = {"male": 0, "female": 1}
35
+ df.loc[:, 'Sex'] = df['Sex'].map(mapping)
36
+ return df
37
 
38
+ # Funci贸n para transformar todas las caracter铆sticas
39
  def transform_features(df):
40
  df = encode_ages(df)
41
  df = encode_fares(df)
42
  df = encode_sex(df)
43
  return df
44
 
45
+ # Selecci贸n de columnas y transformaci贸n de datos
46
  train = data[['PassengerId', 'Fare', 'Age', 'Sex', 'Survived']]
47
  train = transform_features(train)
48
  train.head()
49
 
50
+ # Separaci贸n en caracter铆sticas (X) y etiqueta (y)
51
  X_all = train.drop(['Survived', 'PassengerId'], axis=1)
52
  y_all = train['Survived']
53
 
54
+ # Divisi贸n en conjunto de entrenamiento y prueba
55
  num_test = 0.20
56
  X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=num_test, random_state=23)
57
 
58
+ # Entrenamiento del modelo
59
  clf = RandomForestClassifier()
60
  clf.fit(X_train, y_train)
61
  predictions = clf.predict(X_test)
62
 
63
+ # Funci贸n de predicci贸n para Gradio
64
  def predict_survival(sex, age, fare):
65
  df = pd.DataFrame.from_dict({'Sex': [sex], 'Age': [age], 'Fare': [fare]})
66
  df = encode_sex(df)
 
69
  pred = clf.predict_proba(df)[0]
70
  return {'Perishes': float(pred[0]), 'Survives': float(pred[1])}
71
 
72
+ # Definir la interfaz de Gradio
73
  sex = gr.Radio(['female', 'male'], label="Sex", value="male")
74
+ age = gr.Slider(minimum=0, maximum=100, value=22, label="Age")
75
+ fare = gr.Slider(minimum=0, maximum=200, value=100, label="Fare (british pounds)")
76
 
77
  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,
78
+ theme="soft", title="Demo Titanic", description="驴Cu谩l es la probabilidad de que un pasajero sobreviva?").launch();