def test_get_ordered_weights(self): expected_results = { 'additional_indemnity_money': { 'additional_facts': ['asker_is_tenant', 'bothers_others'], 'important_facts': [ 'asker_is_landlord', 'tenant_monthly_payment', 'tenant_not_paid_lease_timespan' ] } } class MockClassifierModel: def __init__(self): self.estimators_ = [] class MockEstimator: def __init__(self): self.coef_ = np.array([[ -1.52808095e-05, 1.99961642e+00, 1.85962826e-04, 3.77475828e-15, -3.83904518e-05, -4.15205874e-04 ]]) mock_classifier_labels = { 0: ('additional_indemnity_money', 'int'), } mock_label_column_index = { 'outcomes_vector': [(0, 'additional_indemnity_money', 'int'), (1, 'declares_housing_inhabitable', 'bool'), (2, 'declares_resiliation_is_correct', 'bool')], 'facts_vector': [(0, 'apartment_dirty', 'bool'), (1, 'asker_is_landlord', 'bool'), (2, 'asker_is_tenant', 'bool'), (3, 'bothers_others', 'bool'), (4, 'disrespect_previous_judgement', 'bool'), (5, 'landlord_inspector_fees', 'int')] } mock_estimator = MockEstimator() mock_classifier_model = MockClassifierModel() mock_classifier_model.estimators_.append(mock_estimator) linear_svc = MultiClassSVM(None) linear_svc.model = mock_classifier_model linear_svc.classifier_labels = mock_classifier_labels linear_svc.label_column_index = mock_label_column_index MlController.classifier_model = linear_svc result = MlController.get_weighted_facts() self.assertEqual(result, expected_results)
def test_fact_dict_to_vector(self): # Test data input_json = json.loads(self.test_json) # Execute result = MlController.fact_dict_to_vector(input_json['facts']) # Verify assert_array_equal(result, [ 0., 0., 0., 1., 0., 0., 0., 0., 48., 0., 1., 0., 0., 0., 31., 0., 0., 540., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 674., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. ])
def test_probability_vector_to_dict(self): probability_vector = np.array([0, 1, 0, 0, 0, 1, 1, 0, 1, 0]) MlController.classifier_labels = self.mock_classifier_index result = MlController.probability_vector_to_dict(probability_vector) expected_dict = { 'rejects_tenant_demand': '1', 'orders_expulsion': '0', 'declares_resiliation_is_correct': '0', 'tenant_ordered_to_pay_landlord': '0', 'orders_tenant_pay_first_of_month': '1', 'orders_immediate_execution': '0', 'rejects_landlord_demand': '0', 'declares_housing_inhabitable': '1', 'orders_resiliation': '1', 'additional_indemnity_money': '0' } self.assertEqual(expected_dict, result)
def test_get_ml_statistics(self): result = MlController.get_ml_statistics() self.assertTrue(isinstance(result, dict))
def test_get_anti_fact(self): anti_facts = MlController.get_anti_facts() self.assertTrue(isinstance(anti_facts, dict))
def classify_claim_category(): input_json = request.get_json() return jsonify(MlController.predict_outcome(input_json))
def get_ml_metrics(): return jsonify(MlController.get_ml_statistics())
def get_anti_facts(): return jsonify(MlController.get_anti_facts())
def get_ordered_weights(): return jsonify(MlController.get_weighted_facts())