// Predict the label of an instance, given a model with probability // information. This method returns the label of the predicted class, // a map of class probabilities, and an error if the model was not // trained without the required information to do probability estimates. func (model *Model) PredictProbability(nodes []FeatureValue) (float64, map[int]float64, error) { if C.svm_check_probability_model_wrap(model.model) == 0 { return 0, nil, errors.New("Model was not trained to do probability estimates") } // Allocate sparse C feature vector. cn := cNodes(nodes) defer C.nodes_free(cn) // Allocate C array for probabilities. cProbs := C.probs_new(model.model) defer C.free(unsafe.Pointer(cProbs)) r := C.svm_predict_probability_wrap(model.model, cn, cProbs) // Store the probabilities in a slice labels := model.labels() probs := make(map[int]float64) for idx, label := range labels { probs[label] = float64(C.get_double_idx(cProbs, C.int(idx))) } return float64(r), probs, nil }
func newProbs(model *C.model_t) *C.double { probs := tryNew(func() unsafe.Pointer { return unsafe.Pointer(C.probs_new(model)) }) return (*C.double)(probs) }