2022. 2. 23. 16:50ㆍAI/Codestates
코딩부트캠프 | 코드스테이츠 - 비전공생도 개발자가 될 수 있습니다
코딩부트캠프를 찾는다면? 개발자로 커리어 전환을 위한 책임있는 코딩 교육 기관! 서비스 기획자, 그로스 마케터, 데이터 사이언티스트 등 다양한 전문 커리어에 도전하세요. 취업 성공의 후기
www.codestates.com
Warm-up
Note
- 다중선형회귀 ( Multiple Linear Regression )
- 회귀모델을 평가하는 평가지표들 ( Evaluation Metrics )
- MSE ( Mean Squared Error ) : 전차 제곱의 합의 평균
- MAE ( Mean Absolute Error ) : 전차 절대값의 합
- RMSE ( Root Mean Squared Erroe ) : MSE의 루트를 씌운 값
- R - Squared ( Coefficient of Determination )
- 참고
- SSE ( Sum of Squared Error, 관측치와 예측치 차이 )
- SSR ( Sum of Squared due to Regression, 예측치와 평균 차이 )
- SST ( SUm of Squared Total, 관측치와 평균 차이 )
- 과적합 ( Overfitting ) : 과하게 학습해 일반화를 못해 결국 테스트 데이터에서 오차가 커지는 현상
ex) 분산이 높은 경우 - 과소적합 ( Underfitting ) : 과적합도 못하고 일반화 성질도 학습하지 못해 훈련 / 테스트 데이터 모두 오차가 크게나오는 경우
ex) 편향이 높은 경우 - 이상치 대체 및 제거 함수
def kill_outlayers(dataset,feature,processing_system):
Q1 = dataset[feature].quantile(0.25)
Q3 = dataset[feature].quantile(0.75)
IQR = Q3 - Q1
lower_limit = Q1 - 1.5*IQR
upper_limit = Q3 + 1.5*IQR
if processing_system == "mean" :
processing_value = dataset[feature].mean
for i in range(0,dataset.shape[0]) :
if dataset.loc[i,f"{feature}"] > upper_limit :
dataset.loc[i,f"{feature}"] =processing_value
elif dataset.loc[i,f"{feature}"] < lower_limit :
dataset.loc[i,f"{feature}"] =processing_value
else :
continue
elif processing_system == "median" :
processing_value = dataset[feature].median
for i in range(0,dataset.shape[0]) :
if dataset.loc[i,f"{feature}"] > upper_limit :
dataset.loc[i,f"{feature}"] =processing_value
elif dataset.loc[i,f"{feature}"] < lower_limit :
dataset.loc[i,f"{feature}"] =processing_value
else :
continue
elif processing_system == "drop" :
indexs =[]
for i in range(0,dataset.shape[0]) :
if dataset.loc[i,f"{feature}"] > upper_limit :
indexs.append(i)
elif dataset.loc[i,f"{feature}"] < lower_limit :
indexs.append(i)
else :
continue
dataset.drop(dataset.index[indexs],inplace =True)
dataset.reset_index(drop=True,inplace =True)
return dataset
- 훈련 / 테스트 데이터셋 나누기
from sklearn.model_selection import train_test_split
x = df[['bathrooms', 'grade', 'sqft_living', 'sqft_living15', 'sqft_sum']]
y = df['price']
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, test_size=0.2)
- 평가지표 함수
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
def train_evaluation_metrics(y, y_pred):
# 평가지표
mse = mean_squared_error(y, y_pred)
mae = mean_absolute_error(y, y_pred)
rmse = mse ** 0.5
r2 = r2_score(y, y_pred)
print(f'훈련 MSE: {mse:.2f}')
print(f'훈련 MAE: {mae:.2f}')
print(f'훈련 RMSE: {rmse:.2f}')
print(f'훈련 R2: {r2:.2f}')
def test_evaluation_metrics(y, y_pred):
# 평가지표
mse = mean_squared_error(y, y_pred)
mae = mean_absolute_error(y, y_pred)
rmse = mse ** 0.5
r2 = r2_score(y, y_pred)
print(f'테스트 MSE: {mse:.2f}')
print(f'테스트 MAE: {mae:.2f}')
print(f'테스트 RMSE: {rmse:.2f}')
print(f'테스트 R2: {r2:.2f}')
train_evaluation_metrics(y_train, y_pred_1)
test_evaluation_metrics(y_test, y_pred_2)
- 다중선형회귀
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train, y_train)
y_pred_1 = model.predict(x_train)
y_pred_2 = model.predict(x_test)
Review
확실히 하면 할 수록 재밌다. 오늘 도전과제는 조금 어려웠던거 같다.
참고
https://www.kaggle.com/zeadomar/predict-house-price-using-regression
Predict house price using regression
Explore and run machine learning code with Kaggle Notebooks | Using data from House Sales in King County, USA
www.kaggle.com
What are the best metrics to evaluate your regression model?
R Square, MSE, RMSE, MAE
towardsdatascience.com
https://m.blog.naver.com/istech7/50153288534
상관 계수(correlation coefficient) 와 결정 계수(coefficient of determination)
연속형 또는 순위 자료로 이루어진 두 변수간 상호 관계만을 알아보고자 할 때는 상관 관계 분석(correlati...
blog.naver.com
'AI > Codestates' 카테고리의 다른 글
[Day 26] Logistic Regression (0) | 2022.02.28 |
---|---|
[Day 25] Ridge Regression (0) | 2022.02.24 |
[Day 23] Simple Regression (0) | 2022.02.22 |
[Day 22] Section 1 Review (0) | 2022.02.22 |
[Day 16 ~ Day 21] Section 1 Project (0) | 2022.02.22 |