잠토의 잠망경

[python] scipy를 이용한 max, min, median, mean 본문

Book/0005.파이썬으로 배우응 통계학 교과서

[python] scipy를 이용한 max, min, median, mean

잠수함토끼 2020. 5. 3. 07:58

Github

github

준비

    import numpy as np
    import scipy as sp

max, min

다른데서도 다 쓰는거 scipy에도 해당 함수가 있어서 써본다.

    # 최댓 값
    print(sp.amax([2,3,3,4,4,4,4,5,5,6])) # 6

    print('-'*100)

    # 최솟 값
    print(sp.amin([2,3,3,4,4,4,4,5,5,6])) # 2

평균

평균에 이상치 Data가 있는 경우 평균이 무너지게 된다.

여기서 이상치 Data는 100 이다.

    print(sp.mean([1, 2, 3, 4, 5]))                 # 3.0
    print(sp.mean([2, 3, 3, 4, 4, 4, 4, 5, 5, 6]))  # 4.0
    print(sp.mean([2,3,3,4,4,4,4,5,5,100]))         # 13.4

중앙값

Data를 순서대로 늘어놓았을 때 중간에 있는 수치를 의미한다.

평균의 경우 이상치 Data가 있다면 평균이 이상하게 변하지만 중앙 값을 이상치 Data에 강하다.

    # 중앙값 : 숫자를 순서대로 늘어 놓았을때 딱 중간에 있는 수치
    print(sp.median([1, 2, 3, 4, 5]))     # 3.0
    print(sp.median([1, 2, 3, 4, 5, 6]))  # 3.5
    print(sp.median([2,3,3,4,4,4,4,5,5,6]))  # 4.0    
    print(sp.median([2,3,3,4,4,4,4,5,5,100])) # 4.0 이상치 Data가 있는 경우

full code


def sample_other_scipy_function01():
    '''
    최대 값 및 최소 값을 찾는 함수
    :return:
    '''
    import numpy as np
    import scipy as sp


    # 최댓 값
    print(sp.amax([2,3,3,4,4,4,4,5,5,6])) # 6

    print('-'*100)

    # 최솟 값
    print(sp.amin([2,3,3,4,4,4,4,5,5,6])) # 2

    print('-'*100)

    # 중앙값 : 숫자를 순서대로 늘어 놓았을때 딱 중간에 있는 수치
    print(sp.median([1, 2, 3, 4, 5]))     # 3.0
    print(sp.median([1, 2, 3, 4, 5, 6]))  # 3.5
    print(sp.median([2,3,3,4,4,4,4,5,5,6]))  # 4.0
    print(sp.median([2,3,3,4,4,4,4,5,5,100])) # 4.0 이상치 Data가 있는 경우

    print('-'*100)

    # 평균값
    print(sp.mean([1, 2, 3, 4, 5]))                 # 3.0
    print(sp.mean([2, 3, 3, 4, 4, 4, 4, 5, 5, 6]))  # 4.0
    print(sp.mean([2,3,3,4,4,4,4,5,5,100]))         # 13.4

결과

6
----------------------------------------------------------------------------------------------------
2
----------------------------------------------------------------------------------------------------
3.0
3.5
4.0
----------------------------------------------------------------------------------------------------
3.0
4.0
13.4
Comments