Prepare > Python > Numpy > Mean, Var, and Std
2023. 9. 5. 20:14ㆍHackerRank-Python
Mean, Var, and Std | HackerRank
Use the mean, var and std tools in NumPy on the given 2-D array.
www.hackerrank.com
문제
Task
You are given a 2-D array of size X.
Your task is to find:
- The mean along axis 1
- The var along axis 0
- The std along axis None
Input Format
The first line contains the space separated values of N and M.
The next N lines contains M space separated integers.
Output Format
First, print the mean.
Second, print the var.
Third, print the std.
=> N X M의 array를 받아서 (열)평균, (행)분산, 표준편차를 구하라
코드
import numpy as np
N, M = map(int, input().split())
arr = np.array(
[input().split() for _ in range(N)], float
)
print(np.mean(arr, 1))
print(np.var(arr, 0))
print(round(np.std(arr), 11))
노트
np.mean()
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print(numpy.mean(my_array, axis = 0)) #Output : [ 2. 3.]
print(numpy.mean(my_array, axis = 1)) #Output : [ 1.5 3.5]
print(numpy.mean(my_array, axis = None)) #Output : 2.5
print(numpy.mean(my_array)) #Output : 2.5
- axis=0: 행 방향(가로)에서 평균 리턴
- axis=1: 열 방향(세로)에서 평균 리턴
- Defualt는 None으로 array의 모든 요소의 평균 리턴
np.var()
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print(numpy.var(my_array, axis = 0)) #Output : [ 1. 1.]
print(numpy.var(my_array, axis = 1)) #Output : [ 0.25 0.25]
print(numpy.var(my_array, axis = None)) #Output : 1.25
print(numpy.var(my_array)) #Output : 1.25
- axis=0: 행 방향(가로)에서 분산 리턴
- axis=1: 열 방향(세로)에서 분산 리턴
- Defualt는 None으로 array의 모든 요소의 분산 리턴
np.std
import numpy
my_array = numpy.array([ [1, 2], [3, 4] ])
print(numpy.std(my_array, axis = 0)) #Output : [ 1. 1.]
print(numpy.std(my_array, axis = 1)) #Output : [ 0.5 0.5]
print(numpy.std(my_array, axis = None)) #Output : 1.11803398875
print(numpy.std(my_array)) #Output : 1.11803398875
- axis=0: 행 방향(가로)에서 표준편차 리턴
- axis=1: 열 방향(세로)에서 표준편차 리턴
- Defualt는 None으로 array의 모든 요소의 표준편차 리턴
round()
import numpy as np
N, M = map(int, input().split())
arr = np.array(
[input().split() for _ in range(N)], float
)
print(arr_std) # 1.118033988749895
print(round(arr_std), 11) # 1.11803398875
print(round(-0.1)) # 0
print(round(-1.7)) # -2
print(round(-0.12, 1)) # -0.1
print(round(-1.76, 1)) # -1.8
print(round(6.5)) # 6
print(round(5.5)) # 6
print(round(4.5)) # 4
print(round(3.5)) # 4
print(round(2.5)) # 2
print(round(1.5)) # 2
print(round(0.5)) # 0
print(round(-6.5)) # -6
print(round(-5.5)) # -6
print(round(-4.5)) # -4
print(round(-3.5)) # -4
print(round(-2.5)) # -2
print(round(-1.5)) # -2
print(round(-0.5)) # 0
- 첫 번째 파라미터(number= ): 반올림 할 대상 숫자
- 두 번재 파라미터(ndigits= ): 보고싶은 자릿수
- ndigits를 생략하면 number에 가장 가까운 정수를 반환(Defualt는 0인듯..?)
- 가장 가까운 정수가 두 개라면, 짝수를 리턴(음수인 경우에는 절댓값이 짝수인 수)
참조
[python] 파이썬 반올림 round 함수에 대해서
안녕하세요. BlockDMask 입니다. 오늘 가지고 온 파이썬 함수는 반올림을 알 수 있는 round 함수 입니다. 파이썬의 round 함수는 제가 예상했던것과 달리 조금 특이(?)하게 동작을 하는 방식이더군요.
blockdmask.tistory.com
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Numpy > Inner and Outer (0) | 2023.09.07 |
---|---|
Prepare > Python > Numpy > Dot and Cross (0) | 2023.09.06 |
Prepare > Python > Numpy > Min and Max (0) | 2023.09.04 |
Prepare > Python > Numpy > Sum and Prod (0) | 2023.09.03 |
Prepare > Python > Numpy > Floor, Ceil and Rint (0) | 2023.09.02 |