다중 선형 회귀(multiple linear regression)

By | 2020년 4월 4일
Table of Contents

다중 선형 회귀(multiple linear regression)

아파트의 평수를 이용해 아파트의 매매가를 예측해 보았는데요. 아파트는 단순히 평수만으로 가격이 산정되지 않습니다. 지하철과의 거리, 인근 학군 등, 여러 특성(feature) 을 고려해야만 적정한 집값을 산정할 수 있습니다.

여러 개의 특성(feature) 을 입력받아 계산하게 되면 수식은 아래와 같이 여러개의 가중치(weight) 도 있어야 합니다.

H(x_1, x_2, x_3, ..., x_n) = W_1x_1 + W_2x_2 + W_3x_3 + ... + W_nx_n + b

아래에서, 맨하턴의 집정보 데이타를 이용해 임대료를 산정하는 방법을 알아봅니다.

데이타셋 구하기

데이타셋은 여기 에서 구합니다.

git clone https://github.com/Codecademy/datasets.git
head /home/ubuntu/workspace/datasets/streeteasy/manhattan.csv

데이타는 아래와 같이 구성되어 있습니다.

헤더 설명
rental_id rental ID
rent 임대료 ($)
bedrooms 침실수
bathrooms 화장실수
size_sqft 평수(feet)
min_to_subway 지하철과의 거리 (minutes)
floor 층수
building_age_yrs 건물 연령
no_fee 중계수수료 (0 for fee, 1 for no fee)
has_roofdeck 옥상 (o for no, 1 for yes)
has_washer_dryer 세탁기/건조기 (0/1)
has_doorman 도어맨 (0/1)
has_elevator 엘리베이터 (0/1)
has_dishwasher 식기세척기 (0/1)
has_patio 안마당(patio) (0/1)
has_gym 헬스장(gym) (0/1)
neighborhood 이웃 (ex: 한인타운)
submarket submarket (ex: North Brooklyn)
borough borough (ex: Brooklyn)

파이썬으로 구현하기

sklearn 라이브러리를 이용해 구현해 봅니다.

from sklearn.linear_model import LinearRegression
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_csv("/home/ubuntu/workspace/datasets/streeteasy/manhattan.csv")
# df.head()

X = df[['bedrooms',
        'bathrooms',
        'size_sqft',
        'min_to_subway',
        'floor',
        'building_age_yrs',
        'no_fee',
        'has_roofdeck',
        'has_washer_dryer',
        'has_doorman',
        'has_elevator',
        'has_dishwasher',
        'has_patio',
        'has_gym']]
y = df[['rent']]

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, test_size=0.2)

mlr = LinearRegression()
mlr.fit(X_train, y_train)

my_apartment = [[1, 1, 620, 16, 1, 98, 1, 0, 1, 0, 0, 1, 1, 0]]
mlr.predict(my_apartment)
# array([[2400.66691039]])

mlr.score(X_test, y_test)
# 0.7799479629374102

train_test_split 을 이용해 학습셋(80%)과 테스트셋(20%)을 분리합니다.

predict 를 이용해 새로운 데이타를 예측해 볼 수 있습니다.

score 를 이용해 위 모델의 정확도를 측정할 수 있습니다. 위에서는 78%의 정확도를 가집니다.

Category: ML

답글 남기기