def test_lr(self): self.model = CrossEntropyMNL( solver='newton-cg', fit_intercept=True, est_stderr=True, reg_method='l2', alpha=10, l1_ratio=0, tol=1e-4, max_iter=100, coef=None, stderr=None, n_classes=None) self.model.fit(self.data_anes96.exog, self.y) # coefficient # predict self.assertEqual( np.sum(self.model.predict(self.data_anes96.exog) == self.data_anes96.endog), 333) # loglike/_per_sample self.assertAlmostEqual( self.model.loglike(self.data_anes96.exog, self.y), -1540.888458338286, places=3) # to_json json_dict = self.model.to_json('./tests/linear_models/CrossentropyMNL/Multinomial/') self.assertEqual(json_dict['properties']['solver'], 'newton-cg') # from_json self.model_from_json = CrossEntropyMNL.from_json(json_dict) np.testing.assert_array_almost_equal( self.model.coef, self.model_from_json.coef, decimal=3) np.testing.assert_array_almost_equal( self.model.classes, np.array(list(range(7))), decimal=3) self.assertEqual(self.model.n_classes, 7)
def test_lr(self): self.model = CrossEntropyMNL(solver='lbfgs', fit_intercept=True, est_stderr=True, reg_method='l2', alpha=0, l1_ratio=0, tol=1e-4, max_iter=100, coef=None, stderr=None, n_classes=None) self.model.fit(self.data_spector.exog, self.y) # coefficient np.testing.assert_array_almost_equal( self.model.coef, np.array([[-13.021, 2.8261, .09515, 2.378]]), decimal=3) # predict np.testing.assert_array_almost_equal( self.model.predict(self.data_spector.exog), np.array((0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 1., 1., 0., 1., 0., 1., 1., 1., 0.)), decimal=3) # loglike/_per_sample self.assertAlmostEqual(self.model.loglike(self.data_spector.exog, self.y), -12.8896334653335, places=3) # to_json json_dict = self.model.to_json( './tests/linear_models/CrossentropyMNL/Binary/') self.assertEqual(json_dict['properties']['solver'], 'lbfgs') # from_json self.model_from_json = CrossEntropyMNL.from_json(json_dict) np.testing.assert_array_almost_equal(self.model.coef, self.model_from_json.coef, decimal=3) np.testing.assert_array_almost_equal(self.model.classes, np.array([0, 1]), decimal=3) self.assertEqual(self.model.n_classes, 2)
def test_lr(self): self.model = CrossEntropyMNL(solver='newton-cg', fit_intercept=True, est_stderr=True, reg_method='l2', alpha=10, l1_ratio=0, tol=1e-4, max_iter=100, coef=None, stderr=None, n_classes=None) self.model.fit(self.data_anes96.exog, self.y) # coefficient # predict self.assertEqual( np.sum( self.model.predict(self.data_anes96.exog) == self.data_anes96.endog), 333) # loglike/_per_sample self.assertAlmostEqual(self.model.loglike(self.data_anes96.exog, self.y), -1540.888458338286, places=3) # to_json json_dict = self.model.to_json( './tests/linear_models/CrossentropyMNL/Multinomial/') self.assertEqual(json_dict['properties']['solver'], 'newton-cg') # from_json self.model_from_json = CrossEntropyMNL.from_json(json_dict) np.testing.assert_array_almost_equal(self.model.coef, self.model_from_json.coef, decimal=3) np.testing.assert_array_almost_equal(self.model.classes, np.array(list(range(7))), decimal=3) self.assertEqual(self.model.n_classes, 7)
def test_lr(self): self.model = CrossEntropyMNL( solver='lbfgs', fit_intercept=True, est_stderr=True, reg_method='l2', alpha=0, l1_ratio=0, tol=1e-4, max_iter=100, coef=None, stderr=None, n_classes=None) self.model.fit(self.data_spector.exog, self.y) # coefficient np.testing.assert_array_almost_equal( self.model.coef, np.array([[-13.021, 2.8261, .09515, 2.378]]), decimal=3) # predict np.testing.assert_array_almost_equal( self.model.predict(self.data_spector.exog), np.array((0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 1., 1., 0., 1., 0., 1., 1., 1., 0.)), decimal=3) # loglike/_per_sample self.assertAlmostEqual( self.model.loglike(self.data_spector.exog, self.y), -12.8896334653335, places=3) # to_json json_dict = self.model.to_json('./tests/linear_models/CrossentropyMNL/Binary/') self.assertEqual(json_dict['properties']['solver'], 'lbfgs') # from_json self.model_from_json = CrossEntropyMNL.from_json(json_dict) np.testing.assert_array_almost_equal( self.model.coef, self.model_from_json.coef, decimal=3) np.testing.assert_array_almost_equal( self.model.classes, np.array([0, 1]), decimal=3) self.assertEqual(self.model.n_classes, 2)