@ Override public Map < String , Double > predict ( Tuple predict ) { Map < Integer , Double > labelProb = new HashMap <> ( ) ; for ( Integer labelIndex : model . labelIndexer . getIndexSet ( ) ) { double likelihood = 1.0D ; for ( int i = 0 ; i < predict . vector . getVector ( ) . length ; i ++ ) { double fi = predict . vector . getVector ( ) [ i ] ; likelihood = likelihood * VectorUtils . gaussianPDF ( model . meanVectors [ labelIndex ] [ i ] , model . varianceVectors [ labelIndex ] [ i ] , fi ) ; } double posterior = model . labelPrior . get ( labelIndex ) * likelihood ; // prior*likelihood, This is numerator of posterior labelProb . put ( labelIndex , posterior ) ; } double evidence = labelProb . values ( ) . stream ( ) . reduce ( ( e1 , e2 ) -> e1 + e2 ) . orElse ( - 1D ) ; if ( evidence == - 1 ) { LOG . error ( "Evidence is Empty!" ) ; return new HashMap <> ( ) ; } labelProb . entrySet ( ) . forEach ( entry -> { double prob = entry . getValue ( ) / evidence ; labelProb . put ( entry . getKey ( ) , prob ) ; } ) ; // This is denominator of posterior Map < String , Double > result = model . labelIndexer . convertMapKey ( labelProb ) ; if ( predict . label == null || predict . label . isEmpty ( ) ) { // Just for write to predict tuple. predict . label = result . entrySet ( ) . stream ( ) . max ( ( e1 , e2 ) -> e1 . getValue ( ) . compareTo ( e2 . getValue ( ) ) ) . map ( Entry :: getKey ) . orElse ( StringUtils . EMPTY ) ; } return result ; }
Integer is label s Index from labelIndexer