티스토리 뷰
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]]
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 개발자포트폴리오
- github
- googlecolab
- 코드스테이츠
- 빅데이터
- 데이터준전문가
- Notion2Tistory
- 깃허브
- 이력서
- 자버
- 데이터준전문가합격후기
- data annotation
- ADsP
- ai부트캠프
- 빅데이터분석기사
- Kaggle
- todayIlearned
- ADsP합격후기
- 데이터과학
- 취업준비
- 이자포스터디
- 자격증
- 빅분기
- 빅분기후기
- 코테공부
- 데이터분석
- 빅분기합격
- 커리어코칭
- 인공지능
- Til
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함