//PartialFit fit Gaussian Naive Bayes according to X, y
PartialFit fit Gaussian Naive Bayes according to X, y
// PartialFit fit Gaussian Naive Bayes according to X y
func (m *GaussianNB) PartialFit(X, Y mat.Matrix, classes []float64, refit bool, sampleWeight []float64) base.Fiter {
	yr, yc := Y.Dims()
	if yc != 1 {
		panic("GaussianNB fit: expected Y to have 1 column")
	}
	_, xc := X.Dims()
	_, varX, _ := meanvar(matfiltered{Matrix: X, filter: func(int) bool { return true }}, sampleWeight)
	m.Epsilon = m.VarSmoothing * floats.Max(varX)
	if refit {
		m.Classes = nil
	}
	firstCall := m.Theta == nil
	if firstCall {
		m.Classes = classes
		nFeatures := xc
		nClasses := len(m.Classes)
		m.Theta = mat.NewDense(nClasses, nFeatures, nil)
		m.Sigma = mat.NewDense(nClasses, nFeatures, nil)
		m.ClassCount = make([]float64, nClasses)
		if m.Priors != nil {
			priors := m.Priors
			if len(priors) != nClasses {
				panic("Number of priors must match number of classes.")
			}
			priorsSum := floats.Sum(priors)
			if math.Abs(priorsSum-1.) > 1e-6 {
				panic("The sum of the priors should be 1.")
			}
			priorsMin := floats.Min(priors)
			if priorsMin < 0 {
				panic("Priors must be non-negative.")
			}
			m.ClassPrior = make([]float64, len(priors))
			copy(m.ClassPrior, priors)
		} else {
			m.ClassPrior = make([]float64, len(m.Classes))
		}
	} else {
		width := func(X mat.Matrix) int { _, c := X.Dims(); return c }
		wx, wt := width(X), width(m.Theta)
		if wx != wt {
			panic(fmt.Errorf("Number of features %d does not match previous data %d", wx, wt))
		}
		m.Sigma.Apply(func(_, _ int, v float64) float64 { return v - m.Epsilon }, m.Sigma)
	}
	classes = m.Classes
	uniqueY := npUnique(colAsVector(Y, 0))
	var uniqueYnotinClasses []float64
	classmap := map[float64]int{}
	for index, yval := range classes {
		classmap[yval] = index
	}
	for _, yval := range uniqueY {
		if _, ok := classmap[yval]; !ok {
			uniqueYnotinClasses = append(uniqueYnotinClasses, yval)
		}
	}
	if len(uniqueYnotinClasses) > 0 {
		panic(fmt.Errorf("The target labels %g in y do not exist in the initial classes %g", uniqueYnotinClasses, classes))
	}
	base.Parallelize(runtime.GOMAXPROCS(0), len(uniqueY), func(th, start, end int) {
		for i := start; i < end; i++ {
			yi := uniqueY[i]
			i := classmap[yi]
			filter := func(j int) bool { return Y.At(j, 0) == yi }
			Xi := matfiltered{Matrix: X, filter: filter}
			var swi floatsfiltered
			swi = floatsfiltered{sampleWeight, filter, yr}
			setTotalMu := func(c int, v float64) { m.Theta.Set(i, c, v) }
			setTotalVar := func(c int, v float64) { m.Sigma.Set(i, c, v) }
			setClassCount := func(v float64) { m.ClassCount[i] = v }
			m.updateMeanVariance(m.ClassCount[i], m.Theta.RawRowView(i), m.Sigma.RawRowView(i), Xi, swi, setTotalMu, setTotalVar, setClassCount)
		}
	})
	m.Sigma.Apply(func(_, _ int, v float64) float64 { return v + m.Epsilon }, m.Sigma)
	if m.Priors == nil {
		# Empirical prior, with sample_weight taken into account
		self.class_prior_ = self.class_count_ / self.class_count_.sum()
		floats.ScaleTo(m.ClassPrior, 1/floats.Sum(m.ClassCount), m.ClassCount)
	}
	m.jointLogLikelihood = func(jll, xrow []float64) {
		for i := range m.Classes {
			jointi := math.Log(m.ClassPrior[i])
			nij := 0.
			sigmai := m.Sigma.RawRowView(i)
			thetai := m.Theta.RawRowView(i)
			for j := 0; j < len(xrow); j++ {
				nij -= .5 * math.Log(2*math.Pi*sigmai[j])
				xd := xrow[j] - thetai[j]
				nij -= .5 * xd * xd / sigmai[j]
			}
			jll[i] = jointi + nij
		}
	}
	return m
}
func ( m * GaussianNB ) PartialFit ( X , Y mat . Matrix , classes [ ] float64 , refit bool , sampleWeight [ ] float64 ) base . Fiter { yr , yc := Y . Dims ( ) if yc != 1 { panic ( " " ) } _ , xc := X . Dims ( ) _ , varX , _ := meanvar ( matfiltered { Matrix : X , filter : func ( int ) bool { return true } } , sampleWeight ) m . Epsilon = m . VarSmoothing * floats . Max ( varX ) if refit { m . Classes = nil } firstCall := m . Theta == nil if firstCall { m . Classes = classes nFeatures := xc nClasses := len ( m . Classes ) m . Theta = mat . NewDense ( nClasses , nFeatures , nil ) m . Sigma = mat . NewDense ( nClasses , nFeatures , nil ) m . ClassCount = make ( [ ] float64 , nClasses ) if m . Priors != nil { priors := m . Priors if len ( priors ) != nClasses { panic ( " " ) } priorsSum := floats . Sum ( priors ) if math . Abs ( priorsSum - 1. ) > 1e-6 { panic ( " " ) } priorsMin := floats . Min ( priors ) if priorsMin < 0 { panic ( " " ) } m . ClassPrior = make ( [ ] float64 , len ( priors ) ) copy ( m . ClassPrior , priors ) } else { m . ClassPrior = make ( [ ] float64 , len ( m . Classes ) ) } } else { width := func ( X mat . Matrix ) int { _ , c := X . Dims ( ) ; return c } wx , wt := width ( X ) , width ( m . Theta ) if wx != wt { panic ( fmt . Errorf ( " " , wx , wt ) ) } m . Sigma . Apply ( func ( _ , _ int , v float64 ) float64 { return v - m . Epsilon } , m . Sigma ) } classes = m . Classes uniqueY := npUnique ( colAsVector ( Y , 0 ) ) var uniqueYnotinClasses [ ] float64 classmap := map [ float64 ] int { } for index , yval := range classes { classmap [ yval ] = index } for _ , yval := range uniqueY { if _ , ok := classmap [ yval ] ; ! ok { uniqueYnotinClasses = append ( uniqueYnotinClasses , yval ) } } if len ( uniqueYnotinClasses ) > 0 { panic ( fmt . Errorf ( " " , uniqueYnotinClasses , classes ) ) } base . Parallelize ( runtime . GOMAXPROCS ( 0 ) , len ( uniqueY ) , func ( th , start , end int ) { for i := start ; i < end ; i ++ { yi := uniqueY [ i ] i := classmap [ yi ] filter := func ( j int ) bool { return Y . At ( j , 0 ) == yi } Xi := matfiltered { Matrix : X , filter : filter } var swi floatsfiltered swi = floatsfiltered { sampleWeight , filter , yr } setTotalMu := func ( c int , v float64 ) { m . Theta . Set ( i , c , v ) } setTotalVar := func ( c int , v float64 ) { m . Sigma . Set ( i , c , v ) } setClassCount := func ( v float64 ) { m . ClassCount [ i ] = v } m . updateMeanVariance ( m . ClassCount [ i ] , m . Theta . RawRowView ( i ) , m . Sigma . RawRowView ( i ) , Xi , swi , setTotalMu , setTotalVar , setClassCount ) } } ) m . Sigma . Apply ( func ( _ , _ int , v float64 ) float64 { return v + m . Epsilon } , m . Sigma ) if m . Priors == nil { floats . ScaleTo ( m . ClassPrior , 1 / floats . Sum ( m . ClassCount ) , m . ClassCount ) } m . jointLogLikelihood = func ( jll , xrow [ ] float64 ) { for i := range m . Classes { jointi := math . Log ( m . ClassPrior [ i ] ) nij := 0. sigmai := m . Sigma . RawRowView ( i ) thetai := m . Theta . RawRowView ( i ) for j := 0 ; j < len ( xrow ) ; j ++ { nij -= .5 * math . Log ( 2 * math . Pi * sigmai [ j ] ) xd := xrow [ j ] - thetai [ j ] nij -= .5 * xd * xd / sigmai [ j ] } jll [ i ] = jointi + nij } } return m }
PartialFit fit Gaussian Naive Bayes according to X, y
PartialFit fit Gaussian Naive Bayes according to X y
//# If the ratio of data variance between dimensions is too small, it //# will cause numerical errors. To address this, we artificially //# boost the variance by epsilon, a small fraction of the standard //# deviation of the largest dimension. //	self.epsilon_ = self.var_smoothing * np.var(X, axis=0).max() //# Initialise the class prior //# Take into account the priors //priors = np.asarray(self.priors) //# Check that the provide prior match the number of classes //# Check that the sum is 1 //# Check that the prior are non-negative //# Put epsilon back in each time //self.sigma_[:, :] -= self.epsilon_ // # Update if only no priors is provided // //# Empirical prior, with sample_weight taken into account //self.class_prior_ = self.class_count_ / self.class_count_.sum() /*
		   joint_log_likelihood = []
		      for i in range(np.size(self.classes_)):
		          jointi = np.log(self.class_prior_[i])
		          n_ij = - 0.5 * np.sum(np.log(2. * np.pi * self.sigma_[i, :]))
		          n_ij -= 0.5 * np.sum(((X - self.theta_[i, :]) ** 2) /
		                               (self.sigma_[i, :]), 1)
		          joint_log_likelihood.append(jointi + n_ij)


		*/