-
Notifications
You must be signed in to change notification settings - Fork 0
/
spam_functions.py
57 lines (51 loc) · 1.83 KB
/
spam_functions.py
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
# -*- coding: utf-8 -*-
"""
Benjamin H Pepper
BHPepper@gmail.com
linkedin.com/in/BHPepper
"""
import pandas as pd
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.utils import to_categorical
#from keras.constraints import maxnorm
#from tensorflow.keras import regularizers
#from tensorflow.keras import optimizers
from sklearn.metrics import accuracy_score
from tensorflow.keras.callbacks import TensorBoard
import tensorflow as tf
def get_spam_dat():
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data"
dat = pd.read_csv(url, sep=',', header=None)
return(dat)
def nn_mod(X, y, seed = 1, params = None):
tf.random.set_seed(seed)
y = to_categorical(y)
mod = Sequential()
mod.add(Dense(57, input_dim=np.shape(X)[1], activation = 'sigmoid'))
mod.add(Dropout(.5))
mod.add(Dense(12, activation = 'sigmoid'))
mod.add(Dropout(.5))
mod.add(Dense(2, activation = 'sigmoid'))
mod.compile(loss = 'binary_crossentropy', optimizer = 'adam',
metrics=['accuracy'])
log_dir = 'logs'
callback = [
tf.keras.callbacks.EarlyStopping(monitor='loss', patience=1000),
tf.keras.callbacks.TensorBoard(log_dir = log_dir, histogram_freq = 3),
]
# on command line type "tensorboard --logdir logs" if you are in the
# parent dir of logs
class_weight = {0: 1., 1: 1.}
mod.fit(X, y, epochs = 10100, batch_size = 99999, class_weight = class_weight,
callbacks = callback)
return(mod)
def nn_outer_perf(mod, X, y):
preds = mod.predict(X)
preds = [1 if x[1] > x[0] else 0 for x in preds]
return(accuracy_score(y, preds))
def nn_score(mod, X, y):
preds = mod.predict(X)
return([x[1] for x in preds])