-
-
Save garymanley/5e7d6cc4934bdaf0e082438e04e1e286 to your computer and use it in GitHub Desktop.
Dups NN
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat Jan 6 05:12:40 2018 | |
@author: garym | |
""" | |
#import numpy as np | |
#import matplotlib.pyplot as plt | |
import pandas as pd | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
import keras | |
from keras.models import Sequential | |
from keras.layers import Dense ## Makes all weights close to 0 to start | |
### This is the data I have preprocessed data for ML e.g. Male = 0 / 1 rather than Gender = Male / Female | |
dataset = pd.read_excel(r'C:\Users\garym\Documents\PyWinAutoBlog\DD_data\DDData2.xlsx') | |
### set the data to predict with as all but the last column (12) | |
X = dataset.iloc[:, 0:12].values | |
### set value to predict as column 12 | |
y = dataset.iloc[:, 12].values | |
# As before use sklearn train test split | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0) | |
# This scales the features so age, for example, doesn't have too much impact as age = 70 other weights 0/1 | |
sc = StandardScaler() | |
X_train = sc.fit_transform(X_train) | |
X_test = sc.transform(X_test) | |
### set up a place-holder for the neural network | |
classifier = Sequential() | |
### Create an input layer. To start with using one neuron per item used to preduct | |
### relu = rectifier function. This is the best starting activation function for hidden layers | |
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 12)) | |
# Basically same as above, this is the second hidden layer | |
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu')) | |
# Basically same as above, this is the third hidden layer | |
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu')) | |
### The output layer | |
### I have binary, Ledderhose yes or no so | |
### Therefore I need 1 output (unit) and activation function of signmoid to give probablility | |
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid')) | |
### Build the Neural Network | |
### Use Compile function | |
### adam is a type of sgd optimiser function considered pretty good for this purpose | |
### I use a binary loss function as binary output | |
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) | |
### Training the model | |
### The workhorse bit - this can take a while to run | |
### batch size relate to recalulating the weights, epochs is running the dataset | |
classifier.fit(X_train, y_train, batch_size = 10, epochs = 75) | |
## see how the model works when predicting the test results | |
y_pred = classifier.predict(X_test) | |
y_pred = (y_pred > 0.55) | |
## Create confusion matrix to see how things went | |
from sklearn.metrics import confusion_matrix | |
cm = confusion_matrix(y_test, y_pred) | |
## print the matrix | |
print(cm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment