add: estimate program
This commit is contained in:
19
estimate.py
Normal file
19
estimate.py
Normal file
@@ -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))
|
||||||
17
model.py
17
model.py
@@ -1,6 +1,7 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
import csv
|
||||||
|
|
||||||
class Model:
|
class Model:
|
||||||
def __init__(self, data_name="data.csv", learning_rate=0.01, epochs=2000):
|
def __init__(self, data_name="data.csv", learning_rate=0.01, epochs=2000):
|
||||||
@@ -17,6 +18,7 @@ class Model:
|
|||||||
self.cost_history = []
|
self.cost_history = []
|
||||||
self.m = len(self.data)
|
self.m = len(self.data)
|
||||||
self.normalize_values()
|
self.normalize_values()
|
||||||
|
|
||||||
def __estimate_price(self, mileage):
|
def __estimate_price(self, mileage):
|
||||||
return self.theta0 + (self.theta1 * mileage)
|
return self.theta0 + (self.theta1 * mileage)
|
||||||
|
|
||||||
@@ -46,7 +48,18 @@ class Model:
|
|||||||
tmp_t0, tmp_t1 = self.calculate_thetas()
|
tmp_t0, tmp_t1 = self.calculate_thetas()
|
||||||
self.theta0 -= self.learning_rate * tmp_t0
|
self.theta0 -= self.learning_rate * tmp_t0
|
||||||
self.theta1 -= self.learning_rate * tmp_t1
|
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):
|
def visualize(self):
|
||||||
km_range = np.linspace(min(self.data["km"]), max(self.data["km"]), 100)
|
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))
|
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.set_ylabel("Coût")
|
||||||
cost.grid(True)
|
cost.grid(True)
|
||||||
plt.show()
|
plt.show()
|
||||||
pass
|
|
||||||
|
|
||||||
first_model = Model()
|
first_model = Model()
|
||||||
first_model.train()
|
first_model.train()
|
||||||
|
first_model.export()
|
||||||
first_model.visualize()
|
first_model.visualize()
|
||||||
|
|||||||
2
thetas.csv
Normal file
2
thetas.csv
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
theta0,theta1
|
||||||
|
8499.599649806703,-0.02144896359049913
|
||||||
|
Reference in New Issue
Block a user