Ejemplo n.º 1
0
func finalizeProblem(p *Problem) {
	for _, nodes := range p.insts {
		C.nodes_free(nodes)
	}
	p.insts = nil
	C.problem_free(p.problem)
}
Ejemplo n.º 2
0
// PredictProbabilitySlice predicts the label of an instance, given a
// model with probability information. This method returns the label
// of the predicted class and a slice of class probabilities. Probability
// estimates are currently given for logistic regression only. If another
// solver is used, the probability of each class is zero.
//
// The PredictProbability function is more user-friendly, but has the
// overhead of constructing a map. If you are only interested in the
// classes with the highest probabilities, it may be better to use
// this function in conjunction with Labels().
func (model *Model) PredictProbabilitySlice(nodes []FeatureValue) (float64, []float64, error) {
	// Allocate sparse C feature vector.
	cn := cNodes(nodes)
	defer C.nodes_free(cn)

	probs := make([]float64, len(model.Labels()))
	r := C.predict_probability_wrap(model.model, cn, (*C.double)(unsafe.Pointer(&probs[0])))

	return float64(r), probs, nil
}
Ejemplo n.º 3
0
// PredictDecisionValuesSlice predicts the label of an instance. In
// contrast to Predict, it also returns the per-label decision values.
// The PredictDecisionValues function is more user-friendly, but has
// the overhead of constructing a map. If you are only interested in
// the classes with the highest decision values, it may be better to
// use this function in conjunction with Labels().
func (model *Model) PredictDecisionValuesSlice(nodes []FeatureValue) (float64, []float64, error) {
	// Allocate sparse C feature vector.
	cn := cNodes(nodes)
	defer C.nodes_free(cn)

	labels := model.Labels()

	// Allocate C array for decision values.
	values := make([]float64, len(labels))

	r := C.predict_values_wrap(model.model, cn, (*C.double)(unsafe.Pointer(&values[0])))

	return float64(r), values, nil
}
Ejemplo n.º 4
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. Probability estimates are currently
// given for logistic regression only. If another solver is used,
// the probability of each class is zero.
func (model *Model) PredictProbability(nodes []FeatureValue) (float64, map[int]float64, error) {
	// Allocate sparse C feature vector.
	cn := cNodes(nodes)
	defer C.nodes_free(cn)

	// Allocate C array for probabilities.
	cProbs := newProbs(model.model)
	defer C.free(unsafe.Pointer(cProbs))

	r := C.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
}
Ejemplo n.º 5
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.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
}
Ejemplo n.º 6
0
// Predict the label of an instance using the given model.
func (model *Model) Predict(nodes []FeatureValue) float64 {
	cn := cNodes(nodes)
	defer C.nodes_free(cn)
	return float64(C.svm_predict_wrap(model.model, cn))
}