Numpy_practice

6 minute read

Array creation

1. [“1”, “4”, 5, 8] 를 numpy로 Float로 선언

import numpy as np
aa = np.array(["1", "4", 5, 8], float)
aa
array([1., 4., 5., 8.])

2. 위의 것 shape확인.

aa.shape
(4,)

3. 전체 type확인.

aa.dtype

4. 아래의 tensor를 array로 선언하고 쉐입, 차원, 사이즈 확인

tensor = [[[1,2,5,8],[1,2,5,8],[1,2,5,8]], [[1,2,5,8],[1,2,5,8],[1,2,5,8]], [[1,2,5,8],[1,2,5,8],[1,2,5,8]], [[1,2,5,8],[1,2,5,8],[1,2,5,8]]]

tensor  = [[[1,2,5,8],[1,2,5,8],[1,2,5,8]],
           [[1,2,5,8],[1,2,5,8],[1,2,5,8]],
           [[1,2,5,8],[1,2,5,8],[1,2,5,8]],
           [[1,2,5,8],[1,2,5,8],[1,2,5,8]]]
ten_array = np.array(tensor, float)
ten_array.shape, ten_array.ndim, ten_array.size
((4, 3, 4), 3, 48)

Handling shape

5. 위 행렬을 array로 선언하기

  • test_matrix = [[1,2,3,4], [1,2,5,8]]
test_matrix = [[1,2,3,4], [1,2,5,8]]
test_array = np.array(test_matrix)
test_array
array([[1, 2, 3, 4],
       [1, 2, 5, 8]])

6. 2X2X2 행렬로 변환

test_array.reshape(2,2,2)
array([[[1, 2],
        [3, 4]],

       [[1, 2],
        [5, 8]]])

7. 2행짜리로 변환하고 열은 element갯수에 맞춰서 만들기

test_array.reshape(2,-1)
array([[1, 2, 3, 4],
       [1, 2, 5, 8]])

8. 위행렬을 flatten 시키기. 2가지방법으로

  • test_matrix = [[[1,2,3,4], [1,2,5,8]], [[1,2,3,4], [1,2,5,8]]]
#1
test_array.flatten()
array([1, 2, 3, 4, 1, 2, 5, 8])
#2
test_array.reshape(1,-1).squeeze()
array([1, 2, 3, 4, 1, 2, 5, 8])

Indexing And Slicing

9. 위행렬을 행:전체, 열:2열이상 선택하기

test_exmaple = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], int)

test_exmaple = np.array([[1, 2, 3, 4, 5],
                         [6, 7, 8, 9, 10]], int)
test_exmaple[:, 2:]
array([[ 3,  4,  5],
       [ 8,  9, 10]])

10. 행:1행, 열: 1~2열 선택

test_exmaple[1, 1:3]
array([7, 8])

11. 행:1~2행 열:전체 선택

test_exmaple[1:3, :]
array([[ 6,  7,  8,  9, 10]])

12. 0~99까지를 10*10짜리 array만들고

12-1 이중 9, 19, 29,와 같이 9로 끝나는애들만 1열짜리로 뽑아내라.

#1 
a = np.arange(0,100).reshape(-1,10)
a
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
       [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
       [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
#2
a[:,-1].reshape(-1,1)
array([[ 9],
       [19],
       [29],
       [39],
       [49],
       [59],
       [69],
       [79],
       [89],
       [99]])

Creation array

13. 0에서 5까지 0.5 step으로 만들어라

a = np.arange(0, 5.5, 0.5)
a
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])

14. 위 array를 list로 변환해라.

list(a)
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

15. 0으로 가득한 2*5 matrix만들어라

np.zeros((2,5))
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

16. 3*3짜리 identity 행렬만들어라

np.identity(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

Operation functions

17. 아래 어래이를 열방향으로 sum한 array를 반환.

  • test_array = np.arange(1,13).reshape(3,4)
test_array = np.arange(1,13).reshape(3,4)
test_array.sum(axis=1)
array([10, 26, 42])

18. 위 어래이를 행방향으로 sum한 것을 반환

test_array.sum(axis=0)
array([15, 18, 21, 24])

19. 위 두개를 a 아래행에 b행이오게 붙여라

  • a = np.array([[1, 2, 3]])
  • b = np.array([[2, 3, 4]])
a = np.array([[1, 2, 3]])
b = np.array([[2, 3, 4]])
np.vstack((a,b))
array([[1, 2, 3],
       [2, 3, 4]])

20. 위 a의 2x2에 b를 새열로 들어가게 해서 2x3으로 만들어라.(힌트 Tr)

  • a = np.array([[1, 2], [3, 4]])
  • b = np.array([[5, 6]])
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate((a,b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])

Array operation

21. a기준으로 2*3행렬이 되게 a와 b를 element 더하기해라

  • test_a = np.arange(1,7).reshape(2,3)
  • test_b = np.arange(7,13).reshape(3,2)
test_a = np.arange(1,7).reshape(2,3)
test_b = np.arange(7,13).reshape(3,2)
test_a + test_b.T
array([[ 8, 11, 14],
       [12, 15, 18]])

22. a기준으로 2*3행렬이 되게 a와 b를 element 빼라해라

test_a - test_b.T
array([[-6, -7, -8],
       [-4, -5, -6]])

23. a, b array를 dot product해라.

test_a.dot(test_b)
array([[ 58,  64],
       [139, 154]])

24. 위 matrix를 행단위로 vector와 더하게 하라.

  • test_matrix = np.arange(1,13).reshape(4,3)
  • test_vector = np.arange(10,40,10)
test_matrix = np.arange(1,13).reshape(4,3)
print('test_matrix',test_matrix, '\n')
test_vector = np.arange(10,40,10)
print('test_vector',test_vector,'\n')
test_matrix+test_vector
test_matrix [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]] 

test_vector [10 20 30] 






array([[11, 22, 33],
       [14, 25, 36],
       [17, 28, 39],
       [20, 31, 42]])

comparisons

25. a라는 벡터를 0~9 element로 선언하라.

a = np.arange(0,10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

26. 25번의 벡터에서 0보다 큰애들은 true, 아닌애들 false로 array를 행렬만들어라

a>0
array([False,  True,  True,  True,  True,  True,  True,  True,  True,
        True])


27. (5보다큰것이 있는지, 0보다 작은애가 있는지) 로 결과값받아라

any(a>5), any(a<0)
(True, False)

28. (전부 5보다 큰지, 전부 10보다 작은지)로 결과값받아라.

all(a>5), all(a<=10)
(False, True)

29. 아래 Array에서 0보다 크면 3을, 아니면 2를 element로 받게 하라

  • a = np.array([1, 3, 0], float)
np.where(a>0, 3, 2)
array([2, 3, 3, 3, 3, 3, 3, 3, 3, 3])

30. 0보다 큰애들의 인덱스만 array로 받아라

np.where(a>0)
(array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64),)

31. 최대값/최소값의 index를 있게 array반환

  • a = np.array([1,2,4,5,8,78,23,3])
a = np.array([1,2,4,5,8,78,23,3])
print(a)
np.argmax(a), np.argmin(a)
[ 1  2  4  5  8 78 23  3]





(5, 0)

32. 아래 Array를 열방향으로 sum했을때 최대값이 있는 index를 반환하라.

  • a = np.array([[1,2,4,7],[9,88,6,45],[9,76,3,4]])
a = np.array([[1,2,4,7],[9,88,6,45],[9,76,3,4]])
print('a: ',a)
print('\n')
print('a.sum(axis=1) : ',a.sum(axis=1))
np.argmax(a.sum(axis=1))
a:  [[ 1  2  4  7]
 [ 9 88  6 45]
 [ 9 76  3  4]]


a.sum(axis=1) :  [ 14 148  92]





1
  1. 행방향끼리비교해서 최대값들을 array로 받아라.
a.max(axis=0)
array([ 9, 88,  6, 45])

34. 위 array중 15보다 큰애들만 1, 작은애들은 0으로 되게 array를 받아라. (astype이용)

A = np.array([ [12, 13, 14, 12, 16, 14, 11, 10, 9], [11, 14, 12, 15, 15, 16, 10, 12, 11], [10, 12, 12, 15, 14, 16, 10, 12, 12], [ 9, 11, 16, 15, 14, 16, 15, 12, 10], [12, 11, 16, 14, 10, 12, 16, 12, 13], [10, 15, 16, 14, 14, 14, 16, 15, 12], [13, 17, 14, 10, 14, 11, 14, 15, 10], [10, 16, 12, 14, 11, 12, 14, 18, 11], [10, 19, 12, 14, 11, 12, 14, 18, 10], [14, 22, 17, 19, 16, 17, 18, 17, 13], [10, 16, 12, 14, 11, 12, 14, 18, 11], [10, 16, 12, 14, 11, 12, 14, 18, 11], [10, 19, 12, 14, 11, 12, 14, 18, 10], [14, 22, 12, 14, 11, 12, 14, 17, 13], [10, 16, 12, 14, 11, 12, 14, 18, 11]])

A = np.array([
[12, 13, 14, 12, 16, 14, 11, 10,  9],
[11, 14, 12, 15, 15, 16, 10, 12, 11],
[10, 12, 12, 15, 14, 16, 10, 12, 12],
[ 9, 11, 16, 15, 14, 16, 15, 12, 10],
[12, 11, 16, 14, 10, 12, 16, 12, 13],
[10, 15, 16, 14, 14, 14, 16, 15, 12],
[13, 17, 14, 10, 14, 11, 14, 15, 10],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 19, 12, 14, 11, 12, 14, 18, 10],
[14, 22, 17, 19, 16, 17, 18, 17, 13],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 19, 12, 14, 11, 12, 14, 18, 10],
[14, 22, 12, 14, 11, 12, 14, 17, 13],
[10, 16, 12, 14, 11, 12, 14, 18, 11]])

np.where(A>15, 1,0)
array([[0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 0, 0, 1, 0]])

35. 0->2, 1->4, 2->6, 3->8이 되게 아래 b array를 변환해서 반환하라. 기준이되는 a를 만들고 2가지 방법으로 되게하라.

  • b = np.array([0, 0, 1, 3, 2, 1], int) # 반드시 integer로 선언
b = np.array([0, 0, 1, 3, 2, 1], int) # 반드시 integer로 선언
a = np.array([2,4,6,8,], float)
#1
a.take(b)
array([2., 2., 4., 8., 6., 4.])
#2
a[b]
array([2., 2., 4., 8., 6., 4.])

Updated: