반응형

 

#1. Import the numpy package under the name np

import numpy as np

2. Print the numpy version and the configuration

import numpy as np
print(np.__version__)

3. Create a null vector of size 10

data = np.zeros(10)
data

4. How to find the memory size of any array

#메모리 크기 확인
data = np.zeros((5,5))
print(data.size)
print(data.itemsize)
print("%d bytes" % (data.size * data.itemsize))

5. How to get the documentation of the numpy add function from the command line?

np.info(np.add)

6. Create a null vector of size 10 but the fifth value which is 1

10게 에서 5번째는 1로 한다.

data = np.zeros(10)
data[4] = 1
data

7. Create a vector with values ranging from 10 to 49

data = np.arange(10,50)
data

8. Reverse a vector (first element becomes last)

data = np.arange(10)
data = data[::-1]
data

9. Create a 3x3 matrix with values ranging from 0 to 8

data = np.arange(9).reshape(3,3)
data

10. Find indices of non-zero elements from [1,2,0,0,4,0]

data = np.nonzero([1,2,0,0,4,0])
data

 

11. Create a 3x3 identity matrix 

data = np.eye(3)
print(data)

12. Create a 3x3x3 array with random values

data = np.random.random((3,3,3))
data

13. Create a 10x10 array with random values and find the minimum and maximum values

import numpy as np
data = np.random.random((10,10))
_min, _max = data.min(), data.max()
_min, _max

14. Create a random vector of size 30 and find the mean value

data = np.random.random(30)
print(data.mean())

15. Create a 2d array with 1 on the border and 0 inside

#외부는 1 내부는 0으로 해준다.
import numpy as np
data = np.ones((5,5))
data[1:-1, 1:-1] = 0
data

16. How to add a border (filled with 0's) around an existing array?

data = np.zeros((5,5))
print(data)
print('-'* 10)
# constant_values 주위의 값을 이것으로 한다.
data = np.pad(data,pad_width = 1, mode='constant', constant_values=0)
print(data)

 

17. What is the result of the following expression?

0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)

18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal 

#np.diag
data = np.diag(1+np.arange(4), k = -1)
data

19. Create a 8x8 matrix and fill it with a checkerboard pattern

data = np.zeros((8,8))
data[::2, 1::2] = 1
data[1::2,::2] = 1
data

20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?

#np.unravel_index
data = np.unravel_index(99, (6,7,8))
data

21. Create a checkerboard 8x8 matrix using the tile function

data = np.array([[0, 1],[1,0]]) 
data = np.tile(data, (4,4)) 
data

22. Normalize a 5x5 random matrix

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

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

#np.dtype
data = np.dtype([("r",np.ubyte, 1),("g",np.ubyte, 1),("b",np.ubyte, 1),("a",np.ubyte, 1)])
data

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

data = np.dot(np.ones((5,3)), np.ones((3,2)))
print(data)

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

# 3부터 8까지 마이너스 로 바꾸기

data = np.arange(10)
data[(3<= data) & (data<=8)] *= -1
print(data)
반응형

'문제 > Numpy 100제' 카테고리의 다른 글

numpy exercise-4  (0) 2021.02.03
100 numpy exercises-3  (0) 2021.01.29
100 numpy exercises-2  (0) 2021.01.28

+ Recent posts