2019. 9. 29. 20:48ㆍ파이썬
from numpy import * #API를 import
파이썬에서 list로는 행렬 계산 불가, numpy로 가능
A = np.array([1,2,3])
B = np.array([4,5,6])
A.shape #형상
A.ndim #차원
-------------------------------------------------
import numpy as np
A = np.array([[10, 20, 30, 40], [50, 60, 70, 80]])
print(A, "\n")
print("A.shape == ", A.shape, "\n")
it = np.nditer(A, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
idx = it.multi_index
print("current value => ", A[idx])
it.iternext()
-------------------------------------------------
#numpy concatenate : 행렬에 행이나 열을 추가
#[[10,20,30]
# [40,50,60]]
#[[70,80,90]]
#[[10,20,30] [[1000],
# [40,50,60]] [2000]]
import numpy as np
A = np.array([[10,20,30],[40,50,60]])
print(A.shape)
row_add = np.array([70,80,90]).reshape(1,3)
column_add = np.array([1000,2000]).reshape(2,1)
print(column_add.shape)
B = np.concatenate((A, row_add), axis=0)
print(B)
C = np.concatenate((A, column_add), axis=1)
print(C)
---------------------------------------------------
#loadtxt : seperator로 구분된 파일에서 데이터를 읽기 위한 함수
loaded_data = np.loadtxt('./data-01.csv', delimiter=',', dtype=np.float32)
x_data = loaded_data[:,0,-1]
t_data = loaded_data[:,[-1]]
print("x_data.ndim = ", x_data.ndim, ", x_data.shape = ", x_data.shape)
print("t_data.ndim = ", t_data.ndim, ", t_data.shape = ", t_data.shape)
---------------------------------------------------
#radom 함수
radnom_number1 = np.random.rand(3)
radnom_number2 = np.random.rand(1,3)
radnom_number3 = np.random.rand(3,1)
A = np.ones([3,3])
B = np.zeros([3,2])
------------------------------------------------------
'파이썬' 카테고리의 다른 글
수치미분 (0) | 2019.10.05 |
---|---|
Matplotlib scatter/line graph (0) | 2019.09.29 |
numpy iterator (0) | 2019.09.29 |
클래스, Exception, with 구문 (0) | 2019.09.28 |
파이썬의 람다(Lambda) 함수 (0) | 2019.09.28 |