-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANN.py
More file actions
99 lines (74 loc) · 2.41 KB
/
Copy pathANN.py
File metadata and controls
99 lines (74 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 26 10:09:39 2022
@author: mac1
"""
# Artificial Neural Network
# Importacion de Librerias
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
tf.__version__
# Part 1 - Pre-porcesamiento de datos
# importar set de datos
dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values
print(X)
print(y)
# Codificando dato categorico
# "Gender"
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X[:, 2] = le.fit_transform(X[:, 2])
print(X)
# Codificacion One Hot "Geography"
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
print(X)
# Scalado de variables
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
print(X)
# Separacion de set entrenamiento y prueba
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)
# Part 2 -Construccion ANN
# Inicializacion ANN
ann = tf.keras.models.Sequential()
# primera capa oculra
ann.add(tf.keras.layers.Dense(units=6, activation='relu'))
# segunda capa oculta
ann.add(tf.keras.layers.Dense(units=10, activation='relu'))
# Capa de Salida
ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
# Part 3 - Entrenamiento ANN
# Compilar ANN
ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Entrenamiento con set numero de muestras para actualizar pesos
ann.fit(X_train, y_train, batch_size =32 , epochs = 100)
# Part 4 - Clasificacion
# Prediccion con set de prueba
y_pred = ann.predict(X_test).round()
# Contruyendo Mztriz de Confusion
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
from sklearn import metrics
cm_display=metrics.ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=[False,True])
cm_display.plot()
plt.show()
from sklearn import metrics
fpr, tpr, _ = metrics.roc_curve(y_test, y_pred)
auc = metrics.roc_auc_score(y_test, y_pred)
plt.plot(fpr,tpr,label='AUR='+str(auc)) #% (auc))#label=r"AUC=")
plt.legend()
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.grid()
plt.show()