I have a dataset that looks like this :
char x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6 x7 y7 x8 y8 x9 y9
n 0.1875 0.140625 0.09375 0.515625 0 0.8828125 0.1796875 0.5078125 0.4609375 0.140625 0.640625 0.109375 0.515625 0.5390625 0.3828125 0.8828125 0.671875 0.9765625
h 0 0 0.046875 0.3125 0.0625 0.671875 0.15625 0.765625 0.328125 0.4609375 0.4609375 0.3359375 0.625 0.4609375 0.6015625 0.7421875 0.53125 0.9921875
I am using nnet library in R to predict the char column from this data set. The data set is divided into 80%(training) and 20%(test)
Also I have converted the response char to numeric (1:26) for all the rows using the below code
trainResponse = as.integer(factor(trainingData[,1], levels = letters))
After changing response column of training data set, where the char was "h", the value currently = 8:
char x1 y1 x2 y2 x3 y3 x4 y4 x5
8 0 0 0.0390625 0.25 0.0546875 0.5859375 0.046875 0.8359375 0.1015625
y5 x6 y6 x7 y7 x8 y8 x9 y9
0.671875 0.2421875 0.625 0.3515625 0.828125 0.40625 0.921875 0.2890625 0.7578125
// The above 2 lines shows the modifiedTrainData
modifiedTrainData = cbind(char = trainResponse,trainingData[,2:19])
//fitting the nnet model
nn1 = nnet(char ~ ., data = modifiedTrainData,size = 20, maxit = 1000,
range = 0.1, trace = T)
# weights: 401
initial value 904134.647697
final value 846130.000000
converged
When I ask for fitted values , it gives me 1 for all the rows
nn1$fitted.values
2662 1
4991 1
445 1
3311 1
2271 1
2622 1
4433 1
3571 1
1269 1
For predict , it again gives me the result in the similar format:
predict(nn1, testData[,2:19])
3543 1
3545 1
3549 1
3554 1
for all the rows in the test data
How ever I am not sure if I am doing it correctly and how should I infer that the model predicted the char column correctly in the test data?
Best Answer
For the most part you are fine; however, the glaring issue is that you have no reason to convert
char
to an integer asnnet
accepts factors. That is why you only see 1's reported. As an example:When you submit actual classes, you then get output that you can actually use for predicting classes.