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

5
  • Code not running: Error in eval(expr, envir, enclos) : object 'trainData' not found
    – Baumann
    Feb 14, 2014 at 20:29
  • And what is z in the nnet formula?
    – Baumann
    Feb 14, 2014 at 20:31
  • I corrected the code, so the 2 discriminating variables are 'x' and 'y', 'z' being the class variable for the learning phase. Feb 14, 2014 at 20:44
  • Try predict(nnRes, sig_test, type = 'class').
    – Fernando
    Feb 14, 2014 at 20:50
  • I get then Error in predict.nnet(nnRes, sig_test, type = "class") : inappropriate fit for class even when making 'z' a factor through as.factor(rep) Feb 14, 2014 at 21:17

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.