How to Draw Line of Polynomial Regression in Python


In this example, we will learn how to draw the line of Polynomial Regression in Python.

Source Code

import numpy as np
import matplotlib.pyplot as plt

x = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
y = [645, 552, 450, 408, 411, 430, 418, 440, 445, 430, 560, 630]

p = np.poly1d(np.polyfit(x, y, 3))

line_x = np.linspace(1, 23, num=1000)

plt.scatter(x, y, label='original data')
plt.plot(line_x, p(line_x), 'r', label='the line of polynomial regression')
plt.legend()
plt.show()

Output:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement