-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_v0.py
66 lines (51 loc) · 1.82 KB
/
alg_v0.py
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
import numpy as np
import matplotlib.pyplot as plt
# confirms the data characteristics
def nth_term_validator(coord: dict) -> bool:
if not isinstance(coord, dict) or len(coord) < 2:
print("Invalid input data. Expected a list with at least two elements.")
return False
return True
def polynomial_regression(coord: dict, param):
if nth_term_validator(coord):
order = len(coord) - 1
coeffs = np.polyfit(np.arange(len(coord)), list(coord.values()), order)
if isinstance(param, float):
# if the user asks for what was already given
if param in list(coord.keys()):
return coord.get(param)
# Perform polynomial regression to estimate the value
estimated_value = np.polyval(coeffs, param)
return estimated_value
elif isinstance(param, str):
if param == 'exp':
# Construct the polynomial expression
expression = ''
for i in range(order, -1, -1):
coeff = coeffs[order - i]
if coeff != 0:
term = f"{coeff}n^{i}" if i > 1 else f"{coeff}n" if i == 1 else str(coeff)
expression += term + " + "
expression = expression.rstrip(" + ")
return expression
elif param == 'graph':
# Plot the graph using the suggested polynomial expression
x_vals = np.arange(len(list(coord.values())))
y = np.array(list(coord.values()))
coeffs = np.polyfit(x_vals, y, order)
poly = np.poly1d(coeffs)
plt.plot(x_vals, y, 'bo', label='Data')
plt.plot(x_vals, poly(x_vals), 'r-', label='Polynomial Fit')
plt.xlabel('n')
plt.ylabel('f(n)')
plt.legend()
plt.show()
else:
raise ValueError("Invalid parameter. Expected 'exp' or 'graph'.")
else:
raise ValueError("Invalid parameter type. Expected a float or a string.")
# Example usage
data = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
parameter = 100.0
output = polynomial_regression(data, parameter)
print(output)