-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecision.py
More file actions
39 lines (27 loc) · 1.12 KB
/
precision.py
File metadata and controls
39 lines (27 loc) · 1.12 KB
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
import numpy as np
def estimated_prices(mileages, theta0, theta1):
return [theta0 + (theta1 * m) for m in mileages]
def mean_absolute_error(est_prices, prices, m):
return sum(abs(ep - p) for ep, p in zip(est_prices, prices)) / m
def calculate_average_prices(prices, m):
return sum(prices) / m
def display_accuracy_percentage(mae, average_prices):
percentage = 100 * (1 - mae / average_prices)
print(f"The accuracy of the algorithm is {percentage:.2f}%.")
def main():
try:
with open("thetas.txt", "r") as file:
thetas = file.read()
thetas = [float(theta) for theta in thetas.split(sep="\n")]
data = np.loadtxt("data.csv", delimiter=",", skiprows=1)
mileages = data[:, 0]
prices = data[:, 1]
est_prices = estimated_prices(mileages, thetas[0], thetas[1])
m = len(prices)
mae = mean_absolute_error(est_prices, prices, m)
average_prices = calculate_average_prices(prices, m)
display_accuracy_percentage(mae, average_prices)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()