W3schools - Python_Numpy Unfunc
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Numpy unfuncs
ufuncs stands for “Universal Functions” and they are Numpy functions that operates on the ndarray
object
Are used to implement vectorization in Numpy which is way faster than iterating over elements
They provide broadcasting and additional methods like reduce, accumulate
They take additional args
-
where : Boolean array or condition defining where the operations should take place
-
dtype : defining the return type of elements
-
out : output array where the return value should be copied
Vectorization
- Converting iterative statements into a vector based operation is called vectorization
import numpy as np
x = [1,2,3,4]
y = [4,5,6,7]
z = []
for i, j in zip(x,y):
z.append(i+j)
print(z)
z = np.add(x,y)
print(z)
# It will produce the same result
Create ufunc
To define a function, then add it to Numpy ufunc library with the frompyfunc()
It takes 3 args
-
function : the name of the function
-
inputs : the number of input arguments(arrays)
-
outputs : the number of output arrays
import numpy as np
def myadd(x,y):
return x+y
myadd = np.frompyfunc(myadd,2,1)
print(myadd([1,2,3,4],[5,6,7,8])) # output [6,8,10,12]
Simple Arithmetic
add(), subtract(), multiply(), divide(), power(), remainder() and mod(), divmod(), absolute()
Rounding Decimal
Truncation
- trunc(), fix()
Rounding
- around()
Floor
- floor()
Ceil
- ceil()
Logs
Numpy provides functions to perform log at the base 2, e and 10
All of the log functions will place -inf or inf in the elements if the log can’t be computed
import numpy as np
arr = np.arange(1, 10)
# log at the base 2
print(np.log2(arr))
# log at the base 10
print(np.log10(arr))
# log at the base e
print(np.log(arr))
# log at any base
from math import log
nplog = np.frompyfunc(log,2,1)
print(nplog(100,15))
Summations
Addition is done between two args whereas summation happens over n elements
import numpy as np
arr1 = np.array([1,2,3])
arr2 = np.array([1,2,4])
arr3 = np.add(arr1,arr2) # output [2 4 7]
arr4 = np.sum([arr1,arr2]) # output 13
arr5 = np.sum([arr1,arr2], axis=1) # output [6 7]
# Cummulative sum, means partially adding the elements in array
arr6 = np.cumsum(arr1) # output [1 3 6]
Products
To find the product of the elements in an array
import numpy as np
arr1 = np.array([1,2,3,4])
arr2 = np.array([5,6,7,8])
a = np.prod(arr1) # output 24
b = np.prod([arr1,arr2]) # output 40320
c = np.prod([arr1,arr2], axis=1) # output [24 1680]
d = np.cumprod([arr]) # output [1 2 6 24]
Differences
A discrete difference means subtracting two successive elements
It finds the discrete difference
import numpy as np
arr = np.array([10,15,25,5])
arr1 = np.diff(arr) # output [5 10 -20]
# Can perform this operation repeatedly by giving parameter n
arr2 = np.diff(arr, n=2) # output [5 -30]
LCM
Lowest Common Multiple
Is the least number that is common multiple of both of the numbers
import numpy as np
num1 = 4
num2 = 6
x = np.lcm(num1,num2) # output 12
arr = np.array([3,6,9])
# Reduce the array by one dimension
y = np.lcm.reduce(arr) # output 18
GCD
Greatest Common Denominator
Also known as HCF(Highest Common Factor) is the biggest number that is a common factor of both of the numbers
import numpy as np
num1 = 6
num2 = 9
x = np.gcd(num1,num2) # output 3
arr = ([20, 8, 32, 36, 16])
# Reduce the array by one dimension
x = np.gcd.reduce(arr) # output 4
Trigonometric
Numpy provides the ufuncs sin()
, cos()
, tan()
that take values in radians and produce the corresponding sin, cos, tan values
import numpy as np
a = np.sin(np.pi/2) # output 1
arr1 = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])
b = np.sin(arr) # output [1 0.8660254 0.70710678 0.58778525]
arr2 = np.array([90,180,270,360])
# Convert degrees into radians
c = np.deg2rad(arr) # output [1.57079633 3.14159265 4.71238898 6.28318531]
# radians to degrees
d = np.rad2deg(c) # output [90 180 270 360]
# find angles from values of sine, cos, tan inverse(arcsin, arccos, arctan)
x = np.arcsin(1.0) # output 1.57079…
# takes the base and perpendicular values and produces hypotenuse based on pythagoras theorem
base = 3
perp = 4
x = np.hypot(base, perp) # output 5
Hyperbolic
Numpy provides the ufuncs sinh()
, cosh()
, tanh()
that take values in radians and produce the corresponding sinh, cosh, tanh values
import numpy as pd
a = np.sinh(np.pi/2) # output 2.301298…
arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])
b = np.cosh(arr) # output [2.50917848 1.60028686 1.32460909 1.20397209]
# find angle
c = np.arcsinh(1.0) # output 0.881373…
Set
import numpy as np
arr = np.array([1,1,1,2,3,4,5,5,6,7])
arr1 = np.array([1,2,3,4])
arr2 = np.array([3,4,5,6])
# Create set
a = np.unique(arr) # output [1 2 3 4 5 6 7]
# Find union
b = np.union1d(arr1, arr2) # output [1 2 3 4 5 6]
# Find intersection
c = np.intersect1d(arr1, arr2, assume_unique=True) # output [3 4]
# It takes an optional argument assume_unique, which if set to True can speed up computation, it should always be set to True when dealing with sets
# Find difference
d = np.setdiff1d(arr1, arr2, assume_unique) # output [1 2]
# Find Symmetric difference
e = np.setxor1d(arr1, arr2, assume_unique) # output [1 2 5 6]