使用神经网络软件包进行多项式分类 [英] Multinomial classification using neuralnet package

查看:94
本文介绍了使用神经网络软件包进行多项式分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题应该很简单.但是文档没有帮助.

This question ought to be real simple. But the documentation isn't helping.

我正在使用R.对于多项式分类问题,必须使用neuralnet软件包.

I am using R. I must use the neuralnet package for a multinomial classification problem.

所有示例均适用于二项式或线性输出.我可以使用二项式输出来实现一些一对所有"的实现.但是我相信我应该能够通过将3个单位作为输出层来做到这一点,其中每个单位都是一个二项式(即,这是正确输出的概率).不?

All examples are for binomial or linear output. I could do some one-vs-all implementation using binomial output. But I believe I should be able to do this by having 3 units as the output layer, where each is a binomial (ie. probability of that being the correct output). No?

这就是我要使用的nnet(我相信它正在做我想做的事):

This is what I would using nnet (which I believe is doing what I want):

data(iris)
library(nnet)
m1 <- nnet(Species ~ ., iris, size = 3)
table(predict(m1, iris, type = "class"), iris$Species)

这是我尝试使用neuralnet进行的操作(公式破解是因为neuralnet在公式中似乎不支持'.'表示法):

This is what I am trying to do using neuralnet (the formula hack is because neuralnet does not seem to support the '.' notation in the formula):

data(iris)
library(neuralnet)
formula <- paste('Species ~', paste(names(iris)[-length(iris)], collapse='+'))
m2 <- neuralnet(formula, iris, hidden=3, linear.output=FALSE)
# fails !

推荐答案

您是正确的,因为neuralnet()的公式接口不支持'.'.

You are right that the formula interface of neuralnet() does not support '.'.

但是,上述代码的问题在于,一个因素未被接受为目标.您必须首先将因子Species扩展为三个二进制变量.具有讽刺意味的是,这与nnet包中的函数class.ind()效果最佳(由于nnet()multinom()可以很好地与因子配合使用,因此不需要这样的功能):

However, the problem with your code above is rather that a factor is not accepted as target. You have to expand the factor Species to three binary variables first. Ironically, this works best with the function class.ind() from the nnet package (which wouldn't need such a function, since nnet() and multinom() work fine with factors):

trainData <- cbind(iris[, 1:4], class.ind(iris$Species))
neuralnet(setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, trainData)

这有效-至少对我而言.

This works - at least for me.

这篇关于使用神经网络软件包进行多项式分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆