// double predict(const struct model *model_, const struct feature_node *x); func Predict(model *Model, X *mat64.Dense) *mat64.Dense { nRows, nCols := X.Dims() cX := mapCDouble(X.RawMatrix().Data) y := mat64.NewDense(nRows, 1, nil) result := doubleToFloats(C.call_predict( model.cModel, &cX[0], C.int(nRows), C.int(nCols)), nRows) y.SetCol(0, result) return y }
// predictFeaturized multiplies the featureWeights by the featurized input and stores the value. It assumes // that inMat and outMat already have the correct shape, but will replace the data in them func predictFeaturized(featurizedInput []float64, output []float64, featureWeights *mat64.Dense, inMat *mat64.Dense, outMat *mat64.Dense) { rm := inMat.RawMatrix() rmin.Data = featurizedInput inMat.LoadRawMatrix(rmin) rm = outMat.RawMatrix() rm.Data = outMat outMat.LoadRawMatrix(rmin) // Multiply the feature weights by the featurized input ond store outMat.Mul(inMat, featureWeights) }
// double predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates); func PredictProba(model *Model, X *mat64.Dense) *mat64.Dense { nRows, nCols := X.Dims() nrClasses := int(C.get_nr_class(model.cModel)) cX := mapCDouble(X.RawMatrix().Data) y := mat64.NewDense(nRows, nrClasses, nil) result := doubleToFloats(C.call_predict_proba( model.cModel, &cX[0], C.int(nRows), C.int(nCols), C.int(nrClasses)), nRows*nrClasses) for i := 0; i < nRows; i++ { y.SetRow(i, result[i*nrClasses:(i+1)*nrClasses]) } return y }
// Wrapper for the `train` function in liblinear. // // `model* train(const struct problem *prob, const struct parameter *param);` // // The explanation of parameters are: // // solverType: // // for multi-class classification // 0 -- L2-regularized logistic regression (primal) // 1 -- L2-regularized L2-loss support vector classification (dual) // 2 -- L2-regularized L2-loss support vector classification (primal) // 3 -- L2-regularized L1-loss support vector classification (dual) // 4 -- support vector classification by Crammer and Singer // 5 -- L1-regularized L2-loss support vector classification // 6 -- L1-regularized logistic regression // 7 -- L2-regularized logistic regression (dual) // for regression // 11 -- L2-regularized L2-loss support vector regression (primal) // 12 -- L2-regularized L2-loss support vector regression (dual) // 13 -- L2-regularized L1-loss support vector regression (dual) // // eps is the stopping criterion. // // C_ is the cost of constraints violation. // // p is the sensitiveness of loss of support vector regression. // // classWeights is a map from int to float64, with the key be the class and the // value be the weight. For example, {1: 10, -1: 0.5} means giving weight=10 for // class=1 while weight=0.5 for class=-1 // // If you do not want to change penalty for any of the classes, just set // classWeights to nil. func Train(X, y *mat64.Dense, bias float64, solverType int, c_, p, eps float64, classWeights map[int]float64) *Model { var weightLabelPtr *C.int var weightPtr *C.double nRows, nCols := X.Dims() cX := mapCDouble(X.RawMatrix().Data) cY := mapCDouble(y.ColView(0).RawVector().Data) nrWeight := len(classWeights) weightLabel := []C.int{} weight := []C.double{} for key, val := range classWeights { weightLabel = append(weightLabel, (C.int)(key)) weight = append(weight, (C.double)(val)) } if nrWeight > 0 { weightLabelPtr = &weightLabel[0] weightPtr = &weight[0] } else { weightLabelPtr = nil weightPtr = nil } model := C.call_train( &cX[0], &cY[0], C.int(nRows), C.int(nCols), C.double(bias), C.int(solverType), C.double(c_), C.double(p), C.double(eps), C.int(nrWeight), weightLabelPtr, weightPtr) return &Model{ cModel: model, } }