In [4]:
import pandas as pd

datos = pd.read_csv("U4_04_train.csv")

datos.head()
Out[4]:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S
1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C
2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S
3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S
4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S
In [9]:
import seaborn as sb
# mostrar cuantas personas sobrevivieron y cuales no
conteo =sb.countplot(x="Survived" , data=datos)
Out[9]:
<Axes: xlabel='Survived', ylabel='count'>
No description has been provided for this image
In [11]:
conteo =sb.countplot(x="Survived" , data=datos ,  hue="Sex")
No description has been provided for this image
In [13]:
# datos vacios

datos.isna().sum()
Out[13]:
PassengerId      0
Survived         0
Pclass           0
Name             0
Sex              0
Age            177
SibSp            0
Parch            0
Ticket           0
Fare             0
Cabin          687
Embarked         2
dtype: int64
In [15]:
sb.displot(x="Age" , data=datos)
Out[15]:
<seaborn.axisgrid.FacetGrid at 0x7fcba1961030>
No description has been provided for this image
In [19]:
# proceso para llenar los 177 datos vacios de las edades

prom_edad = datos["Age"].mean()
In [21]:
datos["Age"] = datos["Age"].fillna(prom_edad)
In [43]:
datos.isna().sum()
Out[43]:
Survived    0
Pclass      0
Age         0
SibSp       0
Parch       0
Fare        0
male        0
Q           0
S           0
dtype: int64
In [26]:
# ELIMINACION DE CELDA Cabin

datos = datos.drop("Cabin" , axis=1)
In [27]:
datos["Embarked"].value_counts()
Out[27]:
S    644
C    168
Q     77
Name: Embarked, dtype: int64
In [28]:
# eliminando los valores Non A Number

datos = datos.dropna()
In [30]:
# eliminando columnas innecesarias

datos = datos.drop(["PassengerId" , "Ticket" , "Name"], axis=1)
In [37]:
# transformando los datos de sexo en datos numericos

dummies_sex = pd.get_dummies(datos["Sex"], drop_first=True)


# la linea anterior contenia una columna irrelevante para el analisis , se reemplazo por una sola en la cual indica 0 = hombre , 1 = mujer
# multicollinearity -> sucede cuando el modelo encuentra relaciones donde no deberia haber
In [38]:
# juntando set con los datos de sexo en numeros

datos = datos.join(dummies_sex)
datos = datos.drop(["Sex"] , axis=1)
datos
Out[38]:
Survived Pclass Age SibSp Parch Fare Embarked male
0 0 3 22.000000 1 0 7.2500 S 1
1 1 1 38.000000 1 0 71.2833 C 0
2 1 3 26.000000 0 0 7.9250 S 0
3 1 1 35.000000 1 0 53.1000 S 0
4 0 3 35.000000 0 0 8.0500 S 1
... ... ... ... ... ... ... ... ...
886 0 2 27.000000 0 0 13.0000 S 1
887 1 1 19.000000 0 0 30.0000 S 0
888 0 3 29.699118 1 2 23.4500 S 0
889 1 1 26.000000 0 0 30.0000 C 1
890 0 3 32.000000 0 0 7.7500 Q 1

889 rows × 8 columns

In [45]:
# verificando la relacion de sobrevivir con la embarked

sb.countplot(x="Survived" , hue="Embarked" , data=datos)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-22ecfc0a7b1c> in <cell line: 3>()
      1 # verificando la relacion de sobrevivir con la embarked
      2 
----> 3 sb.countplot(x="Survived" , hue="Embarked" , data=datos)

/usr/local/lib/python3.10/dist-packages/seaborn/categorical.py in countplot(data, x, y, hue, order, hue_order, orient, color, palette, saturation, width, dodge, ax, **kwargs)
   2941         raise ValueError("Cannot pass values for both `x` and `y`")
   2942 
-> 2943     plotter = _CountPlotter(
   2944         x, y, hue, data, order, hue_order,
   2945         estimator, errorbar, n_boot, units, seed,

/usr/local/lib/python3.10/dist-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, estimator, errorbar, n_boot, units, seed, orient, color, palette, saturation, width, errcolor, errwidth, capsize, dodge)
   1528                  errcolor, errwidth, capsize, dodge):
   1529         """Initialize the plotter."""
-> 1530         self.establish_variables(x, y, hue, data, orient,
   1531                                  order, hue_order, units)
   1532         self.establish_colors(color, palette, saturation)

/usr/local/lib/python3.10/dist-packages/seaborn/categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    539                 if isinstance(var, str):
    540                     err = f"Could not interpret input '{var}'"
--> 541                     raise ValueError(err)
    542 
    543             # Figure out the plotting orientation

ValueError: Could not interpret input 'Embarked'
In [44]:
# dummies de embarked
dummies_emb = pd.get_dummies(datos["Embarked"], drop_first=True)
# agregando al set de datos
datos = datos.join(dummies_emb)
datos = datos.drop(["Embarked"] , axis=1)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3801             try:
-> 3802                 return self._engine.get_loc(casted_key)
   3803             except KeyError as err:

/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Embarked'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-44-4309e26b5677> in <cell line: 2>()
      1 # dummies de embarked
----> 2 dummies_emb = pd.get_dummies(datos["Embarked"], drop_first=True)
      3 # agregando al set de datos
      4 datos = datos.join(dummies_emb)
      5 datos = datos.drop(["Embarked"] , axis=1)

/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py in __getitem__(self, key)
   3805             if self.columns.nlevels > 1:
   3806                 return self._getitem_multilevel(key)
-> 3807             indexer = self.columns.get_loc(key)
   3808             if is_integer(indexer):
   3809                 indexer = [indexer]

/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3802                 return self._engine.get_loc(casted_key)
   3803             except KeyError as err:
-> 3804                 raise KeyError(key) from err
   3805             except TypeError:
   3806                 # If we have a listlike key, _check_indexing_error will raise

KeyError: 'Embarked'
In [47]:
# heatmap
sb.heatmap(datos.corr(), annot=True , cmap="YlGnBu" )
Out[47]:
<Axes: >
No description has been provided for this image
In [48]:
sb.countplot(x="Survived" , data = datos , hue="Pclass")
Out[48]:
<Axes: xlabel='Survived', ylabel='count'>
No description has been provided for this image
In [49]:
# ENTRENAMIENTO

X= datos.drop(["Survived"] , axis=1)
y= datos["Survived"]
In [50]:
from sklearn.model_selection import train_test_split

X_ent , X_pru , y_ent , y_pru = train_test_split(X , y , test_size=0.2)
In [52]:
from sklearn.linear_model import LogisticRegression

modelo = LogisticRegression(max_iter=1000)

modelo.fit(X_ent , y_ent)
Out[52]:
LogisticRegression(max_iter=1000)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LogisticRegression(max_iter=1000)
In [53]:
# Predicciones

predicciones = modelo.predict(X_pru)
In [57]:
from sklearn.metrics import accuracy_score

accuracy_score(y_pru , predicciones)
Out[57]:
0.848314606741573
In [59]:
from sklearn.metrics import classification_report
print(classification_report(y_pru , predicciones))
              precision    recall  f1-score   support

           0       0.87      0.89      0.88       109
           1       0.82      0.78      0.80        69

    accuracy                           0.85       178
   macro avg       0.84      0.84      0.84       178
weighted avg       0.85      0.85      0.85       178

In [68]:
from sklearn.metrics import confusion_matrix
confusion_matrix(y_pru , predicciones)
#
Out[68]:
array([[97, 12],
       [15, 54]])
In [69]:
pd.DataFrame(confusion_matrix(y_pru , predicciones), columns=["Pred: No" , "Pred : Si"], index=["Real: No" , "Real: Si"])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-69-e47f2eb8d555> in <cell line: 1>()
----> 1 pd.DataFrame(confusion_matrix(y_pru , predicciones), columns=["Pred: No" , "Pred : Si"], index=["Real: No" , "Real: Si"])

TypeError: 'numpy.ndarray' object is not callable
In [70]:
X.head()
Out[70]:
Pclass Age SibSp Parch Fare male Q S
0 3 22.0 1 0 7.2500 1 0 1
1 1 38.0 1 0 71.2833 0 0 0
2 3 26.0 0 0 7.9250 0 0 1
3 1 35.0 1 0 53.1000 0 0 1
4 3 35.0 0 0 8.0500 1 0 1
In [74]:
from math import e
nueva_persona = [1 , 35 , 0 ,0 , 80 , 1 , 0 , 0]
prediccion = modelo.predict([nueva_persona])
if prediccion[0] == 1:
  print("Sobreviviste")
else:
  print("No Sobreviviste")
Sobreviviste
/usr/local/lib/python3.10/dist-packages/sklearn/base.py:439: UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names
  warnings.warn(
In [ ]: