0% found this document useful (0 votes)
58 views

Clonamos El Repositorio para Obtener Los Dataset: From Import

The document describes steps to clone a GitHub repository containing deep learning data sets, mount a Google Drive to access data, install common Python packages like sklearn, Theano, TensorFlow and Keras for deep learning, and resolve any dependency issues that arise.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Clonamos El Repositorio para Obtener Los Dataset: From Import

The document describes steps to clone a GitHub repository containing deep learning data sets, mount a Google Drive to access data, install common Python packages like sklearn, Theano, TensorFlow and Keras for deep learning, and resolve any dependency issues that arise.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Clonamos el repositorio para obtener los

dataSet
!git clone https://github.com/joanby/deeplearning-az.git

Cloning into 'deeplearning-az'...


remote: Enumerating objects: 71, done.ote: Counting objects: 100%
(71/71), done.ote: Compressing objects: 100% (51/51), done.ote: Total
10167 (delta 35), reused 44 (delta 20), pack-reused 10096

Damos acceso a nuestro Drive


from google.colab import drive
drive.mount('/content/drive')

Test it
!ls '/content/drive/My Drive'

Google colab tools


from google.colab import files # Para manejar los archivos y, por
ejemplo, exportar a su navegador
import glob # Para manejar los archivos y, por ejemplo, exportar a su
navegador
from google.colab import drive # Montar tu Google drive

Instalar dependendias
!pip install sklearn

Requirement already satisfied: sklearn in


/usr/local/lib/python3.6/dist-packages (0.0)
Requirement already satisfied: scikit-learn in
/usr/local/lib/python3.6/dist-packages (from sklearn) (0.22.2.post1)
Requirement already satisfied: joblib>=0.11 in
/usr/local/lib/python3.6/dist-packages (from scikit-learn->sklearn)
(0.16.0)
Requirement already satisfied: scipy>=0.17.0 in
/usr/local/lib/python3.6/dist-packages (from scikit-learn->sklearn)
(1.4.1)
Requirement already satisfied: numpy>=1.11.0 in
/usr/local/lib/python3.6/dist-packages (from scikit-learn->sklearn)
(1.18.5)

Instalar Theano
!pip install --upgrade --no-deps
git+git://github.com/Theano/Theano.git

Collecting git+git://github.com/Theano/Theano.git
Cloning git://github.com/Theano/Theano.git to /tmp/pip-req-build-
7q89qzfc
Running command git clone -q git://github.com/Theano/Theano.git
/tmp/pip-req-build-7q89qzfc
Building wheels for collected packages: Theano
Building wheel for Theano (setup.py) ... e=Theano-
1.0.5+1.geb6a4125c-cp36-none-any.whl size=2668281
sha256=4d4c9648f72d9a1cfd6b33dea50b7685d703ab52da5c3aa3fe6ecd7dcad0604
8
Stored in directory:
/tmp/pip-ephem-wheel-cache-_6l9lz2i/wheels/ae/32/7c/62beb8371953eb20c2
71b3bac7d0e56e1a2020d46994346b52
Successfully built Theano
Installing collected packages: Theano
Found existing installation: Theano 1.0.5
Uninstalling Theano-1.0.5:
Successfully uninstalled Theano-1.0.5
Successfully installed Theano-1.0.5+1.geb6a4125c

Instalar Tensorflow y Keras


!pip install keras
!pip install tensorflow

Defaulting to user installation because normal site-packages is not


writeable
Looking in links: /usr/share/pip-wheels
Collecting keras
Downloading keras-2.14.0-py3-none-any.whl (1.7 MB)
al site-packages is not writeable
Looking in links: /usr/share/pip-wheels
Collecting tensorflow
Downloading tensorflow-2.13.0-cp39-cp39-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (524.1 MB)
IOPub data rate exceeded.
The Jupyter server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--ServerApp.iopub_data_rate_limit`.

Current values:
ServerApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
ServerApp.rate_limit_window=3.0 (secs)

ent already satisfied: h5py>=2.9.0 in /opt/conda/envs/anaconda-


2022.05-py39/lib/python3.9/site-packages (from tensorflow) (3.6.0)
Collecting astunparse>=1.6.0
Downloading astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting libclang>=13.0.0
Downloading libclang-16.0.6-py2.py3-none-manylinux2010_x86_64.whl
(22.9 MB)
ent already satisfied: packaging in /opt/conda/envs/anaconda-2022.05-
py39/lib/python3.9/site-packages (from tensorflow) (21.3)
Collecting protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!
=4.21.5,<5.0.0dev,>=3.20.3
Downloading protobuf-4.24.3-cp37-abi3-manylinux2014_x86_64.whl (311
kB)
>=0.23.1
Downloading tensorflow_io_gcs_filesystem-0.34.0-cp39-cp39-
manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.4 MB)
ent already satisfied: typing-extensions<4.6.0,>=3.6.6 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from tensorflow) (4.1.1)
Collecting termcolor>=1.1.0
Downloading termcolor-2.3.0-py3-none-any.whl (6.9 kB)
Collecting tensorboard<2.14,>=2.13
Downloading tensorboard-2.13.0-py3-none-any.whl (5.6 MB)
ent already satisfied: wrapt>=1.11.0 in /opt/conda/envs/anaconda-
2022.05-py39/lib/python3.9/site-packages (from tensorflow) (1.12.1)
Collecting tensorflow-estimator<2.14,>=2.13.0
Downloading tensorflow_estimator-2.13.0-py2.py3-none-any.whl (440
kB)
ent already satisfied: six>=1.12.0 in /opt/conda/envs/anaconda-
2022.05-py39/lib/python3.9/site-packages (from tensorflow) (1.16.0)
Collecting opt-einsum>=2.3.2
Downloading opt_einsum-3.3.0-py3-none-any.whl (65 kB)
ent already satisfied: setuptools in /opt/conda/envs/anaconda-2022.05-
py39/lib/python3.9/site-packages (from tensorflow) (61.2.0)
Collecting numpy<=1.24.3,>=1.22
Downloading numpy-1.24.3-cp39-cp39-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
ent already satisfied: grpcio<2.0,>=1.24.3 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from tensorflow) (1.42.0)
Collecting absl-py>=1.0.0
Downloading absl_py-2.0.0-py3-none-any.whl (130 kB)
ent already satisfied: wheel<1.0,>=0.23.0 in /opt/conda/envs/anaconda-
2022.05-py39/lib/python3.9/site-packages (from astunparse>=1.6.0-
>tensorflow) (0.37.1)
Collecting google-auth-oauthlib<1.1,>=0.5
Downloading google_auth_oauthlib-1.0.0-py2.py3-none-any.whl (18 kB)
Requirement already satisfied: werkzeug>=1.0.1 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from tensorboard<2.14,>=2.13->tensorflow) (2.0.3)
Requirement already satisfied: google-auth<3,>=1.6.3 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from tensorboard<2.14,>=2.13->tensorflow) (1.33.0)
Collecting tensorboard-data-server<0.8.0,>=0.7.0
Downloading tensorboard_data_server-0.7.1-py3-none-
manylinux2014_x86_64.whl (6.6 MB)
ent already satisfied: requests<3,>=2.21.0 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from tensorboard<2.14,>=2.13->tensorflow) (2.27.1)
Collecting grpcio<2.0,>=1.24.3
Downloading grpcio-1.58.0-cp39-cp39-
manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB)
ent already satisfied: markdown>=2.6.8 in /opt/conda/envs/anaconda-
2022.05-py39/lib/python3.9/site-packages (from
tensorboard<2.14,>=2.13->tensorflow) (3.3.4)
Requirement already satisfied: rsa<5,>=3.1.4 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from google-auth<3,>=1.6.3->tensorboard<2.14,>=2.13->tensorflow)
(4.7.2)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from google-auth<3,>=1.6.3->tensorboard<2.14,>=2.13->tensorflow)
(4.2.2)
Requirement already satisfied: pyasn1-modules>=0.2.1 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from google-auth<3,>=1.6.3->tensorboard<2.14,>=2.13->tensorflow)
(0.2.8)
Collecting google-auth<3,>=1.6.3
Downloading google_auth-2.23.0-py2.py3-none-any.whl (181 kB)
ent already satisfied: urllib3<2.0 in /opt/conda/envs/anaconda-
2022.05-py39/lib/python3.9/site-packages (from google-auth<3,>=1.6.3-
>tensorboard<2.14,>=2.13->tensorflow) (1.26.9)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3-
>tensorboard<2.14,>=2.13->tensorflow) (0.4.8)
Requirement already satisfied: idna<4,>=2.5 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from requests<3,>=2.21.0->tensorboard<2.14,>=2.13->tensorflow) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from requests<3,>=2.21.0->tensorboard<2.14,>=2.13->tensorflow)
(2021.10.8)
Requirement already satisfied: charset-normalizer~=2.0.0 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from requests<3,>=2.21.0->tensorboard<2.14,>=2.13->tensorflow)
(2.0.4)
Collecting oauthlib>=3.0.0
Downloading oauthlib-3.2.2-py3-none-any.whl (151 kB)
ent already satisfied: pyparsing!=3.0.5,>=2.0.2 in
/opt/conda/envs/anaconda-2022.05-py39/lib/python3.9/site-packages
(from packaging->tensorflow) (3.0.4)
Installing collected packages: oauthlib, requests-oauthlib, google-
auth, tensorboard-data-server, protobuf, numpy, grpcio, google-auth-
oauthlib, absl-py, termcolor, tensorflow-io-gcs-filesystem,
tensorflow-estimator, tensorboard, opt-einsum, libclang, keras,
google-pasta, gast, flatbuffers, astunparse, tensorflow
WARNING: The scripts f2py, f2py3 and f2py3.9 are installed in
'/home/7c0365f5-a4f8-4869-a872-d2a9ef2d66d7/.local/bin' which is not
on PATH.
Consider adding this directory to PATH or, if you prefer to suppress
this warning, use --no-warn-script-location.
WARNING: The script google-oauthlib-tool is installed in
'/home/7c0365f5-a4f8-4869-a872-d2a9ef2d66d7/.local/bin' which is not
on PATH.
Consider adding this directory to PATH or, if you prefer to suppress
this warning, use --no-warn-script-location.
WARNING: The script tensorboard is installed in '/home/7c0365f5-
a4f8-4869-a872-d2a9ef2d66d7/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress
this warning, use --no-warn-script-location.
Attempting uninstall: keras
Found existing installation: keras 2.14.0
Uninstalling keras-2.14.0:
Successfully uninstalled keras-2.14.0
WARNING: The scripts estimator_ckpt_converter,
import_pb_to_tensorboard, saved_model_cli, tensorboard, tf_upgrade_v2,
tflite_convert, toco and toco_from_protos are installed in
'/home/7c0365f5-a4f8-4869-a872-d2a9ef2d66d7/.local/bin' which is not
on PATH.
Consider adding this directory to PATH or, if you prefer to suppress
this warning, use --no-warn-script-location.
ERROR: pip's dependency resolver does not currently take into account
all the packages that are installed. This behaviour is the source of
the following dependency conflicts.
daal4py 2021.5.0 requires daal==2021.4.0, which is not installed.
scipy 1.7.3 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.24.3
which is incompatible.
numba 0.55.1 requires numpy<1.22,>=1.18, but you have numpy 1.24.3
which is incompatible.
google-cloud-storage 1.31.0 requires google-auth<2.0dev,>=1.11.0, but
you have google-auth 2.23.0 which is incompatible.
google-cloud-core 1.7.1 requires google-auth<2.0dev,>=1.24.0, but you
have google-auth 2.23.0 which is incompatible.
google-api-core 1.25.1 requires google-auth<2.0dev,>=1.21.1, but you
have google-auth 2.23.0 which is incompatible.
Successfully installed absl-py-2.0.0 astunparse-1.6.3 flatbuffers-
23.5.26 gast-0.4.0 google-auth-2.23.0 google-auth-oauthlib-1.0.0
google-pasta-0.2.0 grpcio-1.58.0 keras-2.13.1 libclang-16.0.6 numpy-
1.24.3 oauthlib-3.2.2 opt-einsum-3.3.0 protobuf-4.24.3 requests-
oauthlib-1.3.1 tensorboard-2.13.0 tensorboard-data-server-0.7.1
tensorflow-2.13.0 tensorflow-estimator-2.13.0 tensorflow-io-gcs-
filesystem-0.34.0 termcolor-2.3.0

Redes Neuronales Artificales

Cómo importar las librerías


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Matplotlib is building the font cache; this may take a moment.

Importar el data set


dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

dataset

RowNumber CustomerId Surname CreditScore Geography Gender


Age \
0 1 15634602 Hargrave 619 France Female
42
1 2 15647311 Hill 608 Spain Female
41
2 3 15619304 Onio 502 France Female
42
3 4 15701354 Boni 699 France Female
39
4 5 15737888 Mitchell 850 Spain Female
43
... ... ... ... ... ... ...
...
9995 9996 15606229 Obijiaku 771 France Male
39
9996 9997 15569892 Johnstone 516 France Male
35
9997 9998 15584532 Liu 709 France Female
36
9998 9999 15682355 Sabbatini 772 Germany Male
42
9999 10000 15628319 Walker 792 France Female
28

Tenure Balance NumOfProducts HasCrCard IsActiveMember \


0 2 0.00 1 1 1
1 1 83807.86 1 0 1
2 8 159660.80 3 1 0
3 1 0.00 2 0 0
4 2 125510.82 1 1 1
... ... ... ... ... ...
9995 5 0.00 2 1 0
9996 10 57369.61 1 1 1
9997 7 0.00 1 0 1
9998 3 75075.31 2 1 0
9999 4 130142.79 1 1 0

EstimatedSalary Exited
0 101348.88 1
1 112542.58 0
2 113931.57 1
3 93826.63 0
4 79084.10 0
... ... ...
9995 96270.64 0
9996 101699.77 0
9997 42085.58 1
9998 92888.52 1
9999 38190.78 0

[10000 rows x 14 columns]


Parte 1 - Pre procesado de datos

Codificar datos categóricos


from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])

labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])

onehotencoder = ColumnTransformer(
[('one_hot_encoder', OneHotEncoder(categories='auto'), [1])],
remainder='passthrough'
)

X = onehotencoder.fit_transform(X)
X = X[:, 1:]

array([[0.0, 0.0, 619, ..., 1, 1, 101348.88],


[0.0, 1.0, 608, ..., 0, 1, 112542.58],
[0.0, 0.0, 502, ..., 1, 0, 113931.57],
...,
[0.0, 0.0, 709, ..., 0, 1, 42085.58],
[1.0, 0.0, 772, ..., 1, 0, 92888.52],
[0.0, 0.0, 792, ..., 1, 0, 38190.78]], dtype=object)

Dividir el data set en conjunto de entrenamiento


y conjunto de testing
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
0.2, random_state = 0)

Escalado de variables
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
X_train.shape()

Parte 2 - Construir la RNA

Importar Keras y librerías adicionales


import keras
from keras.models import Sequential
from keras.layers import Dense

----------------------------------------------------------------------
-----
ModuleNotFoundError Traceback (most recent call
last)
Input In [18], in <cell line: 1>()
----> 1 import keras
2 from keras.models import Sequential
3 from keras.layers import Dense

ModuleNotFoundError: No module named 'keras'

Inicializar la RNA
classifier = Sequential()

Añadir las capas de entrada y primera capa


oculta
classifier.add(Dense(units = , kernel_initializer = "uniform",
activation = "", input_dim = ))

Añadir la segunda capa oculta


classifier.add(Dense(units =, kernel_initializer = "uniform",
activation = ""))
Añadir la capa de salida
classifier.add(Dense(units = , kernel_initializer = "uniform",
activation = "sigmoid"))

Compilar la RNA
classifier.compile(optimizer = "adam", loss = "binary_crossentropy",
metrics = ["accuracy"])

Ajustamos la RNA al Conjunto de


Entrenamiento

Parte 3 - Evaluar el modelo y calcular


predicciones finales
classifier.fit(X_train, y_train, batch_size = , epochs = )

Epoch 1/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4829
- accuracy: 0.7960
Epoch 2/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4262
- accuracy: 0.7960
Epoch 3/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4210
- accuracy: 0.8039
Epoch 4/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4173
- accuracy: 0.8249
Epoch 5/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4160
- accuracy: 0.8278
Epoch 6/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4140
- accuracy: 0.8313
Epoch 7/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4128
- accuracy: 0.8316
Epoch 8/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4114
- accuracy: 0.8320
Epoch 9/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4100
- accuracy: 0.8326
Epoch 10/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4090
- accuracy: 0.8339
Epoch 11/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4083
- accuracy: 0.8328
Epoch 12/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4070
- accuracy: 0.8335
Epoch 13/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4066
- accuracy: 0.8321
Epoch 14/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4062
- accuracy: 0.8340
Epoch 15/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4058
- accuracy: 0.8347
Epoch 16/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4049
- accuracy: 0.8332
Epoch 17/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4048
- accuracy: 0.8345
Epoch 18/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4048
- accuracy: 0.8336
Epoch 19/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4042
- accuracy: 0.8342
Epoch 20/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4040
- accuracy: 0.8355
Epoch 21/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4040
- accuracy: 0.8353
Epoch 22/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4035
- accuracy: 0.8346
Epoch 23/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4033
- accuracy: 0.8347
Epoch 24/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4035
- accuracy: 0.8335
Epoch 25/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4027
- accuracy: 0.8351
Epoch 26/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4032
- accuracy: 0.8339
Epoch 27/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4021
- accuracy: 0.8356
Epoch 28/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4021
- accuracy: 0.8340
Epoch 29/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4022
- accuracy: 0.8347
Epoch 30/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4021
- accuracy: 0.8340
Epoch 31/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4019
- accuracy: 0.8356
Epoch 32/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4019
- accuracy: 0.8366
Epoch 33/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4021
- accuracy: 0.8351
Epoch 34/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4017
- accuracy: 0.8347
Epoch 35/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4017
- accuracy: 0.8346
Epoch 36/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4017
- accuracy: 0.8361
Epoch 37/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4011
- accuracy: 0.8367
Epoch 38/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4016
- accuracy: 0.8359
Epoch 39/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4013
- accuracy: 0.8340
Epoch 40/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4009
- accuracy: 0.8340
Epoch 41/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4016
- accuracy: 0.8363
Epoch 42/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4012
- accuracy: 0.8344
Epoch 43/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4008
- accuracy: 0.8351
Epoch 44/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4006
- accuracy: 0.8342
Epoch 45/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4011
- accuracy: 0.8346
Epoch 46/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4009
- accuracy: 0.8361
Epoch 47/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4010
- accuracy: 0.8347
Epoch 48/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4005
- accuracy: 0.8357
Epoch 49/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4009
- accuracy: 0.8349
Epoch 50/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4006
- accuracy: 0.8375
Epoch 51/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4009
- accuracy: 0.8344
Epoch 52/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4002
- accuracy: 0.8355
Epoch 53/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4002
- accuracy: 0.8356
Epoch 54/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4007
- accuracy: 0.8339
Epoch 55/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4005
- accuracy: 0.8345
Epoch 56/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4006
- accuracy: 0.8359
Epoch 57/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4006
- accuracy: 0.8357
Epoch 58/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8350
Epoch 59/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8365
Epoch 60/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4001
- accuracy: 0.8367
Epoch 61/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4003
- accuracy: 0.8338
Epoch 62/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4002
- accuracy: 0.8361
Epoch 63/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8344
Epoch 64/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4004
- accuracy: 0.8366
Epoch 65/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3997
- accuracy: 0.8365
Epoch 66/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4006
- accuracy: 0.8360
Epoch 67/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4006
- accuracy: 0.8351
Epoch 68/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8355
Epoch 69/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4000
- accuracy: 0.8353
Epoch 70/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4007
- accuracy: 0.8351
Epoch 71/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3997
- accuracy: 0.8334
Epoch 72/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4007
- accuracy: 0.8360
Epoch 73/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8355
Epoch 74/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3997
- accuracy: 0.8346
Epoch 75/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4004
- accuracy: 0.8366
Epoch 76/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8355
Epoch 77/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4001
- accuracy: 0.8357
Epoch 78/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4005
- accuracy: 0.8353
Epoch 79/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3999
- accuracy: 0.8346
Epoch 80/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4005
- accuracy: 0.8359
Epoch 81/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4005
- accuracy: 0.8360
Epoch 82/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8353
Epoch 83/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3998
- accuracy: 0.8359
Epoch 84/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4001
- accuracy: 0.8349
Epoch 85/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4001
- accuracy: 0.8361
Epoch 86/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3999
- accuracy: 0.8357
Epoch 87/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4001
- accuracy: 0.8357
Epoch 88/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4002
- accuracy: 0.8356
Epoch 89/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4004
- accuracy: 0.8354
Epoch 90/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3999
- accuracy: 0.8364
Epoch 91/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4003
- accuracy: 0.8346
Epoch 92/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4000
- accuracy: 0.8365
Epoch 93/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3998
- accuracy: 0.8364
Epoch 94/100
800/800 [==============================] - 1s 2ms/step - loss: 0.4002
- accuracy: 0.8349
Epoch 95/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3996
- accuracy: 0.8381
Epoch 96/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3997
- accuracy: 0.8340
Epoch 97/100
800/800 [==============================] - 1s 2ms/step - loss: 0.3998
- accuracy: 0.8356
Epoch 98/100
800/800 [==============================] - 2s 2ms/step - loss: 0.3995
- accuracy: 0.8353
Epoch 99/100
800/800 [==============================] - 2s 2ms/step - loss: 0.4003
- accuracy: 0.8359
Epoch 100/100
800/800 [==============================] - 2s 2ms/step - loss: 0.3999
- accuracy: 0.8341

<tensorflow.python.keras.callbacks.History at 0x7f0eb0105630>

Predicción de los resultados con el Conjunto de


Testing
y_pred = classifier.predict(X_test)
y_pred = (y_pred>0.5)

Elaborar una matriz de confusión


from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
Parte 4 - Evaluar, mejorar y Ajustar la RNA
Evaluar la RNA
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score

def build_classifier():
classifier = Sequential()
classifier.add(Dense(units = 6, kernel_initializer = "uniform",
activation = "relu", input_dim = 11))
classifier.add(Dense(units = 6, kernel_initializer = "uniform",
activation = "relu"))
classifier.add(Dense(units = 1, kernel_initializer = "uniform",
activation = "sigmoid"))
classifier.compile(optimizer = "adam", loss = "binary_crossentropy",
metrics = ["accuracy"])
return classifier

classifier = KerasClassifier(build_fn = build_classifier, batch_size =


10, nb_epoch = 100)
accuracies = cross_val_score(estimator=classifier, X = X_train, y =
y_train, cv = 10, n_jobs=-1, verbose = 1)

[Parallel(n_jobs=-1)]: Using backend LokyBackend with 2 concurrent


workers.
[Parallel(n_jobs=-1)]: Done 10 out of 10 | elapsed: 18.9s finished

mean = accuracies.mean()
variance = accuracies.std()

Mejorar la RNA
Regularización de Dropout para evitar el overfitting

Ajustar la RNA
from sklearn.model_selection import GridSearchCV

def build_classifier(optimizer):
classifier = Sequential()
classifier.add(Dense(units = 6, kernel_initializer = "uniform",
activation = "relu", input_dim = 11))
classifier.add(Dense(units = 6, kernel_initializer = "uniform",
activation = "relu"))
classifier.add(Dense(units = 1, kernel_initializer = "uniform",
activation = "sigmoid"))
classifier.compile(optimizer = optimizer, loss =
"binary_crossentropy", metrics = ["accuracy"])
return classifier
classifier = KerasClassifier(build_fn = build_classifier)

parameters = {
'batch_size' : [25,32],
'nb_epoch' : [100, 500],
'optimizer' : ['adam', 'rmsprop']
}

grid_search = GridSearchCV(estimator = classifier,


param_grid = parameters,
scoring = 'accuracy',
cv = 10)
grid_search = grid_search.fit(X_train, y_train)

best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_

288/288 [==============================] - 0s 2ms/step - loss: 0.5595


- accuracy: 0.7960
WARNING:tensorflow:From
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrapper
s/scikit_learn.py:241: Sequential.predict_classes (from
tensorflow.python.keras.engine.sequential) is deprecated and will be
removed after 2021-01-01.
Instructions for updating:
Please use instead:* `np.argmax(model.predict(x), axis=-1)`, if your
model does multi-class classification (e.g. if it uses a `softmax`
last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,
if your model does binary classification (e.g. if it uses a
`sigmoid` last-layer activation).
288/288 [==============================] - 0s 2ms/step - loss: 0.5741
- accuracy: 0.7946
288/288 [==============================] - 0s 2ms/step - loss: 0.5798
- accuracy: 0.7933
288/288 [==============================] - 1s 2ms/step - loss: 0.5651
- accuracy: 0.7962
288/288 [==============================] - 0s 2ms/step - loss: 0.5633
- accuracy: 0.7937
288/288 [==============================] - 0s 2ms/step - loss: 0.5897
- accuracy: 0.7922
288/288 [==============================] - 1s 2ms/step - loss: 0.5586
- accuracy: 0.7965
288/288 [==============================] - 0s 2ms/step - loss: 0.5527
- accuracy: 0.7947
288/288 [==============================] - 1s 2ms/step - loss: 0.5689
- accuracy: 0.7949
288/288 [==============================] - 1s 2ms/step - loss: 0.5542
- accuracy: 0.7960
288/288 [==============================] - 1s 2ms/step - loss: 0.5835
- accuracy: 0.7967
288/288 [==============================] - 1s 2ms/step - loss: 0.6071
- accuracy: 0.7943
288/288 [==============================] - 1s 2ms/step - loss: 0.6058
- accuracy: 0.7942
288/288 [==============================] - 1s 2ms/step - loss: 0.5745
- accuracy: 0.7965
288/288 [==============================] - 1s 2ms/step - loss: 0.5673
- accuracy: 0.7929
288/288 [==============================] - 1s 2ms/step - loss: 0.6254
- accuracy: 0.7918
288/288 [==============================] - 1s 2ms/step - loss: 0.5645
- accuracy: 0.7965
288/288 [==============================] - 1s 2ms/step - loss: 0.5663
- accuracy: 0.7961
288/288 [==============================] - 1s 2ms/step - loss: 0.5845
- accuracy: 0.7956
288/288 [==============================] - 1s 2ms/step - loss: 0.5886
- accuracy: 0.7956
288/288 [==============================] - 0s 2ms/step - loss: 0.5763
- accuracy: 0.7961
288/288 [==============================] - 0s 2ms/step - loss: 0.5451
- accuracy: 0.7965
288/288 [==============================] - 0s 2ms/step - loss: 0.5629
- accuracy: 0.7951
288/288 [==============================] - 0s 2ms/step - loss: 0.5882
- accuracy: 0.7957
288/288 [==============================] - 0s 2ms/step - loss: 0.5617
- accuracy: 0.7922
288/288 [==============================] - 0s 2ms/step - loss: 0.5851
- accuracy: 0.7935
25/288 [=>............................] - ETA: 0s - loss: 0.6893 -
accuracy: 0.7776

----------------------------------------------------------------------
-----
KeyboardInterrupt Traceback (most recent call
last)
<ipython-input-39-f2d4fd603a69> in <module>()
3 scoring = 'accuracy',
4 cv = 10)
----> 5 grid_search = grid_search.fit(X_train, y_train)
6
7 best_parameters = grid_search.best_params_

/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_search
.py in fit(self, X, y, groups, **fit_params)
708 return results
709
--> 710 self._run_search(evaluate_candidates)
711
712 # For multi-metric evaluation, store the best_index_,
best_params_ and

/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_search
.py in _run_search(self, evaluate_candidates)
1149 def _run_search(self, evaluate_candidates):
1150 """Search all candidates in param_grid"""
-> 1151 evaluate_candidates(ParameterGrid(self.param_grid))
1152
1153

/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_search
.py in evaluate_candidates(candidate_params)
687 for parameters, (train, test)
688 in product(candidate_params,
--> 689 cv.split(X, y,
groups)))
690
691 if len(out) < 1:

/usr/local/lib/python3.6/dist-packages/joblib/parallel.py in
__call__(self, iterable)
1030 self._iterating = self._original_iterator is
not None
1031
-> 1032 while self.dispatch_one_batch(iterator):
1033 pass
1034

/usr/local/lib/python3.6/dist-packages/joblib/parallel.py in
dispatch_one_batch(self, iterator)
845 return False
846 else:
--> 847 self._dispatch(tasks)
848 return True
849

/usr/local/lib/python3.6/dist-packages/joblib/parallel.py in
_dispatch(self, batch)
763 with self._lock:
764 job_idx = len(self._jobs)
--> 765 job = self._backend.apply_async(batch,
callback=cb)
766 # A job can complete so quickly than its callback
is
767 # called before we get here, causing self._jobs to

/usr/local/lib/python3.6/dist-packages/joblib/_parallel_backends.py in
apply_async(self, func, callback)
206 def apply_async(self, func, callback=None):
207 """Schedule a func to be run"""
--> 208 result = ImmediateResult(func)
209 if callback:
210 callback(result)

/usr/local/lib/python3.6/dist-packages/joblib/_parallel_backends.py in
__init__(self, batch)
570 # Don't delay the application, to avoid keeping the
input
571 # arguments in memory
--> 572 self.results = batch()
573
574 def get(self):

/usr/local/lib/python3.6/dist-packages/joblib/parallel.py in
__call__(self)
251 with parallel_backend(self._backend,
n_jobs=self._n_jobs):
252 return [func(*args, **kwargs)
--> 253 for func, args, kwargs in self.items]
254
255 def __reduce__(self):

/usr/local/lib/python3.6/dist-packages/joblib/parallel.py in
<listcomp>(.0)
251 with parallel_backend(self._backend,
n_jobs=self._n_jobs):
252 return [func(*args, **kwargs)
--> 253 for func, args, kwargs in self.items]
254
255 def __reduce__(self):

/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_valida
tion.py in _fit_and_score(estimator, X, y, scorer, train, test,
verbose, parameters, fit_params, return_train_score,
return_parameters, return_n_test_samples, return_times,
return_estimator, error_score)
513 estimator.fit(X_train, **fit_params)
514 else:
--> 515 estimator.fit(X_train, y_train, **fit_params)
516
517 except Exception as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrapper
s/scikit_learn.py in fit(self, x, y, **kwargs)
221 raise ValueError('Invalid shape for y: ' + str(y.shape))
222 self.n_classes_ = len(self.classes_)
--> 223 return super(KerasClassifier, self).fit(x, y, **kwargs)
224
225 def predict(self, x, **kwargs):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrapper
s/scikit_learn.py in fit(self, x, y, **kwargs)
164 fit_args.update(kwargs)
165
--> 166 history = self.model.fit(x, y, **fit_args)
167
168 return history

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/
training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint:
disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/
training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks,
validation_split, validation_data, shuffle, class_weight,
sample_weight, initial_epoch, steps_per_epoch, validation_steps,
validation_batch_size, validation_freq, max_queue_size, workers,
use_multiprocessing)
1096 batch_size=batch_size):
1097 callbacks.on_train_batch_begin(step)
-> 1098 tmp_logs = train_function(iterator)
1099 if data_handler.should_sync:
1100 context.async_wait()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_fun
ction.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_fun
ction.py in _call(self, *args, **kwds)
805 # In this case we have created variables on the first
call, so we run the
806 # defunned version which is guaranteed to never create
variables.
--> 807 return self._stateless_fn(*args, **kwds) # pylint:
disable=not-callable
808 elif self._stateful_fn is not None:
809 # Release the lock early so that multiple threads can
perform the call
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/functio
n.py in __call__(self, *args, **kwargs)
2827 with self._lock:
2828 graph_function, args, kwargs =
self._maybe_define_function(args, kwargs)
-> 2829 return graph_function._filtered_call(args, kwargs) #
pylint: disable=protected-access
2830
2831 @property

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/functio
n.py in _filtered_call(self, args, kwargs, cancellation_manager)
1846
resource_variable_ops.BaseResourceVariable))],
1847 captured_inputs=self.captured_inputs,
-> 1848 cancellation_manager=cancellation_manager)
1849
1850 def _call_flat(self, args, captured_inputs,
cancellation_manager=None):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/functio
n.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1922 # No tape is watching; skip to running the function.
1923 return
self._build_call_outputs(self._inference_function.call(
-> 1924 ctx, args,
cancellation_manager=cancellation_manager))
1925 forward_backward =
self._select_forward_and_backward_functions(
1926 args,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/functio
n.py in call(self, ctx, args, cancellation_manager)
548 inputs=args,
549 attrs=attrs,
--> 550 ctx=ctx)
551 else:
552 outputs = execute.execute_with_cancellation(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute
.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle,
device_name, op_name,
---> 60 inputs, attrs,
num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:

KeyboardInterrupt:

You might also like