aboutsummaryrefslogtreecommitdiffstats
path: root/predict.py
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-03-23 21:17:11 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-03-23 21:17:11 +0100
commitfd5fe70ce5271f09303b51dae34b42acc47f5730 (patch)
tree551b9b7c38b5fb8307cea3653e269ae79fb4b639 /predict.py
downloadft_linear_regression-fd5fe70ce5271f09303b51dae34b42acc47f5730.tar.gz
ft_linear_regression-fd5fe70ce5271f09303b51dae34b42acc47f5730.zip
Initial commit: linear regression for car price prediction
Training, prediction, and visualization programs using gradient descent with min-max normalization.
Diffstat (limited to 'predict.py')
-rw-r--r--predict.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/predict.py b/predict.py
new file mode 100644
index 0000000..3bbcb47
--- /dev/null
+++ b/predict.py
@@ -0,0 +1,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)