-
통계-2 이산형 데이터통계 2023. 5. 29. 16:48
1. 이산형 데이터
이산형 데이터는 불연속적이며 한정된 값으로 이루어진 데이터이다. 동전 던지기, 세대수 등이 이산형 데이터에 속한다.
data = pd.read_csv('./data/college_data.csv') print(data['top10perc'].unique()) [23 16 22 60 38 17 37 30 21 44 9 83 19 14 24 25 20 46 12 36 42 15 50 53 18 34 39 28 26 11 67 45 76 5 48 10 87 71 49 32 40 8 47 29 75 27 13 35 1 31 6 55 33 3 58 70 68 56 78 77 41 4 90 43 51 89 7 57 95 52 96 2 65 85 86 62 54 66 79 74 80 81]
대학생 중 상위 10퍼센트 학생의 비율을 나타내는 top10perc 컬럼을 고유한 값만 추출하는 unique 함수를 통해 추출한다.
plt.figure(figsize=(10,6)) plt.bar(data['private'], data['top10perc']) plt.title('Bar Chart of Top 10% Scores by Private/Public') plt.xlabel('Private/Public') plt.ylabel('Top 10% Scores') plt.show()
private 대학교와 public 대학교의 top10perc 지표를 비교하였으며 private 대학교의 점수가 더 높은 것을 확인할 수 있다.
plt.figure(figsize=(10,6)) data.groupby('private')['top10perc'].sum().plot(kind='pie', autopct='%1.1f%%') plt.title('Pie Chart of Top 10% Scores by Private/Public') plt.ylabel('') plt.show()
private 대학교가 top10perc 지표의 77프로를 차지하고 있는 것을 원그래프로 확인할 수 있다.
autopct는 그래프내의 숫자 양식을 지정하며 kind를 pie로 설정하여 원그래프를 그린다.
plt.figure(figsize=(10,6)) plt.hist(data['top10perc'], bins=20) plt.title('Histogram of Top 10% Scores') plt.xlabel('Top 10% Scores') plt.ylabel('Frequency') plt.show()
top10perc 지표의 점수를 히스토그램으로 그릴 수 있으며 대부분의 학교는 10-30점에 위치하는 것을 볼 수 있다.
2. 빈도수
빈도수는 이산형 데이터를 분석할 때 중요한 개념이다. 이산형데이터는 값이 한정되어 있기 때문에 빈도수를 통해 전체 데이터의 분포를 파악할 수 있다.
data = np.random.randint(1, 7, size=100) unique, counts = np.unique(data, return_counts=True) print(unique) print(counts) [1 2 3 4 5 6] [ 8 19 17 18 21 17]
random.randint 함수를 통해 1~6까지 랜덤한 값 100개를 생성한다. unique 함수로 고유한 데이터 값을 뽑아내고 return_counts=True 함수는 각각의 개수를 알려준다.
fig, ax = plt.subplots() ax.bar(unique, counts) ax.set_xlabel('Value') ax.set_ylabel('Frequency') ax.set_title('Frequency Distribution') plt.show()
주사위 번호 당 빈도수를 그래프로 표현한다.
3. 상대빈도수
상대빈도수는 해당 데이터 값이 전체 데이터에서 차지하는 비율이다.
data=np.array([1,2,3,4,4,4,5,5,5,5]) value_counts = np.bincount(data)
bincount 함수로 숫자의 빈도 수를 확인한다.
relative_frequencies = value_counts/len(data)
빈도 수는빈도수는 [0 1 1 2 3 4]이며 상대 빈도수는 [0, 0.09, 0.09, 0.18, 0.27, 0.36]으로 나온다. 즉 상대 빈도수는 빈도수 나누기 전체 데이터 개수이며 숫자 5의 경우 4번 등장했으며 상대 빈도수 0.36으로 전체 데이터 중 36퍼센트를 차지한다.
plt.bar(np.arange(len(value_counts)), relative_frequencies) plt.xticks(np.arange(len(value_counts)), np.arange(len(value_counts))+1) plt.xlabel('Value') plt.ylabel('Relative Frequency') plt.title('Relative Frequency Bar Chart') plt.show()
숫자 당 상대 빈도 수를 그래프로 표현한다.
4. 백분율
백분율은 상대 빈도수를 100 곱한 값이다.
labels = ['a','b','c','d','e'] percentages = [30,20,15,10,25] fig, ax = plt.subplots() ax.pie(percentages, labels=labels, autopct='%1.1f%%') ax.axis('equal') ax.set_title('Percentages of Categories') plt.show()
fig, ax = plt.subplots() ax.bar(labels, percentages) ax.set_xlabel('Categories') ax.set_ylabel('Percentages') ax.set_title('Percentages of Categories') plt.show()
백분율을 원 그래프와 막대 그래프로 표현할 수 있다.
'통계' 카테고리의 다른 글
통계-5 데이터 분석 방법 (0) 2023.05.29 통계-4 순서형 데이터, 이진 데이터, 시계열 데이터, 공간 데이터 (0) 2023.05.29 통계-3 범주형 데이터 (0) 2023.05.29 통계-1 연속형 데이터 (0) 2023.05.29