Beispiel #1
0
// Initialize will set up the PLA structure with the following:
// - the random linear function
// - vector Xn with X0 at 1 and X1 and X2 random point in the defined input space.
// - vector Yn the output of the random linear function on each point Xi. either -1 or +1  based on the linear function.
// - vector Wn is set to zero.
//
func (pla *PLA) Initialize() {

	pla.TargetVars = linear.RandLinearVars(pla.Interval) // create the random vars of the random linear function
	pla.TargetFunction = pla.TargetVars.Func()
	pla.Xn = make([]Point, pla.N)
	pla.Yn = make([]int, pla.N)
	pla.Wn[0] = float64(0)
	pla.Wn[1] = float64(0)
	pla.Wn[2] = float64(0)

	for i := 0; i < pla.N; i++ {
		pla.Xn[i][0] = float64(1)
		pla.Xn[i][1] = pla.Interval.RandFloat()
		pla.Xn[i][2] = pla.Interval.RandFloat()

		pla.Yn[i] = evaluate(pla.TargetFunction, pla.Xn[i])
	}
}
Beispiel #2
0
// Initialize will set up the PLA structure with the following:
// - the random linear function
// - vector Xn with X0 at 1 and X1 and X2 random point in the defined input space.
// - vector Yn the output of the random linear function on each point Xi. either -1 or +1  based on the linear function.
// - vector Wn is set to zero.
func (linreg *LinearRegression) Initialize() {

	// generate random target function if asked. (this is the default behavior)
	if linreg.RandomTargetFunction {
		linreg.TargetVars = linear.RandLinearVars(linreg.Interval) // create the random vars of the random linear function
		linreg.TargetFunction = linreg.TargetVars.Func()
	}

	linreg.Xn = make([][]float64, linreg.N)
	for i := 0; i < linreg.N; i++ {
		linreg.Xn[i] = make([]float64, linreg.VectorSize)
	}
	linreg.Yn = make([]int, linreg.N)
	linreg.Wn = make([]float64, linreg.VectorSize)

	for i := 0; i < linreg.N; i++ {
		linreg.Xn[i][0] = float64(1)
		for j := 1; j < len(linreg.Xn[i]); j++ {
			linreg.Xn[i][j] = linreg.Interval.RandFloat()
		}
		flip := 1
		if linreg.Noise != 0 {
			r := rand.New(rand.NewSource(time.Now().UnixNano()))
			rN := r.Intn(100)
			if rN < int(math.Ceil(linreg.Noise*100)) {
				flip = -1
			}
		}
		// output with potential noise in 'flip' variable
		if !linreg.TwoParams {
			linreg.Yn[i] = evaluate(linreg.TargetFunction, linreg.Xn[i]) * flip
		} else {
			linreg.Yn[i] = evaluateTwoParams(linreg.TargetFunction, linreg.Xn[i]) * flip
		}
	}
}