David의 개발 이야기!

[ 파트2 섹션7 ] Multiple Linear Regression 다중선형회귀에 대해 알아보자! 본문

Udemy Python Machine Learning A-Z

[ 파트2 섹션7 ] Multiple Linear Regression 다중선형회귀에 대해 알아보자!

david.kim2028 2022. 9. 11. 22:39
반응형

Multiple Linear Regression 

다중선형회귀모형에 대해 알아보고, 스타트업 수익조건 을 만들어보자! 

 

1. Importing the libraries

2. Importing the dataset

3. Encoding the categorical data

4. Splitting thd dataset into the Training set and Test set

5. Training the Multiple Linear Regression model on the Training set

6. Predicting the Test set results

 


1. Importing the libraries

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

 

2. Importing the dataset

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

스타트업 수익조건

 

 

3. Encoding the categorical data

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder 

ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(),[3])], remainder='passthrough')
x = np.array(ct.fit_transform(x))

 

4. Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)

 

5. Training the Multiple Linear Regression model on the Training set

from sklearn.linearr_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)

regressor.fit 으로 훈련데이터로 모델 학습! 

 

6. Predicting the Test set results

y_pred = regressor.predict(x_test)

#소수점 둘째 자리까지 보여주기!
np.set_printoptions(precision=2)

#배열 합치기
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),axis=1))

np.set_printoptions(precision=2) :  소수 둘째 자리까지만 보여줌!

np.concatenate : 배열을 합침!

 

reshap(행, 열) : 행열로 다시 만들어줌 

 

 

 

 

다중선형회귀 모델도, 단순선형회귀 모델과 결국 방법은 똑같다!!

 

* 다중공선성 문제를 고려해서 더미코딩 변수 하나를 임의로 빼주어야하는가? 

-> X ( 자동으로 처리된다 ) 

 

* 모형을 만들때 변수선택( 예시, 후진소거법, 전진선택법 등)을 고려해야하는가?

-> X ( 라이브러리가 자동으로 처리해준다 ) 

 

반응형
Comments