|
1 | 1 | import pandas as pd
|
| 2 | +import numpy as np |
2 | 3 | from dku_idtb_scoring.score import add_scoring_columns, get_scored_df_schema
|
3 | 4 | from dku_idtb_decision_tree.tree import ScoringTree
|
4 | 5 | from pytest import raises
|
5 | 6 |
|
| 7 | + |
6 | 8 | nodes = {
|
7 | 9 | "0": {
|
8 | 10 | "id": 0,
|
|
71 | 73 |
|
72 | 74 | def get_input_df():
|
73 | 75 | return pd.DataFrame([[.2, "u", "A"],
|
74 |
| - [7, pd.np.nan, "B"], |
| 76 | + [7, np.nan, "B"], |
75 | 77 | [4, "u", "A"],
|
76 | 78 | [3, "v", "A"],
|
77 |
| - [pd.np.nan, "u", "C"]], columns=("num", "cat", "target")) |
| 79 | + [np.nan, "u", "C"]], columns=("num", "cat", "target")) |
78 | 80 |
|
79 | 81 | def test_score():
|
80 | 82 | df = get_input_df()
|
81 | 83 | add_scoring_columns(tree, df, True)
|
82 | 84 | expected_df = pd.DataFrame([
|
83 | 85 | [.2, "u", "A", .8, .2, "A", str(["num < 4"]), 1.0, "hello there"],
|
84 |
| - [7, pd.np.nan, "B", pd.np.nan, pd.np.nan, pd.np.nan, str(["4 ≤ num", "cat not in {}".format(["u", "v"])]), 4.0, "general Kenobi"], |
| 86 | + [7, np.nan, "B", np.nan, np.nan, np.nan, str(["4 ≤ num", "cat not in {}".format(["u", "v"])]), 4.0, "general Kenobi"], |
85 | 87 | [4, "u", "A", .25, .75, "B", str(["4 ≤ num", "cat in {}".format(["u", "v"])]), 3.0, None],
|
86 | 88 | [3, "v", "A", .8, .2, "A", str(["num < 4"]), 1.0, "hello there"],
|
87 |
| - [pd.np.nan, "u", "C", .8, .2, "A", str(["num < 4"]), 1.0, "hello there"] |
| 89 | + [np.nan, "u", "C", .8, .2, "A", str(["num < 4"]), 1.0, "hello there"] |
88 | 90 | ], columns=("num", "cat", "target", "proba_A", "proba_B", "prediction", "decision_rule", "leaf_id", "label"))
|
89 | 91 | pd.testing.assert_frame_equal(df, expected_df)
|
90 | 92 |
|
91 | 93 | df = get_input_df()
|
92 | 94 | add_scoring_columns(tree, df, False, True, False)
|
93 | 95 | expected_df = pd.DataFrame([
|
94 | 96 | [.2, "u", "A", "A", str(["num < 4"]), 1.0, "hello there"],
|
95 |
| - [7, pd.np.nan, "B", pd.np.nan, str(["4 ≤ num", "cat not in {}".format(["u", "v"])]), 4.0, "general Kenobi"], |
| 97 | + [7, np.nan, "B", np.nan, str(["4 ≤ num", "cat not in {}".format(["u", "v"])]), 4.0, "general Kenobi"], |
96 | 98 | [4, "u", "A", "B", str(["4 ≤ num", "cat in {}".format(["u", "v"])]), 3.0, None],
|
97 | 99 | [3, "v", "A", "A", str(["num < 4"]), 1.0, "hello there"],
|
98 |
| - [pd.np.nan, "u", "C", pd.np.nan, str(["num < 4"]), 1.0, "hello there"] |
| 100 | + [np.nan, "u", "C", np.nan, str(["num < 4"]), 1.0, "hello there"] |
99 | 101 | ], columns=("num", "cat", "target", "prediction", "decision_rule", "leaf_id", "label"))
|
100 | 102 | pd.testing.assert_frame_equal(df, expected_df)
|
101 | 103 |
|
102 | 104 | df = get_input_df()
|
103 | 105 | add_scoring_columns(tree, df, False, True, True)
|
104 | 106 | expected_df = pd.DataFrame([
|
105 | 107 | [.2, "u", "A", "A", True, str(["num < 4"]), 1.0, "hello there"],
|
106 |
| - [7, pd.np.nan, "B", pd.np.nan, pd.np.nan, str(["4 ≤ num", "cat not in {}".format(["u", "v"])]), 4.0, "general Kenobi"], |
| 108 | + [7, np.nan, "B", np.nan, np.nan, str(["4 ≤ num", "cat not in {}".format(["u", "v"])]), 4.0, "general Kenobi"], |
107 | 109 | [4, "u", "A", "B", False, str(["4 ≤ num", "cat in {}".format(["u", "v"])]), 3.0, None],
|
108 | 110 | [3, "v", "A", "A", True, str(["num < 4"]), 1.0, "hello there"],
|
109 |
| - [pd.np.nan, "u", "C", pd.np.nan, pd.np.nan, str(["num < 4"]), 1.0, "hello there"] |
| 111 | + [np.nan, "u", "C", np.nan, np.nan, str(["num < 4"]), 1.0, "hello there"] |
110 | 112 | ], columns=("num", "cat", "target", "prediction", "prediction_correct", "decision_rule", "leaf_id", "label"))
|
111 | 113 | pd.testing.assert_frame_equal(df, expected_df)
|
112 | 114 |
|
|
0 commit comments