Coding a Neural Network
Example: Breast Cancer Data
- The Cancer data set originally was captured at UCI Repository ( https://archive.ics.uci.edu/ml/datasets.html)
- Look at the data, so that you understand what is in each file.
Filename | Brief Description |
---|---|
cancer_data.csv | The table of measurements cancer_DESCR.csv – an overview of the data |
cancer_feature_names.csv | The names of the columns in cancer_data.csv |
cancer_target.csv |
The classification (0 or 1) of each row in cancer_data.csv |
cancer_target_names.csv | The names of the classifications (malignant or benign) |
Coding a Neural Network: General Steps
- Load the neural network packages
- Read in the data
- Divide the data into a training set and a test set.
- Preprocess the data
- Design the Network Model
- Train the model
- Apply the model to the test data
- Display the results
1. Load Neural Networks Package
from tensorflow import keras
from tensorflow.keras import layers
2. Read in the Data
import numpy as np
data_file = 'Data/cancer_data.csv'
target_file = 'Data/cancer_target.csv'
cancer_data=np.loadtxt(data_file,dtype=float,delimiter=',')
cancer_target=np.loadtxt(target_file, dtype=float, delimiter=',')
3. Divide Data
from sklearn import model_selection
test_size = 0.30
seed = 7
train_data, test_data, train_target, test_target = model_selection.train_test_split(cancer_data, cancer_target, test_size=test_size, random_state=seed)
4. Preprocess the Data
# Convert the classes to 'one-hot' vector
from keras.utils import to_categorical
y_train = to_categorical(train_target, num_classes=2)
y_test = to_categorical(test_target, num_classes=2)
5. Design the Network Model
def define_model():
model = keras.Sequential([
layers.Dense(30, activation="relu"),
layers.Dense(2, activation="softmax")
])
model.compile(optimizer="rmsprop",
loss="binary_crossentropy",
metrics=["accuracy"]
)
return(model)
model = define_model()
6. Train the Model
num_epochs = 10
batch_size = 32
model.fit(train_data, y_train, epochs=num_epochs, batch_size=batch_size)
7. Apply the Model to the Test Data
predictions = np.argmax(model.predict(test_data), axis=-1)
8. Display the Results
score = model.evaluate(test_data, y_test)
print('\nAccuracy: %.3f' % score[1])
from sklearn.metrics import confusion_matrix
print(confusion_matrix(test_target, predictions))
Activity: Neural Network Program
Make sure that you can run the Neural Network codes: 03_Neural_Network.ipynb