카테고리 없음
20201118 DAY14 OECD 데이터 예측하기
eℓlie
2020. 11. 18. 09:31
LinearRegression() 회귀 모형 함수이다.
sklearn.linear_model.Lasso with l1 regularization
sklearn.linear_model.Ridge with l2 regularization
sklearn.linear_model.ElasticNet with both l1 and l2
fit(X, y, sample_weight=None) Traing data의 X, Target values의 y로 모델을 훈련한다.
출처: 《핸즈온 머신러닝》 2판(자료는 멘토님이 제공해주심)
어제 한 코드(applecider1002.tistory.com/29)에서 이어진다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.linear_model
oecd_bli = pd.read_csv("oecd_bli_2015.csv", thousands=',')
gdp_per_capita = pd.read_csv("gdp_per_capita.csv", thousands=',', delimiter='\t', encoding='latin1', na_values="n/a")
def prepare_country_stats(oecd_bli, gdp_per_capita):
oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
gdp_per_capita.set_index("Country", inplace=True)
full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita, left_index=True, right_index=True)
full_country_stats.sort_values(by="GDP per capita", inplace=True)
remove_indices = [0, 1, 6, 8, 33, 34, 35]
keep_indices = list(set(range(36)) - set(remove_indices))
return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]
country_stats = prepare_country_stats(oecd_bli, gdp_per_capita)
X = np.c_[country_stats["GDP per capita"]]
y = np.c_[country_stats["Life satisfaction"]]
country_stats.plot(kind='scatter', x="GDP per capita", y='Life satisfaction')
plt.show
model = sklearn.linear_model.LinearRegression()
model.fit(X, y)
X_new = [[22587]]
print(model.predict(X_new))
|
cs |
27행에서 어제 입력한 데이터로 선형회귀 모델을 만들고,
29행에서 변수를 부여한 뒤
31행에서 X_new = [[22587]] 하여 키프로스의 GDP 데이터를 주었다.
결과는 [[5.96242338]]