R:混淆矩阵

2019-02-18 15:58:23 浏览数 (1)

在机器学习中,最后要计算混淆矩阵,常用的函数有:

  • table
  • confusionMatrix

下面以前馈神经网络为例来说明:

代码语言:javascript复制
> library("RSNNS") 
载入需要的程辑包:Rcpp
> library("reshape")
> #example data and code from nnet function examples
> ir<-rbind(iris3[,,1],iris3[,,2],iris3[,,3])
> targets<-class.ind( c(rep("s", 50), rep("c", 50), rep("v", 50)) )
> samp<-c(sample(1:50,25), sample(51:100,25), sample(101:150,25))
> ir1<-nnet(ir[samp,], targets[samp,], size = 2, rang = 0.1,decay = 5e-4, maxit = 20000)
# weights:  19
initial  value 57.066913 
iter  10 value 49.626816
iter  20 value 37.715215
iter  30 value 37.621041
iter  40 value 36.061689
iter  50 value 26.406216
iter  60 value 26.358554
iter  70 value 26.304323
iter  80 value 26.037665
iter  90 value 9.566053
iter 100 value 2.356429
iter 110 value 2.330607
iter 120 value 2.322488
iter 130 value 2.320141
iter 140 value 2.319636
iter 150 value 2.319221
iter 160 value 2.318749
iter 170 value 2.318371
iter 180 value 2.318266
iter 190 value 2.318228
iter 200 value 2.318209
final  value 2.318200 
converged
> 
> #####预测和混淆矩阵
> predictions = predict(ir1,ir[-samp,])
> confusionMatrix(targets[-samp,],predictions)
       predictions
targets  1  2  3
      1 23  0  2
      2  0 25  0
      3  0  0 25
> table(targets[-samp,],predictions)
   predictions
    0.00403377540307142 0.00403564641047421 0.00403570768092494
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00403605256654588 0.00403630750101855 0.00403641366388631
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00403692649836654 0.00403786506435831 0.00403825215906985
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00403917030652169 0.00404567897588413 0.00404798236102891
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00404893293411496 0.00405574340716223 0.00405836767997865
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00406710814346536 0.0040701247774299 0.00408251547829692
  0                   1                  1                   1
  1                   0                  0                   0
   predictions
    0.00412797392700353 0.00413594795412441 0.00416158973397778
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00416814984429532 0.00417286744332778 0.00417362395573791
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00417918626716218 0.00418009383086655 0.00419874938914643
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00421981780492951 0.00423152295291501 0.00423193441275875
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00424849342991043 0.00430144238200611 0.00436038633080191
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00439684626994913 0.00442242048735527 0.00458507759207128
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00464975407207362 0.00470506382077678 0.00499347960880904
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00518953025588815 0.00519933029061764 0.0052814348113268
  0                   1                   1                  1
  1                   0                   0                  0
   predictions
    0.00558431531798989 0.00595926604148108 0.00600552141490726
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.006915891368665 0.00759615927656485 0.00829442420831341
  0                 1                   1                   1
  1                 0                   0                   0
   predictions
    0.00844447207060653 0.00856276324301847 0.00859179660930141
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00860201009135315 0.00860601920084842 0.00863743659341979
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00864140831518805 0.00864888074285641 0.0086521847462041
  0                   1                   1                  1
  1                   0                   0                  0
   predictions
    0.00865344584729278 0.00865493915617089 0.00865849335003599
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00865922177678391 0.00867566062753029 0.00867896771232396
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00868312254593745 0.0086859108866324 0.00869537547110483
  0                   1                  1                   1
  1                   0                  0                   0
   predictions
    0.00869670887342119 0.00869864378012673 0.00870865406145213
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00871389523727452 0.00872021311903557 0.00872345360284301
  0                   1                   1                   1
  1                   0                   0                   0
   predictions
    0.00872387327365123 0.0102323682173483 0.0105757335442083
  0                   1                  1                  1
  1                   0                  0                  0
   predictions
    0.0119529981550286 0.0124141057613249 0.0126325452293645
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.012833440812906 0.0131525079576031 0.0132079822099268
  0                 1                  1                  1
  1                 0                  0                  0
   predictions
    0.0133337996928362 0.013367781106834 0.0134762990363478
  0                  1                 1                  1
  1                  0                 0                  0
   predictions
    0.0135442965605383 0.0135810901988006 0.0136761283209433
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0136774878496964 0.0137379609469079 0.0137420907754618
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0144442987763459 0.0156778113343933 0.0156869197995948
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.015757440975887 0.0158959078604056 0.0160117612659147
  0                 1                  1                  1
  1                 0                  0                  0
   predictions
    0.0162355415267055 0.0162791795628935 0.0163093245777637
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0164781461796075 0.0165250129752519 0.0165891344883245
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0166851724888285 0.0167620382615496 0.0171497596692717
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0171671595863806 0.017252328578273 0.0172882467881398
  0                  1                 1                  1
  1                  0                 0                  0
   predictions
    0.0173186417658541 0.0173985447394498 0.0175807065809304
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0176783539787929 0.01847141318664 0.0185753050370508 0.018662823048094
  0                  1                1                  1                 1
  1                  0                0                  0                 0
   predictions
    0.0188427947522583 0.0194803512595932 0.0196257828944348
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.023191551665019 0.0250189206726615 0.0274317187789071
  0                 1                  1                  1
  1                 0                  0                  0
   predictions
    0.027449845321461 0.0274535377434953 0.0274561647040755
  0                 1                  1                  1
  1                 0                  0                  0
   predictions
    0.0274671028942351 0.027474213631734 0.0274754708307417
  0                  1                 1                  1
  1                  0                 0                  0
   predictions
    0.0275215489790161 0.0275253497277815 0.0276609180875735
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.027661232198567 0.0278704168563232 0.0279312670107136
  0                 1                  1                  1
  1                 0                  0                  0
   predictions
    0.0281167604326715 0.0281541170972859 0.028388668868556
  0                  1                  1                 1
  1                  0                  0                 0
   predictions
    0.028890862721736 0.0288985222475749 0.0291613373288461
  0                 1                  1                  1
  1                 0                  0                  0
   predictions
    0.0321499794319152 0.0325145751040805 0.0347032558252448
  0                  1                  1                  1
  1                  0                  0                  0
   predictions
    0.0349570256697902 0.0670718767155293 0.112184565533575
  0                  1                  1                 1
  1                  0                  0                 0
   predictions
    0.118523381829258 0.149732846270052 0.156742558680462 0.246236986571863
  0                 1                 0                 1                 0
  1                 0                 1                 0                 1
   predictions
    0.375634421026004 0.594721005324995 0.747773548238688 0.842525603401285
  0                 1                 0                 1                 0
  1                 0                 1                 0                 1
   predictions
    0.849297998894218 0.867686231396129 0.8824232162991 0.935631524840151
  0                 1                 0               0                 0
  1                 0                 1               1                 1
   predictions
    0.967245893542904 0.967686698818511 0.967955157358171 0.970082509157067
  0                 0                 0                 0                 0
  1                 1                 1                 1                 1
   predictions
    0.97044659702987 0.97335595682131 0.973513712122999 0.973597441388668
  0                0                0                 0                 0
  1                1                1                 1                 1
   predictions
    0.9741089604963 0.974296709168098 0.974313480219197 0.97453043045748
  0               0                 0                 0                0
  1               1                 1                 1                1
   predictions
    0.97457673836994 0.974773168687682 0.974809586221223 0.974930051689301
  0                0                 0                 0                 0
  1                1                 1                 1                 1
   predictions
    0.974945619426515 0.974982681274091 0.97498434260433 0.974986907735066
  0                 0                 0                0                 0
  1                 1                 1                1                 1
   predictions
    0.974993162694631 0.975003012935513 0.975008716416213 0.975034560958276
  0                 0                 0                 0                 0
  1                 1                 1                 1                 1
   predictions
    0.977991144171454 0.981119083566536 0.981811727613766 0.982048875519914
  0                 0                 0                 0                 0
  1                 1                 1                 1                 1
   predictions
    0.982141056136557 0.982846103949432 0.982933087841843 0.983095460654908
  0                 0                 0                 0                 0
  1                 1                 1                 1                 1
   predictions
    0.983166726405304 0.983193842721315 0.98322589142289 0.983301906510452
  0                 0                 0                0                 0
  1                 1                 1                1                 1
   predictions
    0.983317440019031 0.983663903576428 0.983732666104228 0.9838186155581
  0                 0                 0                 0               0
  1                 1                 1                 1               1
   predictions
    0.98387602356082 0.984069261555991 0.984096285201701 0.984135411879867
  0                0                 0                 0                 0
  1                1                 1                 1                 1
   predictions
    0.984336191216582 0.984440225557155 0.984564646746689 0.98462804833628
  0                 0                 0                 0                0
  1                 1                 1                 1                1
   predictions
    0.984636238917024 0.985888119718936 0.989343984979299 0.99111550527635
  0                 0                 0                 0                0
  1                 1                 1                 1                1
   predictions
    0.991183977831676 0.991249360303782 0.992224304664137 0.992508176255563
  0                 0                 0                 0                 0
  1                 1                 1                 1                 1
   predictions
    0.992988966531712 0.993048210832194 0.993178174597829 0.993250413896391
  0                 0                 0                 0                 0
  1                 1                 1                 1                 1
   predictions
    0.993328226044017 0.993378955615131 0.99348242946841 0.993551717508145
  0                 0                 0                0                 0
  1                 1                 1                1                 1
   predictions
    0.993557187071166 0.99363007375035 0.993692579864391 0.993734588950939
  0                 0                0                 0                 0
  1                 1                1                 1                 1

0 人点赞