프로그래머의 삶 Programmer's Life/Python, Numpy, Pandas

Numpy functions

Oliver's World 2022. 6. 9. 22:13
728x90

There are various useful functions in the numpy library. Thus, I left them here to use later.

Numpy 라이브러리에는 다양한 유용한 기능이 있습니다. 나중에 사용하기 위해 여기에 간단한 설명과 함께 남겨 두었습니다.

 

1. shape

# shape : dimension of the array
print(arr1.shape)

 

2. zeros

# zeros(size, dtype=) : dtype in bool/float/int, default is float
arr6 = np.zeros(((3,2)), dtype=int)

 

3. random

# random(shape) : shape dimension random number array generator
print(np.random.random((3,2))

 

4. random.normal

# np.random.normal(mean, std, arry size) - generating array that consist of normal distribution samples
arr7 = np.random.normal(0, 1, 6)
arr8 = np.random.normal(0, 1, (5,3))

 

5. reshape

# horizon vector to vertical vector
# (array).reshape : changing array structure
print(str(arr4.reshape(6,1)))
print(str(arr4.reshape(-1,1)))

 

6. random.randint

randEx = np.random.randint(1,45, size=5)

 

7. sort

# up-down ascending
print(np.sort(arr8, axis=0), end="\n!!!\n")
# ascending
print(np.sort(arr8, axis=1), end="\n!!!\n")
# up-down descending
print(np.sort(arr8, axis=0)[::-1], end="\n!!!\n")

 

 

8. Mathematical expression

x = np.array([1,2,3,5])
# Calculate the exponential of all elements
print('exp' + str(np.exp(x))) 
# Calculate `2**p` for all `p`
print('exp2' + str(np.exp2(x))) 
# First array elements raised to powers from second array, element-wise
print('power' + str(np.power(3, x)))
# Natural logarithm, element-wise
print('log' + str(np.log(x)))
# Return the base 10 logarithm of the input array, element-wise
print('log10' + str(np.log10(x)))

 

9. Pythagorean theorem(hypot and linalg. norm - same result if there're 2 parameters)

arr = np.array([1,2,3,4])
# Pythagorean theorem
print(np.hypot(arr[0], arr[1])) # by 2 parameters
print(np.linalg.norm(arr)) # by more than 2 parameters

 

10. vector distance(np.linalg.norm)

arr = np.arange(3, 5)
arr2 = np.arange(12, 14)
print(np.linalg.norm(arr-arr2))

 

11. Identity matrix

# create identity matrix
print(np.eye(3,3)) # same with np.eye(3)

 

 

12. create the same random arrays by seed

np.random.seed(1)
print(np.random.rand(5))
np.random.seed(0)
print(np.random.rand(5))
np.random.seed(1)
print(np.random.rand(5))

 

13. np.argmax & np.max 

a = np.array([[3,7,5], [6,7,8], [3,5,9]])
# to get index of max #
print(np.argmax(a, axis=1))
print(np.argmax(a, axis=0))
# to get value of max #
print(a.max(axis=0))
print(a.max(axis=1))

 

14. np.poly1d & np.polyder

eq = np.array([1, 0, -2, 1])
# create one-dimensional polynomial
p = np.poly1d(eq)
print(p)
# p.variable : name of the polynomial variable
print(p.variable)
# p.order : order or degree of the polynomial
print(p.order)
#  p.roots : answer of the equation p
print(p.roots)
#  np.linalg.solve(array1, array2) : answer of the system of equations, array1 and array2.
# differential equation by p(created by np.poly1d
print(np.polyder(p)) # same function with np.polyder : p.deriv()

 

15. np.diff

x = np.array([1,2,34,7,10,14])
print(np.diff(x))
# diff of diff x - diff two times by n=2 option
print(np.diff(x, n=2))
# three times by n=3 option
print(np.diff(x, n=3))

 

16. np.gradient

x = np.array([1,2,34,7,10,14])
# solving differential equation of array data
print(np.gradient(x))

 

728x90