// Train the regression with some data points
326866
func (r *Regression) Train(d ...*dataPoint) {
	r.data = append(r.data, d...)
	if len(r.data) > 2 {
		r.initialised = true
	}
}
391480
func (k *TriangleKMeans) UpdateTrainingSet(trainingSet [][]float64) error {
	if len(trainingSet) == 0 {
		return fmt.Errorf("Error: length of given training set is 0! Need data!")
	}

	k.trainingSet = trainingSet
	k.guesses = make([]int, len(trainingSet))

	return nil
}
265717
func (rt *RandomTree) Predict(from base.FixedDataGrid) (base.FixedDataGrid, error) {
	return rt.Root.Predict(from)
}
160984
func PointsToUnitsVar(t int, u ...*float64) {
	for x := 0; x < len(u); x++ {
		*u[x] = PointsToUnits(t, *u[x])
	}
}
1050115
func MakeDataset(block uint64, dir string) {
	d := dataset{epoch: block / epochLength}
	d.generate(dir, math.MaxInt32, false)
}
945176
func (pt *PerceptronTagger) Train(sentences TupleSlice, iterations int) {
	var guess string
	var found bool

	pt.makeTagMap(sentences)
	for i := 0; i < iterations; i++ {
		for _, tuple := range sentences {
			words, tags := tuple[0], tuple[1]
			p1, p2 := "-START-", "-START2-"
			context := []string{p1, p2}
			for _, w := range words {
				if w == "" {
					continue
				}
				context = append(context, normalize(w))
			}
			context = append(context, []string{"-END-", "-END2-"}...)
			for i, word := range words {
				if guess, found = pt.tagMap[word]; !found {
					feats := featurize(i, context, word, p1, p2)
					guess = pt.model.predict(feats)
					pt.model.update(tags[i], guess, feats)
				}
				p2 = p1
				p1 = guess
			}
		}
		shuffle.Shuffle(sentences)
	}
	pt.model.averageWeights()
}
659660
func (r *Regression) SetVar(i int, name string) {
	if len(r.names.vars) == 0 {
		r.names.vars = make(map[int]string, 5)
	}
	r.names.vars[i] = name
}
1249603
func (p *Pooling) OutputShape(input *TensorDescriptor, dims int) (retVal []int, err error) {
	if dims == p.outDims && shapeEq(input.shape, p.inputTensor) {
		return cloneShape(p.outputShape), nil
	}
	return p.CalcOutputShape(input, dims)
}
645351
func builtinRepeat(args []types.Datum, _ context.Context) (d types.Datum, err error) {
	str, err := args[0].ToString()
	if err != nil {
		return d, err
	}
	ch := fmt.Sprintf("%v", str)
	num := 0
	x := args[1]
	switch x.Kind() {
	case types.KindInt64:
		num = int(x.GetInt64())
	case types.KindUint64:
		num = int(x.GetUint64())
	}
	if num < 1 {
		d.SetString("")
		return d, nil
	}
	d.SetString(strings.Repeat(ch, num))
	return d, nil
}
349679
func (c *Command) Predict(a Args) []string {
	options, _ := c.predict(a)
	return options
}
1365664
func builtinPow(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
	sc := ctx.GetSessionVars().StmtCtx
	x, err := args[0].ToFloat64(sc)
	if err != nil {
		return d, errors.Trace(err)
	}

	y, err := args[1].ToFloat64(sc)
	if err != nil {
		return d, errors.Trace(err)
	}
	d.SetFloat64(math.Pow(x, y))
	return d, nil
}
636739
func (f Float64Data) Pearson(d Float64Data) (float64, error) {
	return Pearson(f, d)
}
215638
func (pb *pointBuilder) Build(mbean string, value interface{}) []point {
	hasPattern := strings.Contains(mbean, "*")
	if !hasPattern {
		value = map[string]interface{}{mbean: value}
	}

	valueMap, ok := value.(map[string]interface{})
	if !ok { // FIXME: log it and move on.
		panic(fmt.Sprintf("There should be a map here for %s!\n", mbean))
	}

	points := make([]point, 0)
	for mbean, value := range valueMap {

		points = append(points, point{
			Tags:   pb.extractTags(mbean),
			Fields: pb.extractFields(mbean, value),
		})
	}

	return compactPoints(points)
}
852974
func (p *KernelPerceptron) Predict(x []float64, normalize ...bool) ([]float64, error) {
	if len(normalize) != 0 && normalize[0] {
		base.NormalizePoint(x)
	}

	var sum float64
	for i := range p.SV {
		sum += p.SV[i].Y[0] * p.Kernel(p.SV[i].X, x)
	}

	result := -1.0
	if sum > 0 {
		result = 1
	}

	return []float64{result}, nil
}
877613
func (s *Softmax) Learn() error {
	if s.trainingSet == nil || s.expectedResults == nil {
		err := fmt.Errorf("ERROR: Attempting to learn with no training examples!\n")
		fmt.Fprintf(s.Output, err.Error())
		return err
	}

	examples := len(s.trainingSet)
	if examples == 0 || len(s.trainingSet[0]) == 0 {
		err := fmt.Errorf("ERROR: Attempting to learn with no training examples!\n")
		fmt.Fprintf(s.Output, err.Error())
		return err
	}
	if len(s.expectedResults) == 0 {
		err := fmt.Errorf("ERROR: Attempting to learn with no expected results! This isn't an unsupervised model!! You'll need to include data before you learn :)\n")
		fmt.Fprintf(s.Output, err.Error())
		return err
	}

	fmt.Fprintf(s.Output, "Training:\n\tModel: Softmax Classification\n\tOptimization Method: %v\n\tTraining Examples: %v\n\t Classification Dimensions: %v\n\tFeatures: %v\n\tLearning Rate α: %v\n\tRegularization Parameter λ: %v\n...\n\n", s.method, examples, s.k, len(s.trainingSet[0]), s.alpha, s.regularization)

	var err error
	if s.method == base.BatchGA {
		err = func() error {
			// if the iterations given is 0, set it to be
			// 5000 (seems reasonable base value)
			if s.maxIterations == 0 {
				s.maxIterations = 5000
			}

			iter := 0

			// Stop iterating if the number of iterations exceeds
			// the limit
			for ; iter < s.maxIterations; iter++ {

				// go over each parameter vector for each
				// classification value
				newTheta := make([][]float64, len(s.Parameters))
				for k, theta := range s.Parameters {
					newTheta[k] = make([]float64, len(theta))

					dj, err := s.Dj(k)
					if err != nil {
						return err
					}

					for j := range theta {
						newTheta[k][j] = theta[j] + s.alpha*dj[j]
						if math.IsInf(newTheta[k][j], 0) || math.IsNaN(newTheta[k][j]) {
							return fmt.Errorf("Sorry dude! Learning diverged. Some value of the parameter vector theta is ±Inf or NaN")
						}
					}
				}

				s.Parameters = newTheta
			}

			fmt.Fprintf(s.Output, "Went through %v iterations.\n", iter)

			return nil
		}()
	} else if s.method == base.StochasticGA {
		err = func() error {
			// if the iterations given is 0, set it to be
			// 5000 (seems reasonable base value)
			if s.maxIterations == 0 {
				s.maxIterations = 5000
			}

			iter := 0

			// Stop iterating if the number of iterations exceeds
			// the limit
			for ; iter < s.maxIterations; iter++ {
				for j := range s.trainingSet {
					newTheta := make([][]float64, len(s.Parameters))
					// go over each parameter vector for each
					// classification value
					for k, theta := range s.Parameters {
						newTheta[k] = make([]float64, len(theta))
						dj, err := s.Dij(j, k)
						if err != nil {
							return err
						}

						// now simultaneously update theta
						for j := range theta {
							newTheta[k][j] = theta[j] + s.alpha*dj[j]
							if math.IsInf(newTheta[k][j], 0) || math.IsNaN(newTheta[k][j]) {
								return fmt.Errorf("Sorry dude! Learning diverged. Some value of the parameter vector theta is ±Inf or NaN")
							}
						}
					}

					s.Parameters = newTheta
				}
			}

			fmt.Fprintf(s.Output, "Went through %v iterations.\n", iter)

			return nil
		}()
	} else {
		err = fmt.Errorf("Chose a training method not implemented for Softmax regression")
	}

	if err != nil {
		fmt.Fprintf(s.Output, "\nERROR: Error while learning –\n\t%v\n\n", err)
		return err
	}

	fmt.Fprintf(s.Output, "Training Completed.\n%v\n\n", s)
	return nil
}
624047
func Resample(ls orb.LineString, df orb.DistanceFunc, totalPoints int) orb.LineString {
	if totalPoints <= 0 {
		return nil
	}

	ls, ret := resampleEdgeCases(ls, totalPoints)
	if ret {
		return ls
	}

	// precomputes the total distance and intermediate distances
	total, dists := precomputeDistances(ls, df)
	return resample(ls, dists, total, totalPoints)
}
1250690
func (b *BaggedModel) generateTrainingInstances(model int, from base.FixedDataGrid) base.FixedDataGrid {
	_, rows := from.Size()
	insts := base.SampleWithReplacement(from, rows)
	selected := b.generateTrainingAttrs(model, from)
	return base.NewInstancesViewFromAttrs(insts, selected)
}