Esempio n. 1
0
func TrainNB0(trainMatrix [][]int, trainCategory []int) ([]float64, []float64, float64) {
	numTrainDocs := len(trainMatrix)
	numWords := len(trainMatrix[0])
	sumCategory := calc.SumInt(trainCategory)

	pAbusive := float64(sumCategory) / float64(numTrainDocs)
	p0Num := make([]int, numWords)
	p1Num := make([]int, numWords)

	p0Denom := float64(0.0)
	p1Denom := float64(0.0)

	for i := 0; i < numTrainDocs; i++ {
		if trainCategory[i] == 1 {
			p1Num = calc.AddIntVec(p1Num, trainMatrix[i])
			p1Denom += float64(calc.SumInt(trainMatrix[i]))
		} else {
			p0Num = calc.AddIntVec(p0Num, trainMatrix[i])
			p0Denom += float64(calc.SumInt(trainMatrix[i]))
		}
	}

	fmt.Println("p0Num: ", p0Num)
	fmt.Println("p1Num: ", p1Num)
	p1Vec := calc.DivIntVec(p1Num, p1Denom)
	p0Vec := calc.DivIntVec(p0Num, p0Denom)

	return p0Vec, p1Vec, pAbusive
}
Esempio n. 2
0
func Test_DivIntVec(t *testing.T) {
	a := []int{4, 7, 9}
	d := float64(2.0)
	result := calc.DivIntVec(a, d)

	if len(result) != len(a) {
		t.Fatalf("Div to the wrong size")
	}
}