-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_v1.py
48 lines (40 loc) · 1.09 KB
/
alg_v1.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
import numpy as np
import matplotlib.pyplot as plt
def polytrend(data, degree):
x = np.array(list(data.keys()))
y = np.array(list(data.values()))
# Fit the polynomial
coeffs = np.polyfit(x, y, degree)
poly_func = np.poly1d(coeffs)
# Generate points for plotting
x_range = np.linspace(min(x), max(x), 100)
y_range = poly_func(x_range)
# Plot the graph
plt.figure()
plt.plot(x_range, y_range, label='Fitted Polynomial')
plt.scatter(x, y, c='r', label='Data Points')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Polynomial Fit')
plt.show()
# Extrapolation
def extrapolate(n):
return poly_func(n)
# Display function fit in specified format
func_fit = 'f(n) -> '
for i, coeff in enumerate(coeffs[::-1]):
power = degree - i
if power == 0:
func_fit += f'{coeff:.2f}'
elif power == 1:
func_fit += f'{coeff:.2f}n + '
else:
func_fit += f'{coeff:.2f}n^{power} + '
print(func_fit)
return extrapolate
# Example usage
data = {1: 2, 2: 4, 3: 8, 4: 16, 5: 32}
degree = 3
extrapolate_func = polytrend(data, degree)
print(extrapolate_func(6)) # Extrapolating at n = 6