0

I am using nnet for the first time, played with the basic examples found on the web, but cannot make out its output with a dummy toy data set. That a simple discrimination of two classes (signal and background) using 2 variables normally distributed.

The following code can be copy&paste in R (version 3.0):

library(nnet)

## Signal
xs = rnorm( mean=0, sd=1, n=10000)
ys = rnorm( mean=1, sd=1, n=10000)
typs = rep( x=1, n=10000 )
sig = data.frame( typs, xs, ys )
colnames(sig) = c("z","x","y")
sig_train = sig[c(1:5000),]
sig_test = sig[c(5001:10000),]

## Background
xb = rnorm( mean=1, sd=1, n=10000)
yb = rnorm( mean=0, sd=1, n=10000)
typb = rep( x=-1, n=10000 )
bkg = data.frame( typb, xb, yb )
colnames(bkg) = c("z","x","y")
bkg_train = bkg[c(1:5000),]
bkg_test = bkg[c(5001:10000),]

## Training
trainData = rbind( sig_train, bkg_train )
nnRes = nnet( z ~ ., trainData, size = 2, rang = 0.5, maxit = 100)
print(nnRes)

## Testing
sigNNPred = predict(nnRes, sig_test )
bkgNNPred = predict(nnRes, bkg_test )

When looking at sigNNPred I have only zero's!

So either the configuration of my NN is not performant, or I am looking at the wrong thing.

Any hint is welcome.

Thanks in advance,

Xavier

Xavier Prudent
  • 1,570
  • 3
  • 25
  • 54

1 Answers1

2

There is a misconception about the target values (in your case, the column 'z'). If you want to do classification, you either have to convert the target column to a factor or you have to use 0/1 instead of -1/1. Otherwise, the -1 values are far outside the possible range of the activation function (unless you use linout=TRUE, which makes little sense for classification).

I tried your code with z being a factor and, as suggested by Fernando earlier, type='class' when calling predict: works nicely now, though your two classes overlap way too much to allow for a decent classification accuracy.

Cheers, UBod

UBod
  • 825
  • 7
  • 11