RUBY
최빈값(파이썬) 본문
최빈값
최빈값
데이터에서 빈도수가 가장 많은 데이터를 최빈값이라고 한다.
class MaxAlgorithm:
def __init__(self, ns):
self.nums = ns
self.maxNum = 0
self.maxNumIdx = 0
def setMaxIdxAndNum(self):
self.maxNum = self.nums[0]
self.maxNumIdx = 0
for i, n in enumerate(self.nums):
if self.maxNum < n:
self.maxNum = n
self.maxNumIdx = i
def getMaxNum(self):
return self.maxNum;
def getMaxNumIdx(self):
return self.maxNumIdx;
nums = [1, 3, 7, 6, 7, 7, 7, 12, 12, 17]
maxAlo = MaxAlgorithm(nums)
maxAlo.setMaxIdxAndNum()
maxNum = maxAlo.getMaxNum()
print(f'maxNum: {maxNum}')
indexes = [0 for i in range(maxNum + 1)]
print(f'indexes: {indexes}')
print(f'indexes length: {len(indexes)}')
for n in nums:
indexes[n] = indexes[n] + 1
print(f'indexes: {indexes}')
maxAlo = MaxAlgorithm(indexes)
maxAlo.setMaxIdxAndNum()
maxNum = maxAlo.getMaxNum()
maxNumIdx = maxAlo.getMaxNumIdx()
print(f'maxNum: {maxNum}')
print(f'maxNumIdx: {maxNumIdx}')
print(f'즉, {maxNumIdx}의 빈도수가 {maxNum}로 가장 높다.')
maxNum: 17
indexes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
indexes length: 18
indexes: [0, 1, 0, 1, 0, 0, 1, 4, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1]
maxNum: 4
maxNumIdx: 7
즉, 7의 빈도수가 4로 가장 높다.
최빈값 알고리즘을 이용해서 학생 100명의 점수 분포를 다음과 같이 나타내 보자.
[80, 90, 75, 95, 80, 90, 95, 95, 80, 95, 80, 75, 80, 90, 95, 80, 95, 95, 95, 90, 90, 80, 95, 80, 70, 95, 75, 75, 70, 90, 90, 70, 70, 90, 95, 95, 80, 75, 100, 95, 85, 80, 75, 90, 70, 80, 80, 80, 90, 80, 90, 95, 95, 85, 70, 90, 85, 95, 75, 70, 80, 75, 80, 80, 90, 85, 95, 90, 90, 75, 80, 70, 95, 85, 90, 80, 100, 90, 80, 80, 75, 80, 85, 80, 90, 95, 95, 70, 75, 95, 80, 80, 75, 95, 80, 70, 100, 75, 80, 80]
↓
import random
import maxScore as ms
scores = []
for i in range(100):
rn = random.randint(71, 100)
if rn != 100: rn = rn - (rn % 5)
scores.append(rn)
print(f'scores: {scores}')
# 최댓값 알고리즘
maxAlo = ms.MaxAlgorithm(scores)
maxAlo.setMaxIdxAndNum()
maxNum = maxAlo.getMaxNum()
# 인덱스 리스트 생성
indexes = [0 for i in range(maxNum + 1)]
# 인덱스 리스트에 빈도 저장
for n in scores:
indexes[n] = indexes[n] + 1
n = 1
while True:
maxAlo = ms.MaxAlgorithm(indexes)
maxAlo.setMaxIdxAndNum()
maxNum = maxAlo.getMaxNum()
maxNumIdx = maxAlo.getMaxNumIdx()
if maxNum == 0:
break
print(f'{n}. {maxNumIdx}빈도수: {maxNum}\t', end='')
print('+' * maxNum)
indexes[maxNumIdx] = 0
n += 1
class MaxAlgorithm:
def __init__(self, ns):
self.nums = ns
self.maxNum = 0
self.maxNumIdx = 0
def setMaxIdxAndNum(self):
self.maxNum = self.nums[0]
self.maxNumIdx = 0
for i, n in enumerate(self.nums):
if self.maxNum < n:
self.maxNum = n
self.maxNumIdx = i
def getMaxNum(self):
return self.maxNum;
def getMaxNumIdx(self):
return self.maxNumIdx;
scores: [80, 80, 80, 85, 75, 95, 90, 80, 90, 80, 80, 90, 80, 75, 85, 70, 80, 75, 75, 80, 80, 70, 90, 80, 70, 95, 85, 75, 75, 80, 95, 85, 80, 75, 75, 80, 75, 70, 90, 70, 75, 85, 80, 90, 80, 95, 90, 85, 70, 90, 100, 85, 90, 90, 95, 90, 80, 75, 95, 80, 85, 80, 90, 75, 75, 70, 80, 75, 85, 90, 95, 85, 70, 90, 70, 85, 75, 80, 70, 85, 85, 75, 85, 85, 90, 70, 80, 95, 70, 95, 80, 70, 70, 80, 85, 80, 95, 90, 75, 80]
1. 80빈도수: 26 ++++++++++++++++++++++++++
2. 75빈도수: 17 +++++++++++++++++
3. 85빈도수: 16 ++++++++++++++++
4. 90빈도수: 16 ++++++++++++++++
5. 70빈도수: 14 ++++++++++++++
6. 95빈도수: 10 ++++++++++
7. 100빈도수: 1 +
Comments