Numpy ufunc如何使用通用函数?代码示例

2021年3月23日15:25:17 发表评论 625 次浏览

通用函数在Numpy中是简单的数学函数。这只是我们在Numpy库中使用的数学函数术语。 Numpy提供了涵盖各种操作的各种通用函数

这些函数包括标准三角函数, 用于算术运算, 处理复数的函数, 统计函数等。通用函数具有以下各种特征:

  • 这些函数在ndarray(N维数组), 即Numpy的数组类。
  • 它执行快速的按元素数组操作。
  • 它支持阵列广播, 类型转换等各种函数。
  • Numpy通用函数是属于numpy.ufunc类的对象。
  • 也可以使用以下命令将Python函数创建为通用函数frompyfunc库函数。
  • 一些函数在数组上使用相应的算术运算符时, 将自动调用它们。例如, 当使用'+'运算符逐元素执行两个数组的加法运算时, 则会在内部调用np.add()。

Numpy中的一些基本通用函数是-

三角函数:

这些函数适用于弧度, 因此需要通过乘以pi / 180将角度转换为弧度。只有这样我们才能调用三角函数。他们将数组作为输入参数。它包括如下函数:

函数 描述
罪恶, COS, 棕褐色 计算角度的正弦, 余弦和正切
arcsin, arccos, arctan 计算反正弦, 余弦和正切
虚伪 计算给定直角三角形的斜边
锡, 科什, 丹 计算双曲正弦, 余弦和正切
arcsinh, arccosh, arctanh 计算反双曲正弦, 余弦和正切
deg2rad 将度转换为弧度
rad2deg 将弧度转换为度
# Python code to demonstrate trignometric function
import numpy as np
  
# create an array of angles
angles = np.array([ 0 , 30 , 45 , 60 , 90 , 180 ]) 
  
# conversion of degree into radians
# using deg2rad function
radians = np.deg2rad(angles)
  
# sine of 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)) 
  
# hypot function demonstration
base = 4
height = 3
print ( 'hypotenuse of right triangle is:' )
print (np.hypot(base, height))

输出如下:

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 hyperbolic:
[ 0.          0.52085606  0.76347126  0.94878485  0.74483916 -0.85086591]

hypotenuse of right triangle is:
5.0

统计函数:

这些函数用于计算均值, 中位数, 方差, 数组元素的最小值。它包括如下函数:

函数 描述
阿敏, 阿玛 返回数组或沿轴的最小值或最大值
点对点 返回数组或沿轴的值范围(最大-最小)
百分位(a, p, 轴) 计算数组或沿指定轴的pth百分位
中位数 计算沿指定轴的数据中位数
意思 计算沿指定轴的数据平均值
性病 计算沿指定轴的数据标准偏差
变种 计算沿指定轴的数据方差
平均 计算沿指定轴的数据平均值
# Python code demonstrate statistical function
import numpy as np
  
# construct a weight array
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))
  
# range of weight i.e. max weight-min 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 ))
   
# mean 
print ( 'Mean weight of the students: ' )
print (np.mean(weight))
  
# median 
print ( 'Median weight of the students: ' )
print (np.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))
  
# average 
print ( 'Average weight of the students: ' )
print (np.average(weight))

输出如下:

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

位旋转函数:

这些函数接受整数值作为输入参数, 并对这些整数的二进制表示形式执行按位运算。它包括如下函数:

函数 描述
按位与 对两个数组元素执行按位和运算
bitwies_or 对两个数组元素执行按位或运算
bitwise_xor 对两个数组元素执行按位异或运算
倒置 对数组元素执行按位求逆
左移 向左移元素的位
向右移 向左移元素的位
# Python code to demonstrate bitwise-function
import numpy as np
  
# construct 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))
  
# left_shift 
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 ))

输出如下:

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]

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: