Пример #1
0
func init() {

	// create the /tmp/.goml/ dir for persistance testing
	// if it doesn't already exist!
	err := os.MkdirAll("/tmp/.goml", os.ModePerm)
	if err != nil {
		panic(fmt.Sprintf("You should be able to create the directory for goml model persistance testing.\n\tError returned: %v\n", err.Error()))
	}

	// the line y=3
	flatX = [][]float64{}
	flatY = []float64{}
	for i := -10; i < 10; i++ {
		for j := -10; j < 10; j++ {
			for k := -10; k < 10; k++ {
				flatX = append(flatX, []float64{float64(i), float64(j), float64(k)})
				flatY = append(flatY, 3.0)
			}
		}
	}

	// the line y=x
	increasingX = [][]float64{}
	increasingY = []float64{}
	for i := -10; i < 10; i++ {
		increasingX = append(increasingX, []float64{float64(i)})
		increasingY = append(increasingY, float64(i))
	}

	threeDLineX = [][]float64{}
	threeDLineY = []float64{}

	normX = [][]float64{}
	normY = []float64{}
	// the line z = 10 + (x/10) + (y/5)
	for i := -10; i < 10; i++ {
		for j := -10; j < 10; j++ {
			threeDLineX = append(threeDLineX, []float64{float64(i), float64(j)})
			threeDLineY = append(threeDLineY, 10+float64(i)/10+float64(j)/5)

			normX = append(normX, []float64{float64(i), float64(j)})
		}
	}

	base.Normalize(normX)
	for i := range normX {
		normY = append(normY, 10+float64(normX[i][0])/10+float64(normX[i][1])/5)
	}

	// noisy x has random noise embedded
	rand.Seed(42)
	noisyX = [][]float64{}
	noisyY = []float64{}
	for i := 256.0; i < 1024; i += 2 {
		noisyX = append(noisyX, []float64{i + (rand.Float64()-0.5)*3})
		noisyY = append(noisyY, 0.5*i+rand.NormFloat64()*25)
	}
	// save the random data to make some nice plots!
	base.SaveDataToCSV("/tmp/.goml/noisy_linear.csv", noisyX, noisyY, true)
}
Пример #2
0
// SaveClusteredData takes operates on a k-means
// model, concatenating the given dataset with the
// assigned class from clustering and saving it to
// file.
//
// Basically just a wrapper for the base.SaveDataToCSV
// with the K-Means data.
func (k *TriangleKMeans) SaveClusteredData(filepath string) error {
	floatGuesses := []float64{}
	for _, val := range k.guesses {
		floatGuesses = append(floatGuesses, float64(val))
	}

	return base.SaveDataToCSV(filepath, k.trainingSet, floatGuesses, true)
}
Пример #3
0
// tests basically make a bunch of planes where
// when the input is above the plane the resultant
// output is 1.0, else 0.0
func init() {

	// create the /tmp/.goml/ dir for persistance testing
	// if it doesn't already exist!
	err := os.MkdirAll("/tmp/.goml", os.ModePerm)
	if err != nil {
		panic(fmt.Sprintf("You should be able to create the directory for goml model persistance testing.\n\tError returned: %v\n", err.Error()))
	}

	// 1 when ( 10*i + j/20 + k ) > 0
	fourDX = [][]float64{}
	fourDY = []float64{}
	for i := -40; i < 40; i += 4 {
		for j := -40; j < 40; j += 4 {
			for k := -40; k < 40; k += 4 {
				fourDX = append(fourDX, []float64{float64(i), float64(j), float64(k)})
				if 10*i+j/20+k > 0 {
					fourDY = append(fourDY, 1.0)
				} else {
					fourDY = append(fourDY, 0.0)
				}
			}
		}
	}

	// 1 when i > 0
	twoDX = [][]float64{}
	twoDY = []float64{}
	for i := -40.0; i < 40.0; i += 0.15 {
		twoDX = append(twoDX, []float64{i})
		if i/2+10 > 0 {
			twoDY = append(twoDY, 1.0)
		} else {
			twoDY = append(twoDY, 0.0)
		}
	}

	threeDX = [][]float64{}
	threeDY = []float64{}

	nX = [][]float64{}
	nY = []float64{}
	// 1 when i+j > 5
	for i := -10; i < 10; i++ {
		for j := -10; j < 10; j++ {
			threeDX = append(threeDX, []float64{float64(i), float64(j)})
			nX = append(nX, []float64{float64(i), float64(j)})

			if i+j > 5 {
				threeDY = append(threeDY, 1.0)
			} else {
				threeDY = append(threeDY, 0.0)
			}
		}
	}

	base.Normalize(nX)

	for i := range nX {
		if nX[i][0]+nX[i][1] > 5 {
			nY = append(nY, 1.0)
		} else {
			nY = append(nY, 0.0)
		}
	}

	// now make gaussian clusters for cool plots!
	rand.Seed(42)
	gaussianX = [][]float64{}
	gaussianY = []float64{}

	for i := 0; i < 100; i++ {
		gaussianX = append(gaussianX, []float64{
			rand.NormFloat64()*3 + 10,
			rand.NormFloat64()*3 + 10,
		})
		gaussianY = append(gaussianY, 1.0)
	}
	for i := 0; i < 100; i++ {
		gaussianX = append(gaussianX, []float64{
			rand.NormFloat64() * 5,
			rand.NormFloat64() * 5,
		})
		gaussianY = append(gaussianY, 0.0)
	}
	base.SaveDataToCSV("/tmp/.goml/gaussian_clusters.csv", gaussianX, gaussianY, true)
}