Example #1
0
func (c *KNN) PredictMultiClass(sample *core.Sample) *core.ArrayVector {
	x := sample.GetFeatureVector()
	predictions := []*eval.LabelPrediction{}
	for i, s := range c.sv {
		predictions = append(predictions, &(eval.LabelPrediction{Label: c.labels[i], Prediction: c.Kernel(s, x)}))
	}

	compare := func(p1, p2 *eval.LabelPrediction) bool {
		return p1.Prediction > p2.Prediction
	}

	eval.By(compare).Sort(predictions)

	ret := core.NewArrayVector()
	for i, pred := range predictions {
		if i > c.k {
			break
		}
		ret.AddValue(pred.Label, 1.0)
	}
	return ret
}
Example #2
0
func (c *SVM) Predict(sample *core.Sample) float64 {
	x := sample.GetFeatureVector()
	return c.PredictVector(x)
}