def test_card_without_combiner(): transer = WOETransformer() woe_X = transer.fit_transform(df, target) card = ScoreCard(transer=transer) card.fit(woe_X, target) score, sub = card.predict(df, return_sub=True) assert score[404] == pytest.approx(460.9789823549386, FUZZ_THRESHOLD)
def test_card_combiner_str_not_match(): c = combiner.export() c['C'] = [['A'], ['B'], ['C']] com = Combiner().load(c) bins = com.transform(df) woe_transer = WOETransformer() woe = woe_transer.fit_transform(bins, target) card = ScoreCard( combiner=com, transer=woe_transer, ) with pytest.raises(Exception) as e: # will raise an exception when fitting a card card.fit(woe, target) assert '\'C\' is not matched' in str(e.value)
def test_card_combiner_str_not_match(): c = combiner.export() c['C'] = [['A'], ['B'], ['C']] com = Combiner().set_rules(c) bins = com.transform(df) woe_transer = WOETransformer() woe = woe_transer.fit_transform(bins, target) model = LogisticRegression() model.fit(woe, target) with pytest.raises(Exception) as e: # will raise an exception when create a card card = ScoreCard( combiner=com, transer=woe_transer, model=model, ) assert '\'C\' is not matched' in str(e.value)
print('AUC:', AUC(prob_dev, y)) prob_off = lr.predict_proba(offx)[:, 1] print('测试集') print('F1:', F1(prob_off, offy)) print('KS:', KS(prob_off, offy)) print('AUC:', AUC(prob_off, offy)) print('模型PSI:', toad.metrics.PSI(prob_dev, prob_off)) print('特征PSI:', '\n', toad.metrics.PSI(x, offx).sort_values(0)) off_bucket = toad.metrics.KS_bucket(prob_off, offy, bucket=10, method='quantile') print('off_bucket', off_bucket) from toad.scorecard import ScoreCard card = ScoreCard(combiner=combiner, transer=t, class_weight='balanced', C=0.1, base_score=600, base_odds=35, pdo=60, rate=2) card.fit(x, y) final_card = card.export(to_frame=True) final_card.to_csv('D:/0.学习/python/屁屁和铭仔的数据之路/xjh_toad_train_rule.csv', sep=',', index=None)
def test_card_map(): config = card.export() card_from_map = ScoreCard(card=config) score = card_from_map.predict(df) assert score[404] == TEST_SCORE
def test_export_frame(): card = ScoreCard(card=card_config) frame = card.export(to_frame=True) rows = frame[(frame['name'] == 'B') & (frame['value'] == 'else')].reset_index() assert rows.loc[0, 'score'] == 500
def test_generate_testing_frame(): card = ScoreCard(card=card_config) frame = card.testing_frame() assert frame.loc[4, 'B'] == 'E'
def test_card_map_with_else(): card_from_map = ScoreCard(card=card_config) score = card_from_map.predict(df) assert score[80] == 1000
'else': 500, }, 'C': { 'A': 200, 'B': 100, }, } combiner = Combiner() bins = combiner.fit_transform(df, target, n_bins=5) woe_transer = WOETransformer() woe = woe_transer.fit_transform(bins, target) # create a score card card = ScoreCard( combiner=combiner, transer=woe_transer, ) card.fit(woe, target) FUZZ_THRESHOLD = 1e-4 TEST_SCORE = pytest.approx(453.58, FUZZ_THRESHOLD) def test_proba_to_score(): model = LogisticRegression() model.fit(woe, target) proba = model.predict_proba(woe)[:, 1] score = card.proba_to_score(proba) assert score[404] == TEST_SCORE
def test_export_frame(): card = ScoreCard(card = card_config) frame = card.export(to_frame = True) assert frame.loc[6, 'value'] == 'else'
}, } combiner = Combiner() bins = combiner.fit_transform(df, target, n_bins = 5) woe_transer = WOETransformer() woe = woe_transer.fit_transform(bins, target) model = LogisticRegression() # fit model by woe model.fit(woe, target) # create a score card card = ScoreCard( combiner = combiner, transer = woe_transer, model = model, ) FUZZ_THRESHOLD = 1e-4 TEST_SCORE = pytest.approx(453.58, FUZZ_THRESHOLD) def test_proba_to_score(): proba = model.predict_proba(woe)[:,1] score = card.proba_to_score(proba) assert score[404] == TEST_SCORE def test_predict(): score = card.predict(df)