ahmet / ft_linear_regression
Herkese Açık Ahmet Kerem Bonusu ile birlikte bitti
ft_linear_regression predict.py
predict.py 961 B
import csv
THETAS_PATH = "thetas.csv"
def estimate_price(theta0, theta1, mileage):
"""estimatePrice(mileage) = theta0 + (theta1 * mileage)"""
return theta0 + (theta1 * mileage)
def load_thetas():
"""Both thetas default to 0 until the training program has run."""
try:
with open(THETAS_PATH, newline="") as f:
row = next(csv.DictReader(f))
return float(row["theta0"]), float(row["theta1"])
except (FileNotFoundError, StopIteration, KeyError, TypeError, ValueError):
return 0.0, 0.0
def main():
theta0, theta1 = load_thetas()
try:
mileage = float(input("Mileage: "))
except (ValueError, EOFError, KeyboardInterrupt):
print("\nPlease enter a valid number.")
return
if mileage < 0:
print("Mileage cannot be negative.")
return
print(f"Estimated price: {estimate_price(theta0, theta1, mileage):.2f}")
if __name__ == "__main__":
main()