다항식 회귀는 독립 변수 x와 종속 변수 y 간의 관계가 n차 다항식으로 모델링되는 선형 회귀의 한 형태입니다. 다항식 회귀는 x 값과 y의 해당 조건부 평균 사이의 비선형 관계에 적합하며 E(y |x)
로 표시됩니다.예
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset datas = pd.read_csv('data.csv') datas # divide the dataset into two components X = datas.iloc[:, 1:2].values y = datas.iloc[:, 2].values # Fitting Linear Regression to the dataset from sklearn.linear_model import LinearRegression lin = LinearRegression() lin.fit(X, y) # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree = 4) X_poly = poly.fit_transform(X) poly.fit(X_poly, y) lin2 = LinearRegression() lin2.fit(X_poly, y) # Visualising the Linear Regression results plt.scatter(X, y, color = 'blue') plt.plot(X, lin.predict(X), color = 'red') plt.title('Linear Regression') plt.xlabel('Temperature') plt.ylabel('Pressure') plt.show() # Visualising the Polynomial Regression results plt.scatter(X, y, color = 'blue') plt.plot(X, lin2.predict(poly.fit_transform(X)), color = 'red') plt.title('Polynomial Regression') plt.xlabel('Temperature') plt.ylabel('Pressure') plt.show() # Predicting a new result with Linear Regression lin.predict(110.0) # Predicting a new result with Polynomial Regression lin2.predict(poly.fit_transform(110.0))