如何在R中使用glmnet进行多类的分类

时间:2017-06-26 14:20:34

标签: r classification text-classification glmnet

我正在试图弄清楚如何使用glmnet对文本进行分类。

我设法使用

让它适用于两个班级
  

家族= “二项式”

     

type.measure = “AUC”

我想对使用多项式家族的多个班级做同样的事情。

我试过这样的事情

NFOLDS = 4;
glmnet_classifier = cv.glmnet(x = dtm_train, y = classes,
                              family = 'multinomial',
                              alpha = 1,
                              type.measure = "class",
                              nfolds = NFOLDS,
                              thresh = 1e-3,
                              maxit = 1e3)

其中类中的每个条目/行都是一个向量,其中类/类应为1,应归类为0,其余为0。这样的东西[1,0,1,0,0,0,0]。

我收到以下错误,这是可以理解的。

  

错误:来自glmnet Fortran代码(错误代码8034); 34级的空概率< 1.0E-5

问题是我是否正确创建了类向量?如果不是,它应该怎么样?

我可以使用glmnet对包含多个类的文本进行分类吗?

最好的问候

编辑: 数据集样本(来自瑞典报纸的文章): 这是一篇体育文章所以相应的课程应该是:篮球(篮球) 或[1,0,0,0,0 ...,0]

  

SödertäljeKingsääbaraen seger来自klubbens tredje raka SM-guld i   篮。 Efter idel hemmasegrar ifinalserienbrötSödertäljemönstret   idenfjärdematchen - och vann med 76-67 bortamotUppsala.Inför   rekordpublik,3004åskådareiFyrishov,ledde Uppsala med 32-28 i   PAUS。 MennärSödertäljeinleddeandra halvlek med tiorakapoäng   svängde倡议我matchen。 John Roberson gjorde baratrepoängi   förstahalvlek,men slutade matchensombästepoängplockaremed   20.Södertäljeledermed 3-1 i matcherochfårsinförstachansattsäkraguldetpåonsdagkvällhemmaiTäljehallen。

部分dput(dtm_train)基于100篇文章

dput(head(dtm_train))
new("dgCMatrix"
    , i = c(3L, 4L, 5L, 0L, 4L, 1L, 4L, 2L, 0L, 2L, 3L, 4L, 3L, 3L, 3L, 
4L, 1L, 2L, 1L, 3L, 3L, 2L, 5L, 3L, 4L, 5L, 2L, 5L, 3L, 3L, 4L, 
3L, 4L, 3L, 3L, 1L, 2L, 2L, 3L, 4L, 4L, 4L, 2L,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71L, 71L, 72L, 73L, 75L, 76L, 76L, 77L, 
77L, 79L, 80L, 80L, 81L, 83L, 84L, 86L, 86L, 86L, 88L, 88L, 90L, 
91L, 92L, 95L, 97L, 97L, 98L, 100L, 101L, 102L, 104L, 104L, 109L, 
112L, 115L, 115L, 118L, 123L, 124L, 124L, 124L, 125L, 125L, 127L, 
127L, 129L, 129L, 130L, 131L, 132L, 134L, 136L, 138L, 140L, 141L, 
143L, 145L, 146L, 147L, 148L, 148L, 152L, 152L, 153L, 154L, 157L, 
159L, 159L, 160L, 163L, 164L, 168L, 170L, 171L, 171L, 171L, 171L, 
171L, 172L, 173L, 174L, 175L, 175L, 178L, 178L, 178L, 178L, 178L, 
179L, 179L, 179L, 179L, 179L, 179L, 179L, 180L, 180L, 181L, 182L, 
183L, 183L, 183L, 184L, 184L, 185L, 185L, 185L, 187L, 187L, 187L, 
187L, 189L, 190L, 191L, 193L, 193L, 193L, 193L, 193L, 193L, 193L, 
193L, 194L, 194L, 194L)
    , Dim = c(6L, 223L)
    , Dimnames = list(c("4146090", "3670901", "3592883", "3662097", "3910593", 
"3617345"), c("Hancock", "London", "slutet", "nnu", "F_r", "tycker", 
"kronor", "start", "ngt", "OS", "tar", "m_ste", "lv", "sk", "ser", 
"16", "ngen", "Jag_r", "slut", "s_ger", "spelar", "k_nns", "3", 
"rde", "match", "po_ng", "po", "fj_rde", "d_rmed", "a", "r_n", 
"rbundet", "14", "EM", "pressmeddelande", "lla", "d_r", "H", 
"rande", "svarade", "rst", "nns", "sj_lv", "raka", "seger", "I", 
"matchen", "omg", "9", "spela", "fr_n", "Men", "par", "trea", 
"96", "b_ttre", "fr", "r_p", "tt", "p_plats", "h_r", "F", "2", 
"1", "se", "plats", "r_r", "6", "r_l", "Marcus", "gon", "senaste", 
"Efter", "4", "N", "l_ngre", "g_r", "Jag", "k", "s_songen", "r_v", 
"beh", "inf", "ny", "De", "nya", "Hon", "ta", "finalen", "Anna", 
"hela", "VM", "n_got", "llen", "tta", "sta", "da", "rt", "vinna", 
"r_b", "ringen", "g_ra", "ryska", "guld", "vlingar", "st_r", 
"rmed", "m_l", "nd", "rldscupen", "kt", "En", "f_re", "p_l", 
"Att", "r_s", "st", "n_sta", "Det_r", "nde", "tog", "r_f", "Vi", 
"r_h", "rnan", "ller", "ju", "matcher", "Och", "ker", "rsta", 
"t", "5", "ge", "sl", "f_rsta", "Den", "n_r", "ka", "sv", "m_nga", 
"tillbaka", "ret", "gra", "Han", "ng", "ocks", "r_g", "vlingen", 
"P", "f_rst", "laget", "vling", "t_vlingen", "nda", "TT", "jd", 
"rv", "V", "tv", "v_ldigt", "p_v", "rige", "25", "n_gra", "r_m", 
"tr", "ver", "ren", "b", "Sverige", "slutade", "Ericsson", "sekunder", 
"sj", "minuter", "ngre", "p_h", "f_rs", "riktigt", "forts", "inf_r", 
"11", "p_s", "vet", "rra", "rs", "songen", "vunnit", "tappade", 
"avg", "e", "lag", "ven", "tror", "got", "SM", "klart", "final", 
"S", "svenska", "Holmlund", "f_tt", "D", "ttre", "tid", "inleds", 
"fj", "r_klart", "ll", "ste", "re", "ra", "ldigt", "0", "b_sta", 
"Artemis", "j", "18", "nga", "vann", "nt", "Rio"))
    , x = c(0.0459223389874228, 0.0647622729309809, 0.0757236977214318, 
0.0419740104600043, 0.052313354577604, 0.0948559992442941, 0.0543657316974382, 
0.0605789581771455, 0.0629426371656586, 0.0407628507358551, 0.00648499898070422, 
0.0182910227660888, 0.0241750912448434, 0.020066135574452, 0.0155890766190175, 
0.0439691904638956, 0.0524911062249339, 0.0299949178428194, 0.0916290731874155, 
0.049979494465863, 0.0771004922254579, 0.0234565872019952, 0.0586414680049879, 
0.0294220816737799, 0.0276617861890238, 0.301436862510004, 0.022814505606222, 
0.0570362640155551, 0.0357475064795061, 0.0212942360273263, 0.0300303328590499, 
0.0450136129455133, 0.0317403681026056, 0.0126026760101808, 0.017873753239753, 
0.0654666659991881, 0.0374095234281075, 0.126129995039413, 0.040132271148904, 
0.0565967926458903, 0.0439691904638956, 0.0565967926458903, 0.0542034281395966, 
0.0948559992442941, 0.0542034281395966, 0.0657881455141156, 0.0209325917544913, 
0.020066135574452, 0.0106471180136631, 0.0351848736822438, 0.0345565723664895, 
0.0262234740965819, 0.0254742319795367, 0.0457229804760915, 0.016659831488621, 
0.0521163777038787, 0.0830365603410825, 0.0150975564256514, 0.0425828514569654, 
0.0605789581771455, 0.0543657316974382, 0.0677230909704131, 0.064919850387933, 
0.116584047344375, 0.0438937612482027, 0.0770196070578684, 0.0119030301816706, 
0.106013176810005, 0.0677230909704131, 0.020066135574452, 0.0344930906342888, 
0.0822351818926445, 0.0425631204072182, 0.297144546389207, 0.0229611694937114, 
0.0708219296662315, 0.0459223389874228, 0.0830365603410825, 0.0452926692769541, 
0.0612428010032831, 0.203169272911239, 0.106013176810005, 0.0757236977214318, 
0.0146312537494009, 0.0630649975197063, 0.020066135574452, 0.022814505606222, 
0.014518321749414, 0.0396198131754885, 0.0122461240724237, 0.050413150163406, 
0.0385098035289342, 0.0488450354947544, 0.0474494630520472, 0.0301951128513027, 
0.0396198131754885, 0.0367383722172712, 0.064919850387933, 0.0582920236721873, 
0.0185474620775141, 0.0578253691690934, 0.0916290731874155, 0.057826848716848, 
0.017873753239753, 0.0510825623765991, 0.0291900356437709, 0.0185754772278542, 
0.0261961858341534, 0.0364875445547136, 0.0611442761037827, 0.0182910227660888, 
0.0509535634198189, 0.0521163777038787, 0.101254676681821, 0.0322173971260341, 
0.066639325954484, 0.0469892683012387, 0.0654493379910111, 0.0569717141594182, 
0.0325552652339533, 0.0207169869670612, 0.0292162636714965, 0.0406940815424416, 
0.0112534032363783, 0.0788312468996329, 0.0185474620775141, 0.0728650295902341, 
0.0538994548161268, 0.033319662977242, 0.010945207312054, 0.0150975564256514, 
0.0385502461127289, 0.104718940785618, 0.0654493379910111, 0.0155890766190175, 
0.0879383809277911, 0.016659831488621, 0.0469892683012387, 0.064919850387933, 
0.110363745659486, 0.0657881455141156, 0.0630649975197063, 0.0788312468996329, 
0.064919850387933, 0.020066135574452, 0.0543657316974382, 0.020066135574452, 
0.0590406434101037, 0.0198042515665813, 0.0122426282411594, 0.0172652449554812, 
0.0480960395188404, 0.0565967926458903, 0.0617421950936377, 0.120397280432594, 
0.137596891922964, 0.0859980574518526, 0.0557976466142906, 0.0344930906342888, 
0.0481097731416646, 0.0770196070578684, 0.0654666659991881, 0.0467619042851344, 
0.0146312537494009, 0.0198042515665813, 0.0192384158075362, 0.0122426282411594, 
0.0345304899109623, 0.016108698563017, 0.045434790818766, 0.016659831488621, 
0.020066135574452, 0.0209325917544913, 0.0469892683012387, 0.0385502461127289, 
0.0830365603410825, 0.0150975564256514, 0.0425828514569654, 0.0355460092594844, 
0.0155890766190175, 0.106013176810005, 0.120397280432594, 0.0439691904638956, 
0.0396084103177112, 0.020066135574452, 0.102011041426328, 0.052313354577604, 
0.100826300326812, 0.0702183162990297, 0.050413150163406, 0.0146312537494009, 
0.0328356219361619, 0.0429990287259263, 0.0554331312260806)
    , factors = list()
)

我还可以看到我有一些编码问题,正在处理它

2 个答案:

答案 0 :(得分:0)

你的类向量现在是二进制向量,这就是二项式版本的工作原理。如果您想使用多项式版本,请创建一个多项式结果向量。

答案 1 :(得分:0)

我认为您应该使用以下内容来训练模型

库(glmnet) glmnetModel&lt; - cv.glmnet(objTrain_hashed,train_data [,outcomeName],                          family =“multinomial”,groups = TRUE,type.measure =“class”)

并从训练有素的模型预测

glm.Predict&lt; - predict(Spooky.glmnetModel,objtest_hashed,s =“lambda.min”,type = c(“class”))

这两个步骤将使用Lamda.min

为您提供类输出