David의 개발 이야기!

Polynomial Regression 다항회귀에 대해 알아보자! 본문

Udemy Python Machine Learning A-Z

Polynomial Regression 다항회귀에 대해 알아보자!

david.kim2028 2022. 9. 12. 16:21
반응형

연차에 따라 올라가는 임금을 알아보는 모델을 만들어보자! 

 

1. Importing the libraries

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

 

2. Importing the dataset

dataset = pd.read_csv("Position_Salaries.csv")
x = dataset.iloc[:,1:-1].values
y = dataset.iloc[:,-1].values

 

3. Training the Polynomial Regression model on the whole dataset

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=2)
x_poly = poly_reg.fit_transform(x)

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_poly, y)

* PolynomialFeatures

-> Generate polynomial and interaction features.

Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].

 

-> parameters : degree

int or tuple (min_degree, max_degree), default=2

If a single int is given, it specifies the maximal degree of the polynomial features. If a tuple (min_degree, max_degree) is passed, then min_degree is the minimum and max_degree is the maximum polynomial degree of the generated features. Note that min_degree=0 and min_degree=1 are equivalent as outputting the degree zero term is determined by include_bias.

 

4. Visualizing the Polynomial Regression Result

plt.scatter(x, y, color='red')
plt.plot(x, regressor.predict(x_poly), color='blue')

 

5. Predicting new result with Polynomial Regression

regressor.predict(poly_reg.fit_transform([[6.5]]))

predict 안에 6.5 가 바로 들어가는게 아님을 주의하자! 

반응형
Comments