🐍Python/Numpy

    [Numpy] Numpy 연습문제 61~70

    61. Find the nearest value from a given value in an array (★★☆)(hint: np.abs, argmin, flat)In [9]:Z = np.random.uniform(0,1,10) z = 0.5 m = Z.flat[np.abs(Z-z).argmin()] print(m) # flat은 펴주는 것 0.5425169605826707 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)(hint: np.nditer)In [31]:A = np.arange(3).reshape(1,3) B = np.arange(3).reshape(3,1..

    [Numpy] Numpy 연습문제 51~60

    51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)(hint: dtype)In [4]:Z = np.zeros(10,[('position',[('x',float,1), ('y',float,1)]), ('color', [('r',float,1), ('g',float,1), ('b',float,1)])]) Z Out[4]:array([((0., 0.), (0., 0., 0.)), ((0., 0.), (0., 0., 0.)), ((0., 0.), (0., 0., 0.)), ((0., 0.), (0., 0., 0.)), ((0., 0.), (0., 0., 0.)), ((0., 0.), (0., 0., 0.)), ..

    [Numpy] Numpy 연습문제 41~50

    41. How to sum a small array faster than np.sum? -> A = np.arange(10) np.add.reduce(A) A : 45 42. Consider two random array A and B, check if they are equal-> A = np.random.randint(0,2,5) B = np.random.randint(0,2,5) print(np.allclose(A,B)) print(np.array_equal(A,B)) A : FalseFalse 해설 : np.random.randint(~부터,~까지,갯수)로 random한 수를 만든다. 그리고 np.allclose / np.array_equal로 이 둘이 같은지 확인한다. 43. Make an ar..

    [Numpy] Numpy 연습문제 31~40

    31. How to ignore all numpy warnings (not recommended)? -> # Suicide mode on defaults = np.seterr(all="ignore") Z = np.ones(1) / 0 # Back to sanity _ = np.seterr(**defaults) An equivalent way, with a context manager: with np.errstate(divide='ignore'): Z = np.ones(1) / 0 해설 : 처음에 all을 ignore로 설정하여 오류발생시 아무 action도 취하지 않게 했다. 그리고 **default로 원래대로 돌려놓는다. 그리고 경고를 표시하지 않으려고 numpy.errstate(divide = ‘ig..

    [Numpy] Numpy 연습문제 21~30

    21. Create a checkerboard 8x8 matrix using the tile function -> Z = np.tile(np.array([[0,1],[1,0]]),(4,4)) Z A : array([[0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0, 1, 0]]) 해설 : np.array로 0,1 / 1,0을 만들고 가로, 세로4번 반복해준다. 22. Normalize a 5x5 ran..

    [Numpy] Numpy 연습문제 11~20

    11. Create a 3x3 identity matrix -> Z = np.eye(3,3) Z A : array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) 해설 : np.eye를 통해 대각선 행렬성분이 1인 Identity matrix를 생성할 수 있다. (A,B)로 A행 B열로 구성할 수 있다. 12. Create a 3x3x3 array with random values-> Z = np.random.random((3,3,3)) Z A : array([[[0.9849039 , 0.44041333, 0.41412264], [0.13367628, 0.99776952, 0.58991017], [0.01979863, 0.41352766, 0.00831818]], [[0.5..