mlprofessor 0.1.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mlprofessor/__init__.py +26 -0
- mlprofessor/__main__.py +7 -0
- mlprofessor/cli.py +149 -0
- mlprofessor/columns.py +524 -0
- mlprofessor/errors.py +110 -0
- mlprofessor/extensions.py +29 -0
- mlprofessor/leakage.py +588 -0
- mlprofessor/load.py +330 -0
- mlprofessor/metrics.py +243 -0
- mlprofessor/models.py +149 -0
- mlprofessor/pipeline.py +1692 -0
- mlprofessor/preprocess.py +108 -0
- mlprofessor/result.py +177 -0
- mlprofessor/split.py +482 -0
- mlprofessor/stages.py +29 -0
- mlprofessor/task.py +172 -0
- mlprofessor/uncertainty.py +282 -0
- mlprofessor-0.1.0a1.dist-info/METADATA +89 -0
- mlprofessor-0.1.0a1.dist-info/RECORD +23 -0
- mlprofessor-0.1.0a1.dist-info/WHEEL +5 -0
- mlprofessor-0.1.0a1.dist-info/entry_points.txt +2 -0
- mlprofessor-0.1.0a1.dist-info/licenses/LICENSE +21 -0
- mlprofessor-0.1.0a1.dist-info/top_level.txt +1 -0
mlprofessor/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""mlprofessor: how predictable one target column of one table is (spec 2.1)."""
|
|
2
|
+
from mlprofessor.result import ProfileResult
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def profile(data, target, task="auto", time_col=None, gap=0,
|
|
6
|
+
profile="full", seed=0, out_dir=None, n_jobs=-1, duplicates="error",
|
|
7
|
+
group_col=None):
|
|
8
|
+
"""Measure how predictable `target` is in `data`. Returns a ProfileResult.
|
|
9
|
+
|
|
10
|
+
Every call says two things: `time_col` (a column's name or False) and
|
|
11
|
+
`group_col` (a column's name or False). None, the default of each, is "not
|
|
12
|
+
said", and the run stops with a named error (spec 2.1, step 5).
|
|
13
|
+
|
|
14
|
+
`profile="fast"` keeps at most 2,000 usable rows (step 6). With an entity
|
|
15
|
+
column and no time column it keeps whole entities, always every entity of
|
|
16
|
+
a quarter or more of the usable rows; when it cannot, the run stops with
|
|
17
|
+
`fast_profile_group_too_large` and the full profile is the answer.
|
|
18
|
+
"""
|
|
19
|
+
from mlprofessor.pipeline import run
|
|
20
|
+
|
|
21
|
+
return run(data, target, task=task, time_col=time_col, gap=gap, profile=profile,
|
|
22
|
+
seed=seed, out_dir=out_dir, n_jobs=n_jobs, duplicates=duplicates,
|
|
23
|
+
group_col=group_col)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
__all__ = ["profile", "ProfileResult"]
|
mlprofessor/__main__.py
ADDED
mlprofessor/cli.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Command line (spec 2.2): a thin wrapper over `profile()`.
|
|
2
|
+
|
|
3
|
+
Exit codes: 0 finished, 2 unparsed command line (argparse), 3 a named error.
|
|
4
|
+
Each call says whether the rows are ordered in time (--time-col or
|
|
5
|
+
--no-time-order) and whether rows belong together (--group-col or
|
|
6
|
+
--no-entities); a pair left out passes None, and the run stops as profile() does.
|
|
7
|
+
"""
|
|
8
|
+
import argparse
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
from mlprofessor import errors
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _build_parser():
|
|
15
|
+
parser = argparse.ArgumentParser(
|
|
16
|
+
prog="python -m mlprofessor",
|
|
17
|
+
description="Measure how predictable one target column of one table is.",
|
|
18
|
+
)
|
|
19
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
20
|
+
run = commands.add_parser("profile", help="profile one target column")
|
|
21
|
+
run.add_argument("--data", required=True, metavar="FILE.csv",
|
|
22
|
+
help="the table, as a CSV file")
|
|
23
|
+
run.add_argument("--target", required=True, metavar="COLUMN",
|
|
24
|
+
help="the target column")
|
|
25
|
+
run.add_argument("--out", required=True, metavar="FOLDER",
|
|
26
|
+
help="folder for profile.json and predictions.csv")
|
|
27
|
+
run.add_argument("--task", default="auto",
|
|
28
|
+
choices=["auto", "classification", "regression"],
|
|
29
|
+
help="the task (default: auto)")
|
|
30
|
+
time_order = run.add_mutually_exclusive_group()
|
|
31
|
+
time_order.add_argument("--time-col", default=None, metavar="COLUMN",
|
|
32
|
+
help="the time column; enables the time-ordered scheme. One "
|
|
33
|
+
"of --time-col and --no-time-order is needed")
|
|
34
|
+
time_order.add_argument("--no-time-order", action="store_true",
|
|
35
|
+
help="state that the order of the rows in time does not "
|
|
36
|
+
"matter: split at random even when a column holds dates. "
|
|
37
|
+
"One of --time-col and --no-time-order is needed")
|
|
38
|
+
run.add_argument("--gap", type=int, default=0, metavar="N",
|
|
39
|
+
help="whole timestamps between training and holdout (default: 0)")
|
|
40
|
+
run.add_argument("--profile", default="full", choices=["full", "fast"],
|
|
41
|
+
help="full or fast (default: full). fast keeps at most 2,000 "
|
|
42
|
+
"usable rows: the latest whole timestamps under --time-col; "
|
|
43
|
+
"whole entities under --group-col, always every entity of a "
|
|
44
|
+
"quarter or more of the rows, and it stops with "
|
|
45
|
+
"fast_profile_group_too_large when it cannot")
|
|
46
|
+
run.add_argument("--seed", type=int, default=0, metavar="N",
|
|
47
|
+
help="the random seed (default: 0)")
|
|
48
|
+
run.add_argument("--n-jobs", type=int, default=-1, metavar="N",
|
|
49
|
+
help="threads; -1 for every core (default: -1)")
|
|
50
|
+
run.add_argument("--duplicates", default="error", choices=["error", "keep", "group"],
|
|
51
|
+
help="rows that are copies of one another (no time column): stop when "
|
|
52
|
+
"holdout rows have copies among the training rows (error), go "
|
|
53
|
+
"ahead with a warning (keep), or keep together the rows that agree "
|
|
54
|
+
"in every feature column, whatever their targets, so that no "
|
|
55
|
+
"combination of feature values is on both sides of the split or "
|
|
56
|
+
"in two folds (group) (default: error)")
|
|
57
|
+
entities = run.add_mutually_exclusive_group()
|
|
58
|
+
entities.add_argument("--group-col", default=None, metavar="COLUMN",
|
|
59
|
+
help="the entity column: the rows of one entity (a customer, a "
|
|
60
|
+
"patient, a device) are kept together. One of --group-col "
|
|
61
|
+
"and --no-entities is needed")
|
|
62
|
+
entities.add_argument("--no-entities", action="store_true",
|
|
63
|
+
help="state that no rows belong to one entity. One of "
|
|
64
|
+
"--group-col and --no-entities is needed")
|
|
65
|
+
return parser
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def main(argv=None):
|
|
69
|
+
"""Run the command line and return its exit code."""
|
|
70
|
+
from mlprofessor import profile
|
|
71
|
+
|
|
72
|
+
args = _build_parser().parse_args(argv)
|
|
73
|
+
time_col = False if args.no_time_order else args.time_col
|
|
74
|
+
group_col = False if args.no_entities else args.group_col
|
|
75
|
+
try:
|
|
76
|
+
result = profile(
|
|
77
|
+
args.data, args.target, task=args.task, time_col=time_col,
|
|
78
|
+
gap=args.gap, profile=args.profile, seed=args.seed,
|
|
79
|
+
out_dir=args.out, n_jobs=args.n_jobs, duplicates=args.duplicates,
|
|
80
|
+
group_col=group_col,
|
|
81
|
+
)
|
|
82
|
+
except errors.ProfileError as exc:
|
|
83
|
+
print(f"error: {exc.code}: {exc}", file=sys.stderr)
|
|
84
|
+
return 3
|
|
85
|
+
print(_summary(result.to_dict(), args.out))
|
|
86
|
+
return 0
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _number(value):
|
|
90
|
+
return "n/a" if value is None else f"{value:.6g}"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _rests_on_line(headline):
|
|
94
|
+
"""One line for what the headline rests on (step 12a): the floor, and each listed
|
|
95
|
+
column in quotes with its fall; `none` for an empty list."""
|
|
96
|
+
floor = headline.get("rests_on_floor")
|
|
97
|
+
listed = headline.get("rests_on") or []
|
|
98
|
+
if floor is None:
|
|
99
|
+
head = "rests on (no floor: skill has no value)"
|
|
100
|
+
else:
|
|
101
|
+
head = f"rests on (columns whose skill falls by at least {_number(floor)} shuffled)"
|
|
102
|
+
if not listed:
|
|
103
|
+
return f"{head}: none"
|
|
104
|
+
return f"{head}: " + ", ".join(
|
|
105
|
+
f"{entry['column']!r} {_number(entry['skill_fall'])}" for entry in listed)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _summary(document, out_dir):
|
|
109
|
+
"""Plain lines: the task, the rows, the headline, its spread, each warning, the files."""
|
|
110
|
+
headline = document["headline"]
|
|
111
|
+
rows = {record["item"]: record["count"] for record in document["rows"]}
|
|
112
|
+
if headline["skill"] is None:
|
|
113
|
+
skill_text = "none (see skill_undefined)"
|
|
114
|
+
else:
|
|
115
|
+
skill_text = _number(headline["skill"])
|
|
116
|
+
lines = [
|
|
117
|
+
f"task: {document['task']['type']} ({document['task']['decided_by']}), "
|
|
118
|
+
f"scheme: {document['validation']['scheme']}",
|
|
119
|
+
f"rows: {rows['loaded']} loaded, {rows['training']} training, "
|
|
120
|
+
f"{rows['holdout']} holdout",
|
|
121
|
+
f"headline: {headline['metric']} {_number(headline['score'])} with "
|
|
122
|
+
f"{headline['model']}; baseline {headline['baseline']} "
|
|
123
|
+
f"{_number(headline['baseline_score'])}; skill {skill_text}",
|
|
124
|
+
]
|
|
125
|
+
if headline["interval_method"] is None:
|
|
126
|
+
lines.append(f"interval: none: {headline['interval_reason']}")
|
|
127
|
+
else:
|
|
128
|
+
level = f"{100 * headline['interval_level']:g}%"
|
|
129
|
+
lines.append(
|
|
130
|
+
f"interval ({level}, {headline['interval_method']}): score "
|
|
131
|
+
f"{_number(headline['interval_low'])} to {_number(headline['interval_high'])}")
|
|
132
|
+
if headline["skill_low"] is None:
|
|
133
|
+
lines.append("interval on skill: none (see the warnings)")
|
|
134
|
+
else:
|
|
135
|
+
lines.append(f"interval on skill: {_number(headline['skill_low'])} to "
|
|
136
|
+
f"{_number(headline['skill_high'])}")
|
|
137
|
+
if headline["p_value"] is None:
|
|
138
|
+
lines.append(f"p-value: none: {headline['p_value_reason']}")
|
|
139
|
+
else:
|
|
140
|
+
lines.append(f"p-value: {_number(headline['p_value'])} "
|
|
141
|
+
f"({headline['n_permutations']} shuffles)")
|
|
142
|
+
lines.append(_rests_on_line(headline))
|
|
143
|
+
lines.append(f"warnings: {len(document['warnings'])}")
|
|
144
|
+
for warning in document["warnings"]:
|
|
145
|
+
where = f" [{warning['column']}]" if warning["column"] is not None else ""
|
|
146
|
+
message = " ".join(str(warning["message"]).split())
|
|
147
|
+
lines.append(f" {warning['code']}{where}: {message}")
|
|
148
|
+
lines.append(f"wrote profile.json and predictions.csv to {out_dir}")
|
|
149
|
+
return "\n".join(lines)
|