-
-
Save rms1000watt/096f2e4f7dad013e331b557c1e8fcdab to your computer and use it in GitHub Desktop.
import numpy as np | |
import matplotlib.pyplot as plt | |
t = np.linspace(0,30,100) | |
y = np.random.random(100) | |
# Note: polynomialOrder too large will yeild warning: RankWarning: Polyfit may be poorly conditioned | |
# Note: depends on the signal you're fitting. A sine wave has no problem with order = 200 | |
# Note: but this random data doesn't fit right with order > 18 (you can see it by eye) | |
polynomialOrder = 10 | |
coefficients = np.polyfit(t,y,polynomialOrder) # find the coefficients for a polynomialOrder order polynomial to make a best fit | |
p = np.poly1d(coefficients) # This makes the polynomial class of a polynomialOrder order polynomial with the best fit coefficients | |
polynomial = p(t) # This calculates the polynomial at every data point. This is the data you plot. | |
fig = plt.figure() | |
ax1 = fig.add_subplot(111) | |
plt.plot(t,y,t,polynomial) | |
plt.show() |
Hello! I have a question. I am working rn with polynomial curve fitting and I want to find the coefficients that minimize the error function i.e. I want to find w* (for a better understanding please read the following slides (pages 6 - 10). However, first I need to find matrix A and vector B due to w* = (inverse of A) x b as seen in page 9. My input will be two arrays: array_x will have all the values of the input variable x, and the array_t will have all the values of the target variable t. Do you have any idea how can I find the matrix A by using lambda function?
Why do you need a lambda function? you can do it with two for loops that go trough the matrix and update its positions [i, j]. Try this code
for i in range(M+1):
for j in range(M+1):
A[i,j] = (x ** (i + j))).sum()
You can do the same thing with the vector b.
for i in range(M+1):
B[i] = (formula)).sum()
Hi. Great work! I am working on a similar problem. However, my input dataset is two-dimensional and I am getting an error message saying "TypeError: expected 1D vector for x". Could you please help me with that? Thanks a lot.
The code:
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
noise_scale = 100
number_of_samples = 50
x = 25*(np.random.rand(number_of_samples, 1) - 0.8)
y = 5 * x + 20 * x2 + 1 * x3 + noise_scale*np.random.randn(number_of_samples, 1)
plt.plot(x,y,'ro')
M=9
z=np.polyfit(x,y,M)
p=np.poly1d(z)
Q=p(x)
plt.plot(x,y,'ro',x,Q,'r-')
Hello! I have a question. I am working rn with polynomial curve fitting and I want to find the coefficients that minimize the error function i.e. I want to find w* (for a better understanding please read the following slides (pages 6 - 10). However, first I need to find matrix A and vector B due to w*= (inverse of A) * (b) as seen in page 9. My input will be two arrays: array_x will have all the values of the input variable x, and the array_t will have all the values of the target variable t. Do you have any idea how can I find the matrix A by using lambda function?