Exemplo n.º 1
0
// 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.gosvm_nodes_free(cn)

	// Allocate C array for probabilities.
	cProbs := C.gosvm_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.gosvm_get_double_idx(cProbs, C.int(idx)))
	}

	return float64(r), probs, nil
}
Exemplo n.º 2
0
// Predict the label of an instance using the given model.
func (model *Model) Predict(nodes []FeatureValue) float64 {
	cn := cNodes(nodes)
	defer C.gosvm_nodes_free(cn)
	return float64(C.svm_predict_wrap(model.model, cn))
}