Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

캘리포니아 주택 가격

캘리포니아 주택 가격 데이터로 회귀를 연습합니다. 여러 지역 특성(소득·방 수·위치 등)으로 주택 가격의 중앙값을 예측하는 문제입니다. 기본 선형 모델로 시작해, 단위 변환과 특성 공학(다항식·상호작용·구간 분할)으로 성능을 끌어올리는 과정을 살펴보겠습니다.

Scikit-Learn 1.7.2
(20640, 8)
float64
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
       linear   knn
train    0.61  1.00
test     0.59  0.55
[('standardscaler', StandardScaler()), ('linearregression', LinearRegression())]
[('scaler', StandardScaler()), ('linreg', LinearRegression())]
bias 2.069 MedInc 0.830 HouseAge 0.119 AveRooms -0.266 AveBedrms 0.306 Population -0.005 AveOccup -0.039 Latitude -0.900 Longitude -0.871 dtype: float64
       minmax  standard  robust
train    0.61      0.61    0.61
test     0.59      0.59    0.59

관찰: 선형 모델의 과소 적합

가설: 통계적 이상치들이 데이터의 복잡도를 증가시키는 것일 수도?

Loading...
20640 16842
Loading...

1다항식 특성

선형 모델은 직선적인 관계만 표현하므로, 특성과 목표값의 관계가 곡선이면 과소적합하기 쉽습니다. 원래 특성의 거듭제곱(x2,x3,…x^2, x^3, \dots)을 새 특성으로 추가하면, 선형 모델이면서도 곡선 관계를 포착할 수 있습니다. PolynomialFeatures로 다항식 특성을 자동으로 만듭니다.

Loading...

2상호 작용

x=(x1,x2)→x′=(x1,x2,x1x2)x = \left(x_1, x_2\right) \rightarrow x'=\left(x_1, x_2, x_1 x_2\right)

두 특성을 곱한 항(x1x2x_1 x_2)을 추가하면, 한 특성의 효과가 다른 특성의 값에 따라 달라지는 상호작용을 표현할 수 있습니다.

(20640, 36)
8 + 28
['MedInc' 'HouseAge' 'AveRooms' 'AveBedrms' 'Population' 'AveOccup'
 'Latitude' 'Longitude' 'MedInc HouseAge' 'MedInc AveRooms'
 'MedInc AveBedrms' 'MedInc Population' 'MedInc AveOccup'
 'MedInc Latitude' 'MedInc Longitude' 'HouseAge AveRooms'
 'HouseAge AveBedrms' 'HouseAge Population' 'HouseAge AveOccup'
 'HouseAge Latitude' 'HouseAge Longitude' 'AveRooms AveBedrms'
 'AveRooms Population' 'AveRooms AveOccup' 'AveRooms Latitude'
 'AveRooms Longitude' 'AveBedrms Population' 'AveBedrms AveOccup'
 'AveBedrms Latitude' 'AveBedrms Longitude' 'Population AveOccup'
 'Population Latitude' 'Population Longitude' 'AveOccup Latitude'
 'AveOccup Longitude' 'Latitude Longitude']
(20640, 44)
8 + 8 + 28
['MedInc' 'HouseAge' 'AveRooms' 'AveBedrms' 'Population' 'AveOccup'
 'Latitude' 'Longitude' 'MedInc^2' 'MedInc HouseAge' 'MedInc AveRooms'
 'MedInc AveBedrms' 'MedInc Population' 'MedInc AveOccup'
 'MedInc Latitude' 'MedInc Longitude' 'HouseAge^2' 'HouseAge AveRooms'
 'HouseAge AveBedrms' 'HouseAge Population' 'HouseAge AveOccup'
 'HouseAge Latitude' 'HouseAge Longitude' 'AveRooms^2'
 'AveRooms AveBedrms' 'AveRooms Population' 'AveRooms AveOccup'
 'AveRooms Latitude' 'AveRooms Longitude' 'AveBedrms^2'
 'AveBedrms Population' 'AveBedrms AveOccup' 'AveBedrms Latitude'
 'AveBedrms Longitude' 'Population^2' 'Population AveOccup'
 'Population Latitude' 'Population Longitude' 'AveOccup^2'
 'AveOccup Latitude' 'AveOccup Longitude' 'Latitude^2'
 'Latitude Longitude' 'Longitude^2']

3구간 분할 (이산화)

연속적인 특성을 몇 개의 구간(bin) 으로 나누어 범주형으로 바꾸는 것을 이산화라 합니다. 구간마다 다른 효과를 학습할 수 있어, 선형 모델로도 비선형 패턴을 일부 포착할 수 있습니다. KBinsDiscretizer로 구간 분할을 수행합니다.

        위도     경도
0.0  11220   4917
1.0   2076   4161
2.0   6780  11203
3.0    564    359
Loading...
Loading...

4알고리즘 비교

같은 캘리포니아 주택 데이터에 여러 회귀 알고리즘을 적용해 성능을 비교해 보겠습니다. 선형 모델, 거리 기반 kNN, 트리 앙상블을 같은 평가 흐름으로 견줍니다. 단위에 민감한 선형 모델과 kNN은 단위 변환을 함께 묶은 파이프라인으로 평가합니다.

Loading...

표에서 각 알고리즘의 훈련·시험 성능(R2R^2)을 한눈에 비교할 수 있습니다. 선형 모델은 단순하고 해석하기 쉽지만 비선형 관계를 잘 담지 못하고, kNN과 랜덤 포레스트는 비선형 관계를 포착해 시험 성능이 더 높은 편입니다. 데이터에 어떤 알고리즘이 적합한지는 이렇게 여러 모델을 같은 기준으로 비교해 판단합니다.