@@ -668,3 +668,45 @@ def test_alpha():
668668 X , y , classes = [0 , 1 ])
669669 assert_raise_message (ValueError , expected_msg , m_nb .partial_fit ,
670670 X , y , classes = [0 , 1 ])
671+
672+
673+ def test_alpha_vector ():
674+ X = np .array ([[1 , 0 ], [1 , 1 ]])
675+ y = np .array ([0 , 1 ])
676+
677+ # Setting alpha=np.array with same length
678+ # as number of features should be fine
679+ alpha = np .array ([1 , 2 ])
680+ nb = MultinomialNB (alpha = alpha )
681+ nb .partial_fit (X , y , classes = [0 , 1 ])
682+
683+ # Test feature probabilities uses pseudo-counts (alpha)
684+ feature_prob = np .array ([[1 / 2 , 1 / 2 ], [2 / 5 , 3 / 5 ]])
685+ assert_array_almost_equal (nb .feature_log_prob_ , np .log (feature_prob ))
686+
687+ # Test predictions
688+ prob = np .array ([[5 / 9 , 4 / 9 ], [25 / 49 , 24 / 49 ]])
689+ assert_array_almost_equal (nb .predict_proba (X ), prob )
690+
691+ # Test alpha non-negative
692+ alpha = np .array ([1. , - 0.1 ])
693+ expected_msg = ('Smoothing parameter alpha = -1.0e-01. '
694+ 'alpha should be > 0.' )
695+ m_nb = MultinomialNB (alpha = alpha )
696+ assert_raise_message (ValueError , expected_msg , m_nb .fit , X , y )
697+
698+ # Test that too small pseudo-counts are replaced
699+ ALPHA_MIN = 1e-10
700+ alpha = np .array ([ALPHA_MIN / 2 , 0.5 ])
701+ m_nb = MultinomialNB (alpha = alpha )
702+ m_nb .partial_fit (X , y , classes = [0 , 1 ])
703+ assert_array_almost_equal (m_nb ._check_alpha (),
704+ [ALPHA_MIN , 0.5 ],
705+ decimal = 12 )
706+
707+ # Test correct dimensions
708+ alpha = np .array ([1. , 2. , 3. ])
709+ m_nb = MultinomialNB (alpha = alpha )
710+ expected_msg = ('alpha should be a scalar or a numpy array '
711+ 'with shape [n_features]' )
712+ assert_raise_message (ValueError , expected_msg , m_nb .fit , X , y )
0 commit comments