From 7ea02216304190775c1945cf5cfc7667c22835df Mon Sep 17 00:00:00 2001 From: Namonay Date: Sun, 4 May 2025 21:43:08 +0200 Subject: [PATCH] add: estimate program --- estimate.py | 19 +++++++++++++++++++ model.py | 17 +++++++++++++++-- thetas.csv | 2 ++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 estimate.py create mode 100644 thetas.csv diff --git a/estimate.py b/estimate.py new file mode 100644 index 0000000..f9af731 --- /dev/null +++ b/estimate.py @@ -0,0 +1,19 @@ +import pandas as pd + +class Estimate: + def __init__(self, thetas_path="thetas.csv"): + self.path = thetas_path + self.theta0 = 0 + self.theta1 = 0 + self.get_thetas() + def get_thetas(self): + with open(self.path, 'r') as file: + self.data = pd.read_csv(file) + self.theta0 = self.data["theta0"].iloc[0] + self.theta1 = self.data["theta1"].iloc[0] + def estimate_price(self, mileage): + return self.theta0 + (self.theta1 * mileage) + +estimate = Estimate() +input = int(input("Please enter the mileage of your car: ")) +print("The estimated price for a", input, "kilometers car is :", estimate.estimate_price(input)) diff --git a/model.py b/model.py index a5d9e34..9e44967 100644 --- a/model.py +++ b/model.py @@ -1,6 +1,7 @@ import pandas as pd import numpy as np import matplotlib.pyplot as plt +import csv class Model: def __init__(self, data_name="data.csv", learning_rate=0.01, epochs=2000): @@ -17,6 +18,7 @@ class Model: self.cost_history = [] self.m = len(self.data) self.normalize_values() + def __estimate_price(self, mileage): return self.theta0 + (self.theta1 * mileage) @@ -46,7 +48,18 @@ class Model: tmp_t0, tmp_t1 = self.calculate_thetas() self.theta0 -= self.learning_rate * tmp_t0 self.theta1 -= self.learning_rate * tmp_t1 - pass + + def export(self): + theta1_raw = self.theta1 / (self.km_max - self.km_min) + theta0_raw = self.theta0 - self.theta1 * self.km_min / (self.km_max - self.km_min) + data = [ + ["theta0", "theta1"], + [theta0_raw, theta1_raw] + ] + with open("thetas.csv", mode="w", newline="") as file: + writer = csv.writer(file) + + writer.writerows(data) def visualize(self): km_range = np.linspace(min(self.data["km"]), max(self.data["km"]), 100) predicted_prices = self.theta0 + (self.theta1 * (km_range - self.km_min) / (self.km_max - self.km_min)) @@ -67,8 +80,8 @@ class Model: cost.set_ylabel("Coût") cost.grid(True) plt.show() - pass first_model = Model() first_model.train() +first_model.export() first_model.visualize() diff --git a/thetas.csv b/thetas.csv new file mode 100644 index 0000000..51be09d --- /dev/null +++ b/thetas.csv @@ -0,0 +1,2 @@ +theta0,theta1 +8499.599649806703,-0.02144896359049913