본문 바로가기
2/[ Python ]

Handling "inf", "-inf" in Python

by Kieran_Han 2022. 7. 12.

Supervised Learning Accuracy를 계산하다보면, 종종 아래와 같이 inf 혹은 -inf 값을 볼 수 있다.

print("Accuracy:", 100 - (100 * (abs(test_labels - test_predictions) / test_labels)).mean())

infinity의 줄임말로 값이 무한히 크다는 뜻이다.

이를 nan값으로 처리한 다음, dropna로 없애고, 구하고자하는 Accuracy를 아래와 같이 계산할 수 있다.

(100 - (100 * (abs(test_labels - test_predictions) / test_labels))).replace([np.inf, -np.inf], np.nan).dropna().describe()