티스토리 뷰

INTRO

데이터 분석 과정에서 시각화는 중요한 부분이다.

변수 값의 분포나 변수 사이의 상관관계를 분석하고 대상 모델링을 위한 가설 도출에 효율 적이다.

여기서는 공공데이터 포탈에서 학생 건강검사 macro data에서 키, 몸무게를 indexing하여 추출하고 간단한 Histogram과 Scatter plot을 그리는 방법을 소개한다.

python 활용 편에서 3rd party module을 응용하여 통계분석 까지 확장하여 다루도록 하겠다.

 

 

 

python code : pyTest/test15.py

# -*- coding: utf-8 -*-
import os
from urllib.request import urlretrieve
import csv
import matplotlib.pyplot as plt


#### path & sample download
base_path, sub_dirs, fnames = next(os.walk(os.getcwd()))
base_dpath = './data'
down_sname = 'file4.csv'
url = 'https://www.data.go.kr/cmm/cmm/fileDownload.do?atchFileId=FILE_000000002287094&fileDetailSn=1&insertDataPrcus=N'
if not os.path.isfile(os.path.join(base_dpath,down_sname)):
	urlretrieve(url, os.path.join(base_dpath,down_sname))


#### data_sort from .csv file
with open(os.path.join(base_dpath,down_sname),'r',encoding='euc-kr') as f0:
	lines = csv.reader(f0)
	headers = next(lines)
	header = headers[9],headers[13],headers[15:17]
	records = []
	for line in lines:
		record = line[9],line[13],line[15:17]
		records.append(record)

#### data_sort index & records view
print("header structure \n{}\n".format(header))
print("record structure \n{}\n".format(records[0]))


#### data separate for plot
x1 = []
x2 = []
for record in records:
	x1.append(record[2][0])
	x2.append(record[2][1])

#### subplot : histogram
fig = plt.figure(figsize=(10,8))
ax1 = fig.add_subplot(1, 3, 1)
plt.hist(x1, bins=100, width=0.9, color="blue")
plt.title('Histogram_Tall')
plt.xlabel('cm')
plt.ylabel('person')
ax2 = fig.add_subplot(1, 3, 2)
plt.hist(x2, bins=100, width=0.9, color="red")
plt.title('Histogram_Weight')
plt.xlabel('kg')
plt.ylabel('person')

#### subplot : scatter
ax3 = fig.add_subplot(1, 3, 3)
plt.scatter(x2,x1, marker='o', color="green")
plt.title('Scatter Plot for weight vs tall')
plt.xlabel('weight(kg)')
plt.ylabel('tall(cm)')
plt.show()

---- SublimeText python Run test15.py ----

header structure 
('학교명', '성별', ['키', '몸무게'])

record structure 
('서울대도초등학교', '남', ['125.8', '27.3'])


***Repl Closed***

---- Figure1 ----

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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
글 보관함