blob: acf1391c3664e20c904022b05b118cb8b5c83008 (
plain)
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
40
41
42
43
44
45
|
import csv
DATA_FILE = "data.csv"
THETAS_FILE = "thetas.csv"
def load_thetas():
with open(THETAS_FILE) as f:
lines = f.readlines()
return float(lines[0]), float(lines[1])
def load_data():
km = []
price = []
with open(DATA_FILE) as f:
reader = csv.reader(f)
next(reader)
for row in reader:
km.append(float(row[0]))
price.append(float(row[1]))
return km, price
def mean(vals):
return sum(vals) / len(vals)
def estimate_price(mileage, theta0, theta1):
return theta0 + theta1 * mileage
def main():
kms, prices = load_data()
theta0, theta1 = load_thetas()
predicted_prices = [estimate_price(x, theta0, theta1) for x in kms]
mean_price = mean(prices)
r2 = 1 - sum((p - pp) ** 2 for p, pp in zip(prices, predicted_prices)) / sum(
(p - mean_price) ** 2 for p in prices
)
print(f"R-squared = {r2}")
if __name__ == "__main__":
main()
|