[Numpy] Numpy 연습문제 21~30
🐍Python/Numpy

[Numpy] Numpy 연습문제 21~30

728x90
반응형

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 random matrix 

x = np.random.random((5,5))
normalize = (x - np.mean(x))/np.std(x)
normalize

A : array([[ 1.55485212, -1.48474094, -0.27353692,  1.35216114,  0.52469029],
       [ 0.08559353,  0.55374601, -1.3824064 ,  0.9755402 ,  1.55400283],
       [-1.3515868 , -0.98196135,  0.99754364,  0.22290204, -0.53569895],
       [-0.86984274, -1.08751141, -0.03489505,  0.98250113,  1.44086173],
       [-0.52950529, -0.36418541,  0.80080579, -1.33838551, -0.81094367]])

해설 : np.random.random((5,5))로 5,5행렬을 만들고 normalize(정규화)(Standardization) 하기 위해 공식을 이용한다. 

23. Create a custom dtype that describes a color as four unsigned bytes (RGBA)

-> col = np.dtype([('r',np.ubyte,1),
                 ('g',np.ubyte,1),
                ('b',np.ubyte,1),
                ('a',np.ubyte,1)])
    col

A : dtype([('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')])

해설 :

각각의 색깔에 속하게 한다.

24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)

-> A = np.random.random((5,3))
    B = np.random.random((3,2))
    A@B
    #or
    Z = np.dot(np.ones((5,3)),np.ones((3,2)))
    Z

A : array([[3., 3.],
       [3., 3.],
       [3., 3.],
       [3., 3.],
       [3., 3.]])

해설 : @를 통해 mat product를 실행할 수 있고, np.dot을 이용하여 바로 곱할 수 있다. 


25. Given a 1D array, negate all elements which are between 3 and 8, in place.

-> Z = np.arange(11)
    Z[(3<Z) & (Z<8)] *= -1
    Z

Aarray([ 0,  1,  2,  3, -4, -5, -6, -7,  8,  9, 10])

해설 : 3,8사이를 -값으로 만들기 위해 일단 먼저 11개를 array했고, 4부터 7까지 *= -1을 통해 음수화하였다. 

26. What is the output of the following script?

print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))

-> np.sum(range(5),-1)

A : 10

해설 : range의 합을 구할 때, np.sum을 통해 간단하게 구할 수 있다. 

27. Consider an integer vector Z, which of these expressions are legal?

Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

-> Z = np.random.choice(10,4)
print(Z**Z) 
print(2 << Z >> 2)
print(Z <- Z)
print(1j*Z)
print(Z/1/1)
print(Z<Z>Z) 

A : print(Z<Z>Z)는 illegal한 것이다

28. What are the result of the following expressions?

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)

A : nan
    0
    array([-9.22337204e+18])

해설 : array 나누기는 불가하다. 하지만 // : 나누기 연산 후 소수점 이하의 수를 버리고, 정수 부분의 수만 구하는 것은 가능하다.

29. How to round away from zero a float array ?

-> Z = np.random.uniform(-10,10,10)
    np.copysign(np.ceil(np.abs(Z)),Z)

A : array([-1., -9., -8.,  4.,  6., -5., -3.,  7., -7., -5.])

해설 :

np.abs로 절대값을 씌워준다. 그 후 np.ceil을 통해 올림(반올림X)해준다.
Copysign : Change the sign of x1 to that of x2, element-wise.
해당 값을 기존 Z의 부호대로 바꿔준다

30. How to find common values between two arrays?

-> A = np.random.randint(0,10,10)
    B = np.random.randint(0,10,10)
    print(A,B)
    np.intersect1d(A,B)

A : 
[4 2 0 7 5 6 7 8 2 8] [5 2 2 4 3 6 2 3 7 7]
array([2, 4, 5, 6, 7])

해설 : np.random.randint(low,high,size) 랜덤한 숫자를 나열하고 intersect1d를 통해 공통된 인자를 골라낸다.


728x90
반응형