Polynorminal Linear Regression

machine learning
공개

2025년 2월 27일

preprocessing

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = pd.read_csv('_data/04.csv')
x = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values

Linear Regression Model

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()
regressor.fit(x, y)
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Polynorminal Linear Regression

from sklearn.preprocessing import PolynomialFeatures

poly = PolynomialFeatures(degree=4)
x_poly = poly.fit_transform(x)
regressor2 = LinearRegression()
regressor2.fit(x_poly, y)
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Visualize Linear Regression

plt.scatter(x, y, color='red')
plt.plot(x, regressor.predict(x), color='blue')
plt.title('Linear Regression Model')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()

Visualize Poly Linear Regression

plt.scatter(x, y, color='red')
plt.plot(x, regressor2.predict(x_poly), color='blue')
plt.title('Poly Linear Regression Model')
plt.xlabel('Position Level')
plt.ylabel('Salary')
plt.show()

맨 위로