Repository navigation
Expand file tree
/
Copy pathstatEx.py
More file actions
52 lines (42 loc) · 2.22 KB
/
Copy pathstatEx.py
File metadata and controls
52 lines (42 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 통계 함수와 축(axis) — 넘파이에서 가장 헷갈리는 개념
import numpy as np
scores = np.array([85, 73, 68, 90, 88, 95, 55, 62])
print('========== 1차원 통계 ==========')
print('합계 :', scores.sum())
print('평균 :', round(scores.mean(), 2))
print('중앙값 :', np.median(scores))
print('표준편차:', round(scores.std(), 2))
print('최대/최소 :', scores.max(), scores.min())
print('최대 위치 :', scores.argmax()) # 값이 아니라 '몇 번째' 인지
print('정렬 :', np.sort(scores))
print('정렬 순서:', np.argsort(scores)) # 정렬했을 때의 원래 위치
print('\n========== 2차원과 axis ==========')
# 행 = 학생, 열 = 과목
table = np.array([[85, 73, 68], # 홍길동
[90, 88, 95], # 김길동
[55, 62, 48]]) # 최길동
names = ['홍길동', '김길동', '최길동']
subjects = ['국어', '영어', '수학']
print(table)
print('전체 평균 :', round(table.mean(), 2))
print('\naxis 를 그림으로 기억하세요.')
print(' axis=0 은 "행을 따라 아래로" 접습니다 → 열별 결과')
print(' axis=1 은 "열을 따라 옆으로" 접습니다 → 행별 결과')
print('\naxis=0 (과목별) :', table.mean(axis=0).round(2))
for i, s in enumerate(subjects):
print(f' {s} 평균 {table[:, i].mean():.2f}')
print('\naxis=1 (학생별) :', table.mean(axis=1).round(2))
for i, n in enumerate(names):
print(f' {n} 총점 {table[i].sum()} 평균 {table[i].mean():.2f}')
print('\n========== 결과의 shape 로 확인하기 ==========')
print('원본 :', table.shape) # (3, 3)
print('axis=0 결과 :', table.sum(axis=0).shape) # (3,) — 행이 사라졌다
print('axis=1 결과 :', table.sum(axis=1).shape) # (3,) — 열이 사라졌다
# "지정한 축이 사라진다" 고 외우면 편합니다.
print('\n========== 조건 통계 ==========')
print('60점 이상 개수 :', (table >= 60).sum())
print('학생별 60점 이상:', (table >= 60).sum(axis=1))
print('한 과목이라도 60 미만인 학생 :',
[names[i] for i in range(len(names)) if (table[i] < 60).any()])
print('모든 과목 60 이상인 학생 :',
[names[i] for i in range(len(names)) if (table[i] >= 60).all()])