-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestimate.py
More file actions
executable file
·94 lines (77 loc) · 2.76 KB
/
estimate.py
File metadata and controls
executable file
·94 lines (77 loc) · 2.76 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
import sys
import numpy as np
from src.config import load_filenames, load_data
from src.ascii_format import INFO, ERROR, DONE, BG_YELLOW, RED, RESET
from typing import Any
"""
This program will be used to predict the y value for a given X value.
When you launch the program, it should prompt you for the X value, and then
give you back the estimated y value for that X value.
The program will use the following hypothesis to predict the y value:
estimateY(X_value) = θ0 + θ1 ∗ X_value
"""
def estimate(
theta0: float, theta1: float, X: float, x_input: float, verbose=False
) -> Any:
"""
'theta0' and 'theta1' are the parameters.
'theta0' is the intercept (constant term).
'theta1' is the slope (how much y changes with X).
'input_x' is the input feature (an individual measurable property).
"""
if verbose:
print(f"{INFO} Normalizing feature values...")
"""
Normalization:
- The mean is the average of a set of numbers, representing the
central value of the dataset.
- The standard deviation measures the spread or dispersion of data
points from the mean.
"""
X_mean = np.mean(X)
X_std = np.std(X)
x_input_normalized = (x_input - X_mean) / X_std
if verbose:
print(f"{INFO} Making estimation...")
print(f"{INFO} Using hypothesis: estimateY(x) = θ0 + (θ1 * x)")
return theta0 + (x_input_normalized * theta1)
def main():
print(f"{INFO} Loading data...")
# Load filenames from the configuration file
filenames = load_filenames()
# Unpack the filenames into two separate variables
if len(filenames) >= 2:
thetaset_filename, dataset_filename = filenames
else:
print(
f"{ERROR} Missing filename(s) in the configuration file.\n",
file=sys.stderr
)
# Get thetaset and feature values
X_label, y_label, theta0, theta1, X, _ = load_data(
thetaset_filename, dataset_filename
)
print(
f"{INFO} This program will predict the y value ({y_label}) "
f"from its X value ({X_label}).\n"
)
try: # Prompt the user for mileage
input_x = int(input(f"├── Enter the X value ({X_label}): "))
if input_x < 0:
raise ValueError()
except ValueError:
print(
f"{ERROR} Invalid input. Please enter a positive number.",
file=sys.stderr
)
sys.exit(1)
# Predict the y value. We only return positive values as
# y values should not be negative
predicted_y = max(0, estimate(theta0, theta1, X, input_x, True))
print(
f"{DONE} Predicted {y_label}: "
f"{BG_YELLOW}{RED} {predicted_y} {RESET}\n"
)
if __name__ == "__main__":
main()