728x90
세이지메이커 호스팅 서비스에 배포된 모델을 평가한다
import numpy as np
def predict(data, rows=1000):
split_array = np.array_split(data, int(data.shape[0] / float(rows) + 1))
predictions = ''
for array in split_array:
predictions = ','.join([predictions, xgb_predictor.predict(array).decode('utf-8')])
return np.fromstring(predictions[1:], sep=',')
rows 인수는 한 번에 예측할 줄 수를 지정하는 것
테스트 세트를 예측하고 히스토그램을 그린다. 실제 값에 대한 0번째 열을 제외하고 테스트세트의 특성 열만 취하면 된다
import matplotlib.pyplot as plt
predictions=predict(test.to_numpy()[:,1:])
plt.hist(predictions)
plt.show()
예측 값은 부동 소수점이라 True/False 기반으로 컷오프 값을 설정해야한다
import sklearn
cutoff=0.5
print(sklearn.metrics.confusion_matrix(test.iloc[:, 0], np.where(predictions > cutoff, 1, 0)))
print(sklearn.metrics.classification_report(test.iloc[:, 0], np.where(predictions > cutoff, 1, 0)))
테스트 세트로 최상의 컷오프를 찾으로면 로지스틱 회귀의 로그손실함수를 계산하면 된다. 로그 손실 함수는 정답 레이블에 대한 예측 확률을 반환하는 로지스틱 모델의 음의 로그 가능성으로 정의된다
import matplotlib.pyplot as plt
cutoffs = np.arange(0.01, 1, 0.01)
log_loss = []
for c in cutoffs:
log_loss.append(
sklearn.metrics.log_loss(test.iloc[:, 0], np.where(predictions > c, 1, 0))
)
plt.figure(figsize=(15,10))
plt.plot(cutoffs, log_loss)
plt.xlabel("Cutoff")
plt.ylabel("Log loss")
plt.show()
Numpy의 argmin과 min 함수를 사용하여 오류 곡선의 최소점을 찾는다
print(
'Log loss is minimized at a cutoff of ', cutoffs[np.argmin(log_loss)],
', and the log loss value at the minimum is ', np.min(log_loss)
)
728x90
'AWS' 카테고리의 다른 글
클라우드 컴퓨팅 개념 | 장점 (1) | 2022.07.08 |
---|---|
[SageMaker] 삭제 (0) | 2022.06.28 |
[SageMaker] 모델 배포 (1) | 2022.06.28 |
[SageMaker] 모델 훈련 (1) | 2022.06.28 |
[SageMaker] 데이터 세트 다운로드하기 (0) | 2022.06.28 |