当前位置: 动力学知识库 > 问答 > 编程问答 >

Neural Network with factor as outcome in r

问题描述:

I'm trying to train a neural network to predict a behaviour based on many summary statistics.

This is the structure of my data:

> str(training)

> 'data.frame': 22002 obs. of 27 variables:

$ X : num 0.376 0.368 0.5 0.402 0.308 0.564 0.368 0.274 0.764 0.464 ...

$ Y : num -0.832 -0.848 -0.792 -0.834 -0.948 -0.766 -0.87 -0.924 -0.568 -0.82 ...

$ q : num 0.913 0.924 0.937 0.926 0.997 ...

$ meanX : num 0.387 0.379 0.509 0.434 0.281 ...

$ meanY : num -0.843 -0.849 -0.792 -0.834 -0.91 ...

$ meanq : num 0.927 0.93 0.941 0.94 0.953 ...

$ SDX : num 0.0073 0.0108 0.0128 0.0109 0.0257 ...

$ SDY : num 0.00639 0.00872 0.00939 0.00891 0.02139 ...

$ SDq : num 0.00577 0.00854 0.01203 0.01069 0.02582 ...

$ angle : num 24.3 23.5 32.3 25.7 18 ...

$ staticX : num -0.01054 -0.01129 -0.00921 -0.03198 0.0265 ...

$ staticY : num 0.010583 0.0015 -0.000354 -0.000292 -0.037792 ...

$ ODBA : num 0.02113 0.01279 0.00956 0.03227 0.06429 ...

$ Vedba : num 0.01494 0.01139 0.00922 0.03198 0.04616 ...

$ kurtosisX: num 2.222 1.163 0.289 1.358 -0.267 ...

$ kurtosisY: num -0.321 -0.183 0.52 1.856 0.334 ...

$ kurtosisq: num -0.222 -0.411 1.32 1.435 0.599 ...

$ skewnessX: num 0.72248 0.76105 0.00852 -0.3768 -0.16768 ...

$ skewnessY: num -0.0741 0.1468 -0.7097 0.705 0.1693 ...

$ skewnessq: num 0.0911 -0.0813 0.6773 -0.3569 -0.2325 ...

$ minX : num 0.374 0.356 0.474 0.396 0.224 0.454 0.276 0.232 0.74 0.416 ...

$ minY : num 0.828 0.83 0.774 0.804 0.854 0.626 0.826 0.874 0.548 0.744 ...

$ minq : num 0.913 0.91 0.916 0.907 0.884 ...

$ maxX : num 0.412 0.41 0.546 0.458 0.33 0.638 0.454 0.316 0.8 0.504 ...

$ maxY : num 0.856 0.868 0.82 0.854 0.958 0.844 0.968 0.934 0.642 0.842 ...

$ maxq : num 0.939 0.947 0.982 0.969 1.013 ...

$ behaviour: Factor w/ 6 levels "1","2","3","5",..: 1 1 1 1 1 1 2 1 2 1 ...

When I input this:

library("neuralnet")

fitann=neuralnet(

formula=behaviour~X+Y+q+meanX+meanY+meanq+SDX+SDY+SDq+angle+staticX+staticY+kurtosisX+kurtosisY+kurtosisq+skewnessX+skewnessY+skewnessq+minX+minY+minq+maxX+maxY+maxq+ODBA+Vedba,

data=training)

I get the following error:

Error in neurons[[i]] %*% weights[[i]] :

requires numeric/complex matrix/vector arguments

Do I get this error because my outcome $behaviour is not numeric? If so, how is it possible to train a neural network with factor values for the outcome?

Thanks!

网友答案:

neuralnet does not handle factors directly. Therefore you need to input the variables as numeric. For factors you need to convert your categorical variable to a set of dummy variables .

In your case, you have one categorical variable behaviour . You can do the transformation as following.

new_train <- model.matrix (~behaviour+X+Y+q+meanX+meanY+meanq+SDX+SDY+SDq+angle+staticX+staticY+kurtosisX+kurtosisY+kurtosisq+skewnessX+skewnessY+skewnessq+minX+minY+minq+maxX+maxY+maxq+ODBA+Vedba,
data=training);

Then use the behaviour1, behaviour2 etc as the target for your training.

NOTE: You might not need all the features to make a prediction. Although it is good to do an initial study using all the variables, but I would recommend some kind of feature selection techniques, which may improve the accuracy of your model.

分享给朋友:
您可能感兴趣的文章:
随机阅读: