아래 사이트에 올라온 문제를 풀면서 기초를 다지기 시작하였다.
https://github.com/rougier/numpy-100
1. Import the numpy package under the name `np` (★☆☆)
import numpy as np
2. Print the numpy version and the configuration (★☆☆)
print(np.__version__)
3. Create a null vector of size 10 (★☆☆)
a = np.zeros(10)
print(a)
4. How to find the memory size of any array (★☆☆)
a = np.array([[1,2],[3,4]])
print(a.nbytes)
5. How to get the documentation of the numpy add function from the command line? (★☆☆)
# 두가지 다 동일한 결과를 얻으니 둘 중 하나 기억하기
print(np.add.__doc__)
help(np.add)
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
a = np.zeros(10)
a[4] = 1
print(a)
7. Create a vector with values ranging from 10 to 49 (★☆☆)
a = np.arange(10,50)
print(a)
8. Reverse a vector (first element becomes last) (★☆☆)
# b와 c 둘다 동일한 결과 얻을 수 있다.
a = np.arange(10)
b = np.sort(a, axis=None)[::-1]
c = np.flip(a)
print(b)
print(c)
9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
a = np.arange(9).reshape(3,3)
print(a)
10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
a = np.array([1,2,0,0,4,0])
b = np.where(a != 0)
print(b)
11. Create a 3x3 identity matrix (★☆☆)
a = np.eye(3)
print(a)
12. Create a 3x3x3 array with random values (★☆☆)
a = np.zeros((3,3,3))
b = np.where(a == 0, np.random.rand(*a.shape),a)
print(b)
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
a = np.random.rand(10,10)
print(np.max(a), np.min(a))
14. Create a random vector of size 30 and find the mean value (★☆☆)
a = np.random.rand(30)
print(np.mean(a))
15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
n, m = 6,4
a = np.ones([n,m])
a[1:n-1,1:m-1] = 0
print(a)
16. How to add a border (filled with 0's) around an existing array? (★☆☆)
a = np.array([[1,2],[2,1]])
b = np.pad(a,1,mode='constant',constant_values=0)
print(b)
17. What is the result of the following expression? (★☆☆)
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 (★☆☆)
org = [1,2,3,4]
a = np.diag(org, k=-1)
print(a)
19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
a = np.zeros((8,8))
a[::2,::2]=1
a[1::2,1::2]=1
print(a)
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)
org = (6,7,8)
end = org[0]*org[1]*org[2]
a = np.arange(end).reshape(org)
print(a.flat[99])
21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)
a = np.array([[1,0],[0,1]])
b = np.tile(a,(4,4))
print(b)
22. Normalize a 5x5 random matrix (★☆☆)
a = np.random.rand(5,5)
b = (a-np.min(a))/(np.max(a)-np.min(a))
print(b)
23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
rgba = np.dtype([('R',np.ubyte),('G',np.ubyte),('B',np.ubyte),('A',np.ubyte)])
print(rgba)
24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
a = np.arange(15).reshape(5,3)
b = np.arange(6).reshape(3,2)
#print(a*b) # 오류 발생, 차원 수가 다르며 호환 불가능하기에 브로드캐스팅 실패
print(np.dot(a,b)) # np.dot(a,b): a행렬의 열 수와 b행렬의 행 수가 일치하는 경우 행렬 곱셈 가능
25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
a = np.arange(10)
a[3:9] *= -1
print(a)
26. What is the output of the following script? (★☆☆)
print(sum(range(5),-1)) # 9
from numpy import *
print(sum(range(5),-1)) # 10
27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)
Z = np.arange(3)
print(Z**Z)
print(2 << Z >> 2)
print(Z <- Z)
print(1j*Z)
print(Z/1/1)
print(Z<Z>Z)
28. What are the result of the following expressions? (★☆☆)
print(np.array(0) / np.array(0))
print(np.array(0) // np.array(0))
print(np.array([np.nan]).astype(int).astype(float))
29. How to round away from zero a float array ? (★☆☆)
a = np.array([1.0,2.0,3.0])
print(np.round(a))
30. How to find common values between two arrays? (★☆☆)
a = np.array([1,2,3,4,5])
b = np.array([3,4,5,6,7])
print(np.intersect1d(a,b))
31. How to ignore all numpy warnings (not recommended)? (★☆☆)
np.seterr(all='ignore')
a = np.divide(0,0)
32. Is the following expressions true? (★☆☆)
print(np.sqrt(-1) == np.emath.sqrt(-1)) # False
33. How to get the dates of yesterday, today and tomorrow? (★☆☆)
import datetime
today = np.datetime64(datetime.date.today())
value = np.timedelta64(1,'D')
print(today - value)
print(today)
print(today + value)
34. How to get all the dates corresponding to the month of July 2016? (★★☆)
import datetime
a = np.datetime64(datetime.date(2016,7,1))
b = np.datetime64(datetime.date(2016,8,1))
gap = np.arange(a,b,dtype='datetime64[D]')
print(gap)
35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
a = np.random.rand(3,3)
b = np.random.rand(3,3)
np.add(a,b,out=b)
np.divide(a,-0.5,out=a)
print(np.multiply(a,b))