Mini-post: Polynomial Regression Model Using NumPy

This is my first time using the Seaborn module in Python, and I have to admit I’m quite mesmerized by the hue feature when plotting. It just seems so beautiful; far from the regular matplotlib scatterplot I have been using.

So let’s say we’re given the below scatterplot, how do we find out the model (or “best-fit curve” as my highschool teacher put it) of the data?

We don’t need the sklearn module for this–we might need it if we’re dealing with multidimensional data AKA more variables.

So first, use the polyfit function with arguments x, y_noised, and 3. 3 is the highest order/degree from our equation (i.e x to the third power).

f = np.polyfit(x, y_noised, 3)

The polyfit function returns a vector of coefficients p that minimises the squared error in the order deg, deg-1, … 0.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html

I’m not sure what that means, but the point is we need the next function, poly1d, to list our equation.

p = np.poly1d(f)
print(p)

This is the output

1.051*x**3 + 0.596*x**2 + 1.515*x + 422.7

So I just need Seaborn’s plot function to plot the above function(pun?)

sns.lineplot(x, 1.051*x**3 + 0.596*x**2 + 1.515*x + 422.7, color = 'r')

And we will get the following output

Beautiful, huh?

Leave a comment

Website Powered by WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started