首页 > 其他分享 >无涯教程-NumPy - 算术运算

无涯教程-NumPy - 算术运算

时间:2023-10-15 21:32:24浏览次数:35  
标签:function 教程 Applying numpy 无涯 np print array NumPy

用于执行算术运算(如add(),subtract(),multipli()和divide())的输入数组必须具有相同的维度或符合数组broadcasting规则。

import numpy as np 
a = np.arange(9, dtype = np.float_).reshape(3,3) 

print 'First array:' 
print a 
print '\n'  

print 'Second array:' 
b = np.array([10,10,10]) 
print b 
print '\n'  

print 'Add the two arrays:' 
print np.add(a,b) 
print '\n'  

print 'Subtract the two arrays:' 
print np.subtract(a,b) 
print '\n'  

print 'Multiply the two arrays:' 
print np.multiply(a,b) 
print '\n'  

print 'Divide the two arrays:' 
print np.divide(a,b)

它将产生以下输出-

First array:
[[ 0. 1. 2.]
 [ 3. 4. 5.]
 [ 6. 7. 8.]]

Second array:
[10 10 10]

Add the two arrays:
[[ 10. 11. 12.]
 [ 13. 14. 15.]
 [ 16. 17. 18.]]

Subtract the two arrays:
[[-10. -9. -8.]
 [ -7. -6. -5.]
 [ -4. -3. -2.]]

Multiply the two arrays:
[[ 0. 10. 20.]
 [ 30. 40. 50.]
 [ 60. 70. 80.]]

Divide the two arrays:
[[ 0. 0.1 0.2]
 [ 0.3 0.4 0.5]
 [ 0.6 0.7 0.8]]

现在让无涯教程讨论NumPy中可用的其他一些重要的算术函数。

numpy.reciprocal()

此函数以元素方式返回参数的倒数,对于绝对值大于1的元素,由于Python处理整数除法的方式,输出始终为0,对于整数0,将发出警告。

import numpy as np 
a = np.array([0.25, 1.33, 1, 0, 100]) 

print 'Our array is:' 
print a 
print '\n'  

print 'After applying reciprocal function:' 
print np.reciprocal(a) 
print '\n'  

b = np.array([100], dtype = int) 
print 'The second array is:' 
print b 
print '\n'  

print 'After applying reciprocal function:' 
print np.reciprocal(b) 

它将产生以下输出-

Our array is:
[   0.25    1.33    1.      0.    100.  ]

After applying reciprocal function:
main.py:9: RuntimeWarning: divide by zero encountered in reciprocal
  print np.reciprocal(a)
[ 4.         0.7518797  1.               inf  0.01     ]

The second array is:
[100]

After applying reciprocal function:
[0]

numpy.power()

此函数将第一个输入数组中的元素视为基数,并将其提升为第二个输入数组中相应元素的幂。

import numpy as np 
a = np.array([10,100,1000]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying power function:' 
print np.power(a,2) 
print '\n'  

print 'Second array:' 
b = np.array([1,2,3]) 
print b 
print '\n'  

print 'Applying power function again:' 
print np.power(a,b)

它将产生以下输出-

Our array is:
[  10  100 1000]

Applying power function:
[    100   10000 1000000]

Second array:
[1 2 3]

Applying power function again:
[        10      10000 1000000000]

numpy.mod()

此函数返回输入数组中相应元素的除法余数。函数 numpy.remainder()也产生相同的输出。

import numpy as np 
a = np.array([10,20,30]) 
b = np.array([3,5,7]) 

print 'First array:' 
print a 
print '\n'  

print 'Second array:' 
print b 
print '\n'  

print 'Applying mod() function:' 
print np.mod(a,b) 
print '\n'  

print 'Applying remainder() function:' 
print np.remainder(a,b) 

它将产生以下输出-

First array:                                                                  
[10 20 30]

Second array:                                                                 
[3 5 7]

Applying mod() function:                                                      
[1 0 2]

Applying remainder() function:                                                
[1 0 2]

以下函数用于对具有复数的数组执行操作。

  • numpy.real()    - 返回复杂数据类型参数的实部。

  • numpy.imag()  -  返回复杂数据类型参数的虚部。

  • numpy.conj()   - 返回复共轭,这是通过更改虚部的符号获得的。

  • numpy.angle() - 返回复杂参数的角度。该函数具有度参数。如果为true,则返回度数中的角度,否则以弧度为单位。

import numpy as np 
a = np.array([-5.6j, 0.2j, 11. , 1+1j]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying real() function:' 
print np.real(a) 
print '\n'  

print 'Applying imag() function:' 
print np.imag(a) 
print '\n'  

print 'Applying conj() function:' 
print np.conj(a) 
print '\n'  

print 'Applying angle() function:' 
print np.angle(a) 
print '\n'  

print 'Applying angle() function again (result in degrees)' 
print np.angle(a, deg = True)

它将产生以下输出-

Our array is:
[ 0.-5.6j 0.+0.2j 11.+0.j 1.+1.j ]

Applying real() function:
[ 0. 0. 11. 1.]

Applying imag() function:
[-5.6 0.2 0. 1. ]

Applying conj() function:
[ 0.+5.6j 0.-0.2j 11.-0.j 1.-1.j ]

Applying angle() function:
[-1.57079633 1.57079633 0. 0.78539816]

Applying angle() function again (result in degrees)
[-90. 90. 0. 45.]

参考链接

https://www.learnfk.com/numpy/numpy-arithmetic-operations.html

标签:function,教程,Applying,numpy,无涯,np,print,array,NumPy
From: https://blog.51cto.com/u_14033984/7873511

相关文章

  • ORBSLAM3 安装及测试教程(Ubuntu20.04)
    1.准备工作1.1安装依赖sudoaptinstallgitcmakegccg++mlocate1.2下载ORBSLAM3源码可以去Github下载源码:https://github.com/UZ-SLAMLab/ORB_SLAM3或者终端内下载:gitclonehttps://github.com/UZ-SLAMLab/ORB_SLAM3.git2.安装Pangolin可以去Github下载源......
  • 无涯教程-NumPy - 数学函数
    可以理解,NumPy包含大量的各种数学运算,NumPy提供标准的三角函数,算术运算功能,处理复数等。三角函数NumPy具有标准三角函数,该函数返回给定角度的弧度的三角比例。importnumpyasnpa=np.array([0,30,45,60,90])print'Sineofdifferentangles:'#通过乘以pi/180转......
  • Creo 5.0 下载与安装教程!!!
    软件介绍:Creo5.0是一款强大的三维建模软件,该软件整合了Pro/Engineer的参数化技术、CoCreate的直接建模技术和ProductView的三维可视化技术的新型CAD设计软件包。PTC推出的全新一代3D计算机辅助设计软件Creo是唯一一款能够帮助您驾驭未来的工具,是目前行业中技术比较领先的CAD软件。......
  • 无涯教程-NumPy - 按位运算符
    以下是NumPy包中可用的按位运算功能。Sr.No.描述1bitwise_and计算数组元素的按位与运算2bitwise_or计算数组元素的按位或运算3invert按位计算NOT4left_shift将二进制表示形式的位向左移动5right_shift将二进制表示形式的位向右移参考链接https://ww......
  • Pycharm 2023版安装教程(附激活码,亲测有效)
    PyCharm是一种PythonIDE(集成开发环境),带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试、版本控制。此外,该IDE提供了一些高级功能,以用于支持Django框架下的专业Web开发。第一步:下载Pycharm安......
  • 无涯教程-NumPy - 数组操作
    NumPy包中提供了一些例程来处理ndarray 对象中的元素。它们可以分为以下类型-Changing维度Sr.No.Shape&Remark1reshape在不更改数据的情况下为数组赋予新的维度2flat数组上的一维迭代器3flatten返回折叠成一维的数组的副本4ravel返回一个连续的扁平数组Tr......
  • 无涯教程-NumPy - 遍历数组
    NumPy包含一个迭代器对象numpy.nditer,这是一个有效的多维迭代器对象,使用它可以遍历数组。使用Python的标准Iterator迭代接口访问数组的每个元素。让无涯教程使用arange()函数创建一个3X4数组,并使用nditer对其进行迭代。示例1importnumpyasnpa=np.arange(0,60,5)a=a......
  • Origin 2022 中文版 下载及安装教程!
    软件介绍:Origin是由OriginLab公司开发的一个科学绘图、数据分析软件,支持在MicrosoftWindows下运行。Origin支持各种各样的2D/3D图形。Origin中的数据分析功能包括统计,信号处理,曲线拟合以及峰值分析。Origin中的曲线拟合是采用基于Levernberg-Marquardt算法(LMA)的非线性最小二乘法拟......
  • 微软带你玩转 Linux —— 发布《如何下载和安装 Linux》教程
    微软近日在技术文档中心上架了一份名为《如何下载和安装Linux》的教程指南,介绍了4种安装Linux的方案,包括:WSL、裸机安装、本地虚拟机安装和云端虚拟机安装。这份教程重点介绍了用户在PC上下载和安装Linux发行版的必要步骤,包括选择合适的Linux发行版本,推荐首选的安装......
  • 无涯教程-NumPy - 数值范围
    在本章中,无涯教程将看到如何从数值范围创建数组。numpy.arange此函数返回一个ndarray对象,该对象包含给定范围内的均匀间隔的值。该函数的格式如下-numpy.arange(start,stop,step,dtype)构造函数采用以下参数。Sr.No.Parameter&描述1start间隔的开始。如果省略,则......