Last active
September 14, 2022 14:38
-
-
Save statcompute/0cb6b94c7fb65f0ed20adcb05f0dd7ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy.random import seed | |
from pandas import read_csv, DataFrame | |
from sklearn.preprocessing import scale | |
from keras.layers.convolutional import Conv1D, MaxPooling1D | |
from keras.layers.merge import average | |
from keras.layers import Input, Dense, Flatten, Reshape, Dropout, SpatialDropout1D | |
from keras.models import Model | |
from keras.optimizers import SGD | |
from keras.utils import plot_model | |
df = read_csv("credit_count.txt") | |
Y = df[df.CARDHLDR == 1].DEFAULT | |
X = scale(df[df.CARDHLDR == 1].iloc[:, 2:12]) | |
D = 0.2 | |
S = 1 | |
seed(S) | |
### INPUT DATA | |
inputs = Input(shape = (X.shape[1],)) | |
### DEFINE A MULTILAYER PERCEPTRON NETWORK | |
mlp_net = Dense(64, activation = 'relu', kernel_initializer = 'he_uniform')(inputs) | |
mlp_net = Dropout(rate = D, seed = S)(mlp_net) | |
mlp_net = Dense(64, activation = 'relu', kernel_initializer = 'he_uniform')(mlp_net) | |
mlp_net = Dropout(rate = D, seed = S)(mlp_net) | |
mlp_out = Dense(1, activation = 'sigmoid')(mlp_net) | |
mlp_mdl = Model(inputs = inputs, outputs = mlp_out) | |
### DEFINE A CONVOLUTIONAL NETWORK | |
cnv_net = Reshape((X.shape[1], 1))(inputs) | |
cnv_net = Conv1D(32, 4, activation = 'relu', padding = "same", kernel_initializer = 'he_uniform')(cnv_net) | |
cnv_net = MaxPooling1D(2)(cnv_net) | |
cnv_net = SpatialDropout1D(D)(cnv_net) | |
cnv_net = Flatten()(cnv_net) | |
cnv_out = Dense(1, activation = 'sigmoid')(cnv_net) | |
cnv_mdl = Model(inputs = inputs, outputs = cnv_out) | |
### COMBINE MLP AND CNV | |
con_out = average([mlp_out, cnv_out]) | |
con_mdl = Model(inputs = inputs, outputs = con_out) | |
sgd = SGD(lr = 0.1, momentum = 0.9) | |
con_mdl.compile(optimizer = sgd, loss = 'binary_crossentropy', metrics = ['binary_accuracy']) | |
con_mdl.fit(X, Y, batch_size = 2000, epochs = 50, verbose = 0) | |
plot_model(con_mdl, to_file = 'model.png', show_shapes = True, show_layer_names = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment