Change language

Numpy Ufunc | Versatile functions

These functions include standard trigonometric functions, functions for arithmetic operations, complex number processing, statistical functions, etc. Generic functions have different characteristics, which are as follows:

  • These functions work with ndarray (N-dimensional array), that is, with the Numpy class.
  • It does fast element-wise operations on arrays.
  • It supports various functions such like array translation, casting, etc.
  • Numpy, generic functions — these are objects belonging to the numpy.ufunc class.
  • Python functions can also be created as a generic function using the frompyfunc library function.
  • Some ufuncs are called automatically when the corresponding arithmetic operator is used in arrays. For example, when adding two arrays is done element by element using the + operator, then np.add () is called internally.

Some of the main generic functions in Numpy are:

Trigonometric functions:

These functions work with radians, so it is necessary to convert angles to radians by multiplying by pi / 180. Only then can we call trigonometric functions. They take an array as input arguments. Includes features such as

Function Description
sin, cos, tan compute sine, cosine and tangent of angles
arcsin, arccos, arctan calculate inverse sine, cosine and tangent
hypot calculate hypotenuse of given right triangle
sinh, cosh, tanh compute hyperbolic sine, cosine and tangent
arcsinh, arccosh, arctanh compute inverse hyperbolic sine, cosine and tangent
deg2rad convert degree into radians
rad2deg convert radians into degree

# Python code to demonstrate trigonometric function

import numpy as np

 
# create an array of corners

angles = np.array ([ 0 , 30 , 45 , 60 , 90 , 180 ]) 

 
# convert powers to radians
# using deg2rad function

radians = np.deg2rad (angles)

 
# sine angles

print ( ’ Sine of angles in the array: ’ )

sine_value = np.sin (radians )

print (np.sin (radians))

 
# inverse sine of sine values ​​

print ( ’Inverse Sine of sine values:’ )

print (np.rad2deg (np.arcsin (sine_value)))

 
# hyperbolic sine of angles

print ( ’Sine hyperbolic of angles in the array:’ )

sineh_value = np.sinh (radians)

print (np.sinh (radians))

 
# inverse sine hyperbolic

print ( ’Inverse Sine hyperbolic:’ )

print (np.sin (sineh_value)) 

  
# hypothesis function demonstration

base = 4

height = 3

print ( ’hypotenuse of right triangle is:’ )

print (np.hypot ( base, height))

Exit:

 Sine of angles in the array: [0.00000000e + 00 5.00000000e-01 7.07106781e-01 8.66025404e-01 1.00000000e + 00 1.22464680e-16] Inverse Sine of sine values: [0.00000000e + 00 3.00000000e + 01 4.50000000e + 01 6.00000000e + 01 9.00000000e + 01 7.01670930e-15] Sine hyperbolic of angles in the array: [0. 0.54785347 0.86867096 1.24936705 2.3012989 11.54873936] Inverse Sine hyperbolicten: [0. 0.52085606 0.76347124 0.9848655 right triangle is: 5.0 

Statistical functions:

These functions are used to calculate the mean , mean -median-mode-in-python-without-libraries/">median, variance, minimum of array elements. Includes features such as

 
Function Description
amin, amax returns minimum or maximum of an array or along an axis
ptp returns range of values ​​(maximum-minimum) of an array or along an axis
percentile (a, p, axis) calculate pth percentile of array or along specified axis
mean -median-mode-in-python-without-libraries/">median compute mean -median-mode-in-python-without-libraries/">median of data along specified axis
mean compute mean of data along specified axis
std compute standard deviation of data along specified axis
var compute variance of data along specified axis
average compute average of data along specified axis

# Python code demonstrates an aggregate function

import numpy as np

 
# build an array of weights

weight = np. array ([ 50.7 , 52.5 , 50 , 58 , 55.63 , 73.25 , 49.5 , 45 ])

  
# minimum and maximum

print ( ’Minimum and maximum weight of the students:’ )

print (np.amin (weight), np.amax (weight))

 
# weight range, i.e. maximum weight, minimum weight

print ( ’Range of the weight of the students:’ )

print (np.ptp (weight))

 
# percentile

print ( ’Weight below which 70% student fall: ’ )

print (np.percentile (weight, 70 ))

 
# greedy

print ( ’Mean weight of the students:’ )

print (np. mean (weight))

 
# mean -median-mode-in-python-without-libraries/">median

print ( ’Median weight of the students: ’ )

print (np.mean -median-mode-in-python-without-libraries/">median (weight))

 
# standard deviation

print ( ’Standard deviation of weight of the students: ’ )

print (np .std (weight))

 
# variance

print ( ’Variance of weight of the students:’ )

print (np.var (weight))

 
# medium

print ( ’ Average weight of the students: ’ )

print (np.average (weight))

Exit:

 Minimum and maximum weight of the students: 45.0 73.25 Range of the weight of the students: 28.25 Weight below which 70% student fall: 55.317 Mean weight of the students: 54.3225 Median weight of the students: 51.6 Standard deviation of weight of the students: 8.05277397857 Variance of weight of the students: 64.84716875 Average weight of the students: 54.3225 

Bit-tiddling functions:

These functions take integer values ​​as input arguments and you perform bitwise operations on the binary representations of these integers. Includes features such as

Function Description
bitwise_and performs bitwise and operation on two array elements
bitwies_or performs bitwise or operation on two array elements
bitwise_xor performs bitwise xor operation on two array elements
invert performs bitwise inversion of an array elements
left_shift shift the bits of elements to left
right_shift shift the bits of elements to left

# Python code to demonstrate the bitwise function

import numpy as np

 
# build an array of even and odd numbers

even = np.array ([ 0 , 2 , 4 , 6 , 8 , 16 , 32 ])

odd = np.array ([ 1 , 3 , 5 , 7 , 9 , 17 , 33 ])

 
# bitwise_and

print ( ’bitwise_and of two arrays:’ )

print (np.bitwise_and (even, odd))

 
# bitwise_or

print ( ’bitwise_or of two arrays:’ )

print (np.bitwise_or (even, odd))

 
# bitwise_xor

 

print ( ’bitwise_xor of two arrays:’ )

print (np.bitwise_xor (even, odd))

 
# invert or not

print ( ’inversion of even no. array: ’ )

print (np .invert (even))

 
# Shift left

print ( ’left_shift of even no. array:’ )

print (np.left_shift (even , 1 ))

 
# right_shift

print ( ’right_shift of even no. array:’ )

print (np.right_shift (even, 1 ))

Exit:

 
 bitwise_and of two arrays: [0 2 4 6 8 16 32] bitwise_or of two arrays: [1 3 5 7 9 17 33] bitwise_xor of two arrays: [1 1 1 1 1 1 1] inversion of even no. array: [-1 -3 -5 -7 -9 -17 -33] left_shift of even no. array: [0 4 8 12 16 32 64] right_shift of even no. array: [0 1 2 3 4 8 16] 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically