Contents
1.1. np.arange ¶
Return evenly spaced values within a given interval https://numpy.org/doc/stable/reference/generated/numpy.arange.html
ex.
ex.
> print(np.arange(6)) [0 1 2 3 4 5] > print(np.arange(6) + 1) [1 2 3 4 5 6]
1.3. v.reshape ¶
>>> v = np.arange(24) + 1 >>> v.reshape(4, 2, 3) array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24]]])
이것의 반대는 flatten.
1.4. v.flatten ¶
바로 위 v.reshape 한 것에 대해,
>>> v.flatten() array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
1.5. v.ravel ¶
저 위 v.reshape 한 것에 대해,
>>> v.ravel() array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])flatten과 똑같이 보이지만... 메모리에 있는 내용을 share하는지 여부가 다르다. 아래 np.may_share_memory 참조.
1.6. np.may_share_memory ¶
>>> np.may_share_memory(v, v.flatten()) False >>> np.may_share_memory(v, v.ravel()) True
1.7. 입력 1 - np.array ¶
전체를 bracket으로 묶어줘야 오류가 나지 않는다.
>>> X = np.array([1,2],[3,4]) Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> X = np.array([1,2],[3,4]) TypeError: Field elements must be 2- or 3-tuples, got '3' >>> X = np.array([[1,2],[3,4]]) >>> print(X) [[1 2] [3 4]] >>>
1.8. 입력 2 - concatenative하게. ¶
>>> X = np.arange(1,10).reshape(3,3) >>> print(X) [[1 2 3] [4 5 6] [7 8 9]] >>>
1.10. np.eye - 항등행렬 ¶
단위행렬,unit_matrix 항등행렬,identity_matrix
np.eye(차원, dtype='타입')
타입 생략하면 TBW ... np.eye
ex.
np.eye(차원, dtype='타입')
타입 생략하면 TBW ... np.eye
ex.
>>> I1 = np.eye(3, dtype='int') >>> I1 array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> I2 = np.eye(3, dtype='float') >>> I2 array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
1.11. np.ones - 1로만 구성된 행렬 ¶
np.eye와 마찬가지로 차원과 타입 지정.
>>> Z1 = np.ones((2, 3)); print(Z1) [[1. 1. 1.] [1. 1. 1.]] >>> Z2 = np.ones((2, 3), dtype='uint8'); print(Z2) [[1 1 1] [1 1 1]]
2. ? ¶
from https://wikidocs.net/22849
{
np.array
np.random.randint(9)는 0부터 9까지 숫자중 아무 숫자나 하나 생성
np.random.randint(숫자1, 숫자2, size = 난수갯수)
}
{
np.array
a = [1,2,3,4,5] # list a를 만들어서 b = np.array(a) # array 자료형으로 바꾸자 # 여기서 type(b) 하면 numpy.ndarray 라고 나온다. nd는 다차원(n-dimensional)을 뜻함.np.sqrt, multiply(내적), max, min, argmax, argmin(최대값 최소값의 index)
np.random.randint(9)는 0부터 9까지 숫자중 아무 숫자나 하나 생성
np.random.randint(숫자1, 숫자2, size = 난수갯수)
}
3.1. ndarray ¶
a가 있을 때
a.shape : 크기 (x, y)를 돌려줌.
a.reshape(x, y) : 크기 x, y로 바꿈.
a.shape : 크기 (x, y)를 돌려줌.
a.reshape(x, y) : 크기 x, y로 바꿈.
np.zeros(n) : 0.으로만 이뤄진 배열을 만듦
np.ones(n) : 1.로만
다차원 배열일때는
np.zeros((행 갯수, 열 갯수)) 처럼 괄호를 두번 써야 함
np.ones(n) : 1.로만
다차원 배열일때는
np.zeros((행 갯수, 열 갯수)) 처럼 괄호를 두번 써야 함
원소의 index를 찾으려면 (list처럼 .index()가 아님)
np.where('찾으려는 원소' == array이름)
np.where('찾으려는 원소' == array이름)
np.sum(a) : 합
np.arange(n) : 수열을 만듦. array형으로.
np.arange(n) : 수열을 만듦. array형으로.
4. tmp; from Simplenote ¶
// 2018-10-04 작성인데 뭐 하다가 적은건지 기억이 안나네
import numpy as np 배열 만들기 x = np.array([1, 2.0, 3.0]) 다차원 배열 만들기 (2차원 배열: 행렬) >>> a=np.array([[1,2],[3,4]]) >>> print(a) [[1 2] [3 4]] >>> a.shape (2, 2) >>> a.dtype #원소의 자료형 dtype('int32') >>> b=np.array([[3,0],[0,6]]) >>> a+b array([[ 4, 2], [ 3, 10]]) >>> a*b array([[ 3, 0], [ 0, 24]]) 원소 접근 >>> x=np.array([[51,55],[14,19],[0,4]]) >>> print(x) [[51 55] [14 19] [ 0 4]] >>> x[0] array([51, 55]) >>> x[0][1] 55 >>> for row in x: print(row); [51 55] [14 19] [0 4] >>> z = x.flatten() >>> print(z) [51 55 14 19 0 4] >>> z[np.array([0,2,4])] # 인덱스가 0,2,4인 원소 얻기 array([51, 14, 0]) >>> x>15 array([[ True, True], [False, True], [False, False]]) >>> z>15 array([ True, True, False, True, False, False]) >>> z[z>15] # 15 초과인 값만 구하기 array([51, 55, 19]) 행렬의 내적 >>> A = np.array([[1, 2], [3, 4]]) >>> A.shape (2, 2) >>> B = np.array([[5, 6], [7, 8]]) >>> np.dot(A, B) array([[19, 22], [43, 50]])
5. NumPy의 간단한 통계 기능들 ¶
from https://wikidocs.net/23279
{
}
본격적인 통계 기능은 보통 팬더스,Pandas를 사용.
{
평균,mean,average | np.average(x) |
중앙값,median - curr see 대표값,평균값,중앙값,최빈값 | np.median(x) |
최빈값 | np.bincount(x).argmax() |
분산,variance | np.var(x) |
표준편차,standard_deviation | np.std(x) |
본격적인 통계 기능은 보통 팬더스,Pandas를 사용.
6. 기타 ¶
MathInspector
https://mathinspector.com/
A Visual Programming Environment for Scientific Computing
Free open source software designed for students, content creators, and professional mathematicians
https://mathinspector.com/
A Visual Programming Environment for Scientific Computing
Free open source software designed for students, content creators, and professional mathematicians