Computation on NumPy arrays can be very fast, or it can be very slow. Using vectorized operations, fast computations is possible and it is implemented by using NumPy's universial functions (ufuncs).
Computations on Arrays
•
Computation on NumPy arrays can be very fast, or it can be very slow. Using
vectorized operations, fast computations is possible and it is implemented by
using NumPy's universial functions (ufuncs).
• A
universal function (ufuncs) is a function that operates on ndarrays in an
element-by- element fashion, supporting array broadcasting, type casting, and
several other standard features. The ufunc is a "vectorized" wrapper
for a function that takes a fixed number of specific inputs and produces a
fixed number of specific outputs.
•
Functions that work on both scalars and arrays are known as ufuncs. For arrays,
ufuncs apply the function in an element-wise fashion. Use of ufuncs is an
esssential aspect of vectorization and typically much more computtionally
efficient than using an explicit loop over each element.
NumPy's Ufuncs :
• Ufuncs
are of two types: unary ufuncs and binary ufuncs.
• Unary
ufuncs operate on a single input and binary ufuncs, which operate on two
inputs.
• Arithmetic operators implemented in NumPy is
as follows:
• Example of Arithmetic Operators: Python Code
# Taking
input
num1 =
input('Enter first number:')
num2 =
input('Enter second number:')
#
Addition
sum =
float(num1) + float(num2)
#
Subtraction
min
=float(num1) - float(num2)
#
Multiplication
mul =
float(num1)* float(num2)
#Division
div =
float(num1) / float(num2)
#Modulus
mod =
float(num1) % float(num2)
#Exponentiation
exp
=float(num1)**float(num2)
#Floor
Division
floordiv
= float(num1) // float(num2)
print("The
sum of {0} and {1} is {2}'.format(num1, num2, sum))
print("The
subtraction of {0} and {1} is {2}'.format(num1, num2, min))
print("The
multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
print("The
division of {0} and {1} is {2}'.format(num1, num2, div))
print("The
modulus of {0} and {1} is {2}'.format(num1, num2, mod))
print("The
exponentiation of {0} and {1} is {2}'.format(num1, num2, exp))
print("The
floor division between {0} and {1} is {2}'.format(num1, num2,floordiv))
Absolute value :
• NumPy
understands Python's built-in arithmetic operators, it also understands
Python's built-in absolute value function. The abs() function returns the
absolute magnitude or value of input passed to it as an argument. It returns
the actual value of input without taking the sign into consideration.
• The
abs() function accepts only a single arguement that has to be a number and it
returns the absolute magnitude of the number. If the input is of type integer
or float, the abs() function returns the absolute magnitude/value. If the input
is a complex number, the abs() function returns only the magnitude portion of
the number.
Syntax: abs(number)
Where
the number can be of integer type, floating point type or a complex number.
• Example:
num -25.79
print("Absolute
value:", abs(num))
• Output:
Absolute
value : 25.79
Trigonometric functions:
• The
numpy package provides trigonometric functions which can be used to calculate
trigonometric ratios for a given angle in radians.
Example:
importnumpy
as np
Arr =
np.array([0, 30, 60, 90])
#converting
the angles in radians
Arr =
Arr*np.pi/180
print("\nThe
sin value of angles:")
print(np.sin(Arr))
print("\nThe
cos value of angles:")
print(np.cos(Arr))
print("\nThe
tan value of angles:")
print(np.tan(Arr))
Foundation of Data Science: Unit IV: Python Libraries for Data Wrangling : Tag: : Python Libraries for Data Wrangling - Computations on Arrays
Foundation of Data Science
CS3352 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation