python
numpy 정리(1)
cj92
2020. 2. 18. 23:29
numpy 연습¶
1. dimension, type¶
- 부호가 있는 정수(i) : int(8, 16, 32, 64)
- 부호가 없는 정수(u) : unit(8, 16, 32, 64)
- 실수(f) : float(16, 32, 64, 128)
- 복소수(c) : complex(64, 128, 256)
- 불리언(b) : bool
- 문자열(S) : string
- 오브젝트(O) : object
- 유니코드(U) : unicode ### 2. sahpe, dtype, astype
- sahpe : 차원 확인
- dtype : 자료 형태 확인
- astype : 자료 형태 변환 ### 3. isinf, isnan
4. np.sign, np.ceil, np.floor¶
- sign : 양수는 1 음수는 -1 0은 0을 반환
- ceil : 올림
- floor : 버림 ### 5. np.multply(arr1,arr2), np.maximum(arr1,arr2)
- multply : 원소곱
- maximum : 원소 최대값 ### 6. min, mean, sum, std, argmin, argamx, cumsum, cumprod
- axis 옵션을 통해 행, 열, 전체에 적용 가능 ### 7. np.hstack(arr1(n,m1),arr2(n,m2)), np.vstack(arr1(n1,m),arr2(n2,m)) ### np.dstack(arr1(n,m),arr2(n,m)), np.stack(arr1(n,m),arr2(n,m))
- hstack : 행 병합 arr(n,m1+m2)
- vstack : 열 병합 arr(n1+n2,m)
- dstack : 원소 병합 arr(n,m,2)
- stack : axis=0(arr(2,n,m)), axis=1(arr(n,2,m)), axis=2(arr(n,m,2))
In [46]:
import numpy as np
In [47]:
data1 = [1,2,3,4,5]
data2 = [1,2,3,3.5,4]
In [48]:
arr1 = np.array(data1)
arr2 = np.array([1,2,3,4,5])
arr3 = np.array(data2)
arr4 = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
shape를 통해 차원을 확인 가능¶
In [49]:
arr1.shape
Out[49]:
dtype을 통해 자료 형태 확인 가능
In [50]:
arr2.dtype
Out[50]:
In [51]:
arr2 = np.array([1,2,3,4,5],dtype='b')
arr2
Out[51]:
astype을 통해 자료 형태 변환 가능¶
In [52]:
arr2.astype('f')
Out[52]:
inf과 nan 이 존재¶
- np.isinf(), np.isnan()으로 불리언화 할 수 있음
In [60]:
np.array([1])/np.array([0])
Out[60]:
In [61]:
np.log(0)
Out[61]:
In [62]:
np.array([0])/np.array([0])
Out[62]:
- np.sign : 양수는 1 음수는 -1 0은 0 을 반환
- np.ceil : 올림
- np.floor : 버림
In [69]:
np.sign(arr1)
Out[69]:
두 개의 array에 대해 동일한 위치의 성분끼리 연산 값을 계산하기(add, subtract, multiply, divide)¶
In [72]:
arr1
Out[72]:
In [82]:
np.random.seed(1)
arr1=np.ceil(np.random.randn(5,3)*10)
print(arr1)
np.random.seed(2)
arr2=np.ceil(np.random.randn(5,3)*10)
print(arr2)
In [90]:
np.abs(arr1)
np.sqrt(arr1)
np.square(arr1)
np.exp(arr1)
np.log(arr1)
Out[90]:
In [93]:
print(arr1)
print(arr2)
In [91]:
np.multiply(arr1,arr2)
Out[91]:
In [94]:
np.maximum(arr1,arr2)
Out[94]:
min, mean, sum, std, argmin, argamx, cumsum,cumprod
In [106]:
arr1
Out[106]:
In [107]:
np.min(arr1,axis=0)
Out[107]:
In [108]:
np.mean(arr1, axis=0)
Out[108]:
In [109]:
np.mean(arr1)
Out[109]:
In [110]:
np.argmin(arr1,axis=0)
Out[110]:
In [111]:
np.argmin(arr1,axis=1)
Out[111]:
In [116]:
np.cumprod(arr1,axis=0)
Out[116]:
In [118]:
arr1
Out[118]:
In [117]:
np.sort(arr1,axis=0)
Out[117]:
In [119]:
np.sort(arr1,axis=0)[::-1]
Out[119]:
In [120]:
a = np.arange(12)
In [121]:
a
Out[121]:
In [123]:
b = a.reshape(3, 4)
b
Out[123]:
In [184]:
a.reshape(6, -1)
Out[184]:
In [133]:
a.reshape(2, -1, 3)
Out[133]:
In [136]:
a.reshape(2,-1,3).flatten()
Out[136]:
In [141]:
a.reshape(12,1)
Out[141]:
In [144]:
a[np.newaxis]
Out[144]:
In [147]:
a[:,np.newaxis]
Out[147]:
In [152]:
a.reshape(3,-1)
Out[152]:
In [159]:
np.array(range(9)).reshape(3,-1)
Out[159]:
In [163]:
b=np.hstack((a.reshape(3,-1),np.array(range(9)).reshape(3,-1)))
b
Out[163]:
In [164]:
b.shape
Out[164]:
In [167]:
a.reshape(4,-1)
Out[167]:
In [168]:
np.array(range(9)).reshape(3,-1)
Out[168]:
In [169]:
np.vstack((a.reshape(4,-1),np.array(range(9)).reshape(3,-1)))
Out[169]:
In [170]:
arr1
Out[170]:
In [171]:
arr2
Out[171]:
In [173]:
np.dstack((arr1,arr2))
Out[173]:
In [174]:
np.dstack((arr1,arr2)).shape
Out[174]:
In [176]:
np.stack((arr1,arr2))
Out[176]:
In [179]:
np.stack((arr1,arr2)).shape
Out[179]:
In [177]:
np.stack((arr1,arr2),axis=1)
Out[177]:
In [178]:
np.stack((arr1,arr2),axis=1).shape
Out[178]:
In [183]:
np.stack((arr1,arr2),axis=2)
Out[183]:
In [185]:
x = np.arange(3)
y = np.arange(5)
X, Y = np.meshgrid(x, y)
X
Out[185]:
In [186]:
Y
Out[186]:
In [187]:
[list(zip(x, y)) for x, y in zip(X, Y)]
Out[187]:
In [ ]: