Repository navigation
Expand file tree
/
Copy pathpredict.py
More file actions
67 lines (54 loc) · 2.4 KB
/
Copy pathpredict.py
File metadata and controls
67 lines (54 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Classify one sensor reading with the saved KNN or GaussianNB model.
Uses the StandardScaler and LabelEncoder that were fitted at training
time. A fresh scaler fitted on a single row would collapse every
feature to zero and is not used here.
"""
import argparse
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import joblib
import pandas as pd
from generate_dataset import FEATURES
from train import default_model_path
def load_bundle(path=None):
path = Path(path) if path else default_model_path()
if not path.exists():
raise SystemExit(
f"No saved model at {path}. Run: python src/train.py"
)
return joblib.load(path)
def predict(values, model="knn", bundle=None):
bundle = bundle or load_bundle()
if len(values) != len(FEATURES):
raise ValueError(f"Expected {len(FEATURES)} features {FEATURES}, got {len(values)}")
X = pd.DataFrame([values], columns=FEATURES)
Xs = pd.DataFrame(bundle["scaler"].transform(X), columns=FEATURES)
est = bundle["knn"] if model == "knn" else bundle["nb"]
pred = est.predict(Xs)
label = bundle["encoder"].inverse_transform(pred)[0]
proba = None
if hasattr(est, "predict_proba"):
proba = dict(zip(bundle["encoder"].classes_, est.predict_proba(Xs)[0]))
return label, proba, Xs.iloc[0].to_dict()
def main():
p = argparse.ArgumentParser(description="Classify a robotic sensor reading.")
p.add_argument("--distance", type=float, required=True)
p.add_argument("--size", type=float, required=True)
p.add_argument("--color", type=float, required=True, dest="color_intensity")
p.add_argument("--weight", type=float, required=True)
p.add_argument("--voltage", type=float, required=True, dest="sensor_voltage")
p.add_argument("--model", choices=["knn", "nb"], default="knn")
args = p.parse_args()
values = [args.distance, args.size, args.color_intensity, args.weight, args.sensor_voltage]
label, proba, scaled = predict(values, model=args.model)
print("Input (raw): ", dict(zip(FEATURES, values)))
print("Input (scaled): ", {k: round(v, 4) for k, v in scaled.items()})
print(f"Model: {args.model}")
print(f"Predicted class: {label}")
if proba:
print("Class probabilities:")
for name, pr in proba.items():
print(f" {name:16s} {pr:.4f}")
if __name__ == "__main__":
main()