파이썬 Python/파이썬
[9일차/파이썬] Numpy
하나비 HANABI
2023. 1. 5. 15:39
import numpy as np
data = np.array([1, 2, 3, 4, 5])
print(type(data)) #<class 'numpy.ndarray'>
print(data.dtype) #int32
print(data) #[1 2 3 4 5]
import numpy as np
data = np.random.randn(2,3)
print(data)
print(type(data))
print(data.shape)
print(data.dtype)
'''
[[ 2.08874584 -0.32466693 0.0936289 ]
[-0.30339426 -1.31066591 -0.27167037]]
<class 'numpy.ndarray'>
(2, 3)
float64
'''
insert
import numpy as np
x = np.array([[1,1,1],[2,2,2],[3,3,3]])
print(x, '\n')
y = np.insert(x, 1, 10, axis=0) # 열 인덱스 1에 집어넣음
print(y, '\n')
y = np.insert(x, 1, 10, axis=1) # 행 인덱스 1에 집어넣음
print(y, '\n')
'''
[[1 1 1]
[2 2 2]
[3 3 3]]
[[ 1 1 1]
[10 10 10]
[ 2 2 2]
[ 3 3 3]]
[[ 1 10 1 1]
[ 2 10 2 2]
[ 3 10 3 3]]
'''
doctor2.csv 의 데이터를 (7x4) ndarray에 넣어 p.409와 같은 그래프 출력
# doctor2.csv 의 의사수를 (7x4) ndarray에 넣어 그래프 출력
# 파일 -> 2차원 리스트 -> ndarray -> 그래프
from matplotlib import pyplot as plt
import numpy as np
file = open('./data/12/doctor2.csv','r',encoding='UTF-8')
lines = file.readlines()
file.close()
list_data=[]
for line in lines[1:]:
temp = line[:-1].split(',')
for idx in range(1, len(temp)):
temp[idx] = int(temp[idx])
list_data.append(temp)
doctors = np.zeros((7,4), dtype='int32')
for i in range(0,len(list_data)):
for j in range(0,4):
doctors[i][j] = list_data[i][j+1]
print(doctors)
x=['서울','부산','대구','인천','대전','광주','울산']
y0 =doctors[:,0]
y1 =doctors[:,1]
y2 =doctors[:,2]
y3 =doctors[:,3]
plt.plot(x, y0)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.show()