강의

멘토링

커뮤니티

Inflearn Community Q&A

werooring1287's profile image
werooring1287

asked

[Revised Edition] The Complete Guide to Python Machine Learning

Transform Pandas DataFrame, create/modify column sets

sum질문

Resolved

Written on

·

364

0

안녕하세요. 수업 잘 듣고 있습니다.

series_fair.sum()과 sum(series_fair)의 결과값의 소수점 자리수가 다른 이유는 무엇인가요?

통계python머신러닝 배워볼래요?

Answer 2

0

Baek Kyun Shin님의 프로필 이미지
Baek Kyun Shin
Questioner

감사드립니다!! 좋은 강의와 친절한 답변 감사합니다 :)

0

dooleyz3525님의 프로필 이미지
dooleyz3525
Instructor

안녕하십니까,

저도 모르고 있었는데, 질문 해주셔서 차이를 찾아 봤습니다. Series의 sum()은 numpy의 sum() 메소드를 사용하고 있고,  sum( )은 python의 sum() 함수를 사용하는데, 2개가 수치 해석적으로 서로 다른 summation 기법을 가지고 있어서 정밀도에 차이가 발생한 것 같습니다.  아래 코드를 수행해보시면 차이를 아실 수 있습니다.

import numpy as np
import pandas as pd

np.random.seed(1)

random_array = np.random.rand(100, )
random_series = pd.Series(data=random_array)
print(random_series.sum()==sum(random_series))
print('numpy sum:',random_array.sum(axis=0))
print('python sum:', sum(random_array))
print('Series:', random_series.sum())

numpy의 sum()은 수행 속도와 메모리 절약을 위해서 pairwise summation을 사용하고 있습니다. (https://en.wikipedia.org/wiki/Pairwise_summation)

반면에 python의 sum()함수는 naive summation을 사용하면서 정밀도의 차이가 발생하는 것으로 보입니다.

날카로운 질문 감사드립니다.

werooring1287's profile image
werooring1287

asked

Ask a question