# Neural Net Project¶

Let's wrap up this course by taking a a quick look at the effectiveness of Neural Nets!

We'll use the Bank Authentication Data Set from the UCI repository.

The data consists of 5 columns:

- variance of Wavelet Transformed image (continuous)
- skewness of Wavelet Transformed image (continuous)
- curtosis of Wavelet Transformed image (continuous)
- entropy of image (continuous)
- class (integer)

Where class indicates whether or not a Bank Note was authentic.

## Get the Data¶

Use read.csv to read the bank_note_data.csv file.

In [122]:

Check the head of the data frame and its structure.

In [123]:

Out[123]:

| Image.Var | Image.Skew | Image.Curt | Entropy | Class
--- | --- | --- | --- | --- | ---
1 | 3.6216 | 8.6661 | -2.8073 | -0.44699 | 0
2 | 4.5459 | 8.1674 | -2.4586 | -1.4621 | 0
3 | 3.866 | -2.6383 | 1.9242 | 0.10645 | 0
4 | 3.4566 | 9.5228 | -4.0112 | -3.5944 | 0
5 | 0.32924 | -4.4552 | 4.5718 | -0.9888 | 0
6 | 4.3684 | 9.6718 | -3.9606 | -3.1625 | 0

In [125]:

    'data.frame':	1372 obs. of  5 variables:
     $ Image.Var : num  3.622 4.546 3.866 3.457 0.329 ...
     $ Image.Skew: num  8.67 8.17 -2.64 9.52 -4.46 ...
     $ Image.Curt: num  -2.81 -2.46 1.92 -4.01 4.57 ...
     $ Entropy   : num  -0.447 -1.462 0.106 -3.594 -0.989 ...
     $ Class     : int  0 0 0 0 0 0 0 0 0 0 ...

## EDA¶

Create whatever visualizations you are interested in. We'll skip this step for the solutions notebook/video because the data isn't easily interpretable since its just statistical info on images.

## Train Test Split¶

Use the caTools library to split the data into training and testing sets.

In [127]:

Check the structure of the train data and note that Class is still an int data type. We won't convert it to a factor for now because the neural net requires all numeric information.

In [129]:

    'data.frame':	960 obs. of  5 variables:
     $ Image.Var : num  3.622 4.546 3.457 0.329 4.368 ...
     $ Image.Skew: num  8.67 8.17 9.52 -4.46 9.67 ...
     $ Image.Curt: num  -2.81 -2.46 -4.01 4.57 -3.96 ...
     $ Entropy   : num  -0.447 -1.462 -3.594 -0.989 -3.163 ...
     $ Class     : int  0 0 0 0 0 0 0 0 0 0 ...

## Building the Neural Net¶

Call the neuralnet library

In [130]:

Browse through the documentation of neuralnet

In [140]:

Use the neuralnet function to train a neural net, set linear.output=FALSe and choose 10 hidden neurons (hidden=10)

In [141]:

## Predictions¶

Use compute() to grab predictions useing your nn model on the test set. Reference the lecture on how to do this.

In [142]:

Check the head of the predicted values. You should notice that they are still probabilities.

In [143]:

Out[143]:

3 | 0.002193454319
11 | 0.00003652277413
12 | 0.001859463551
13 | 0.00000632594144
14 | 0.00000656587257
17 | 0.0001286774258

Apply the round function to the predicted values so you only 0s and 1s as your predicted classes.

In [144]:

In [145]:

Out[145]:

1. 0
2. 0
3. 0
4. 0
5. 0
6. 0

Use table() to create a confusion matrix of your predictions versus the real values

In [146]:

Out[146]:

    predictions   0   1
              0 229   0
              1   0 183

You should have noticed that you did very well! Almost suspiciously well! Let's check our results against a randomForest model!

## Comparing Models¶

Call the randomForest library

In [147]:

Run the Code below to set the Class column of the data as a factor (randomForest needs it to be a factor, not an int like neural nets did. Then re-do the train/test split

In [148]:

Create a randomForest model with the new adjusted training data.

In [149]:

Use predict() to get the predicted values from your rf model.

In [150]:

Use table() to create the confusion matrix.

In [151]:

Out[151]:

    rf.pred   0   1
          0 227   1
          1   2 182

How did the models compare?

# Great Job!¶