aboutsummaryrefslogtreecommitdiffstats
path: root/predict.py
blob: 3bbcb47c0ec953fddacf566cc6d3e467bebb9d91 (plain)
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
import os
import sys

THETAS_FILE = "thetas.csv"


def load_thetas():
    if not os.path.exists(THETAS_FILE):
        return 0.0, 0.0
    with open(THETAS_FILE) as f:
        lines = f.readlines()
        return float(lines[0]), float(lines[1])


def estimate_price(mileage, theta0, theta1):
    return theta0 + theta1 * mileage


def main():
    theta0, theta1 = load_thetas()
    if theta0 == 0.0 and theta1 == 0.0:
        print(f"Warning: no trained model found ({THETAS_FILE}), using θ0=0 θ1=0")
    else:
        print(f"Loaded θ0={theta0}, θ1={theta1}")
    while True:
        try:
            raw = input("\nMileage (km): ")
            mileage = float(raw)
            price = estimate_price(mileage, theta0, theta1)
            print(f"Estimated price: {price:.2f}")
        except ValueError:
            print("Please enter a valid number.")


if __name__ == "__main__":
    try:
        main()
    except (KeyboardInterrupt, EOFError):
        print()
        sys.exit(0)