NumPy算数运算
NumPy - 算数运算
用于执行算术运算(如add(),subtract(),multiply()和divide())的输入数组必须具有相同的形状或符合数组广播规则。
示例
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print '第一个数组:'
print a
print '\n'
print '第二个数组:'
b = np.array([10,10,10])
print b
print '\n'
print '两个数组相加:'
print np.add(a,b)
print '\n'
print '两个数组相减:'
print np.subtract(a,b)
print '\n'
print '两个数组相乘:'
print np.multiply(a,b)
print '\n'
print '两个数组相除:'
print np.divide(a,b)
输出如下:
第一个数组:
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]
第二个数组:
[10 10 10]
两个数组相加:
[[ 10. 11. 12.]
[ 13. 14. 15.]
[ 16. 17. 18.]]
两个数组相减:
[[-10. -9. -8.]
[ -7. -6. -5.]
[ -4. -3. -2.]]
两个数组相乘:
[[ 0. 10. 20.]
[ 30. 40. 50.]
[ 60. 70. 80.]]
两个数组相除:
[[ 0. 0.1 0.2]
[ 0.3 0.4 0.5]
[ 0.6 0.7 0.8]]
让我们现在来讨论 NumPy 中提供的一些其他重要的算术函数。
numpy.reciprocal()
此函数返回参数逐元素的倒数,。 由于 Python 处理整数除法的方式,对于绝对值大于 1 的整数元素,结果始终为 0, 对于整数 0,则发出溢出警告。
示例
import numpy as np
a = np.array([0.25, 1.33, 1, 0, 100])
print '我们的数组是:'
print a
print '\n'
print '调用 reciprocal 函数:'
print np.reciprocal(a)
print '\n'
b = np.array([100], dtype = int)
print '第二个数组:'
print b
print '\n'
print '调用 reciprocal 函数:'
print np.reciprocal(b)
输出如下:
我们的数组是:
[ 0.25 1.33 1. 0. 100. ]
调用 reciprocal 函数:
main.py:9: RuntimeWarning: divide by zero encountered in reciprocal
print np.reciprocal(a)
[ 4. 0.7518797 1. inf 0.01 ]
第二个数组:
[100]
调用 reciprocal 函数:
[0]
numpy.power()
此函数将第一个输入数组中的元素作为底数,计算它与第二个输入数组中相应元素的幂。
import numpy as np
a = np.array([10,100,1000])
print '我们的数组是;'
print a
print '\n'
print '调用 power 函数:'
print np.power(a,2)
print '\n'
print '第二个数组:'
b = np.array([1,2,3])
print b
print '\n'
print '再次调用 power 函数:'
print np.power(a,b)
输出如下:
我们的数组是;
[ 10 100 1000]
调用 power 函数:
[ 100 10000 1000000]
第二个数组:
[1 2 3]
再次调用 power 函数:
[ 10 10000 1000000000]
numpy.mod()
此函数返回输入数组中相应元素的除法余数。 函数numpy.remainder()也产生相同的结果。
import numpy as np
a = np.array([10,20,30])
b = np.array([3,5,7])
print '第一个数组:'
print a
print '\n'
print '第二个数组:'
print b
print '\n'
print '调用 mod() 函数:'
print np.mod(a,b)
print '\n'
print '调用 remainder() 函数:'
print np.remainder(a,b)
输出如下:
第一个数组:
[10 20 30]
第二个数组:
[3 5 7]
调用 mod() 函数:
[1 0 2]
调用 remainder() 函数:
[1 0 2]
以下函数用于对含有复数的数组执行操作。
numpy.real()返回复数类型参数的实部。numpy.imag()返回复数类型参数的虚部。numpy.conj()返回通过改变虚部的符号而获得的共轭复数。numpy.angle()返回复数参数的角度。 函数的参数是degree。 如果为true,返回的角度以角度制来表示,否则为以弧度制来表示。
import numpy as np
a = np.array([-5.6j, 0.2j, 11. , 1+1j])
print '我们的数组是:'
print a
print '\n'
print '调用 real() 函数:'
print np.real(a)
print '\n'
print '调用 imag() 函数:'
print np.imag(a)
print '\n'
print '调用 conj() 函数:'
print np.conj(a)
print '\n'
print '调用 angle() 函数:'
print np.angle(a)
print '\n'
print '再次调用 angle() 函数(以角度制返回):'
print np.angle(a, deg = True)
输出如下:
我们的数组是:
[ 0.-5.6j 0.+0.2j 11.+0.j 1.+1.j ]
调用 real() 函数:
[ 0. 0. 11. 1.]
调用 imag() 函数:
[-5.6 0.2 0. 1. ]
调用 conj() 函数:
[ 0.+5.6j 0.-0.2j 11.-0.j 1.-1.j ]
调用 angle() 函数:
[-1.57079633 1.57079633 0. 0.78539816]
再次调用 angle() 函数(以角度制返回):
[-90. 90. 0. 45.]
NumPy算数运算的更多相关文章
- 数组名取地址所算数运算应注意的"trap"
数组名取地址所算数运算应注意的"trap" 直接看代码: #include <stdio.h> int main() { int array[5]; printf(&q ...
- 初学 Java Script (算数运算及逻辑术语)
在JS中常用的算数运算符与其他编程类语言类似,逻辑术语也近乎相同. 一.常用算数运算符 1.基本算数运算符 赋值运算符:= : 加号:+ : 减号: - : 乘号: * : 除号: / : 求余: % ...
- python魔法方法-单目运算及一般算数运算
在比较的魔法方法中,我们讨论了魔法方法其实就是重载了操作符,例如>.<.==等.而这里,我们继续讨论有关于数值的魔法方法. 1.单目运算符或单目运算函数 __pos__(self) 实现一 ...
- NumPy 位运算
NumPy 位运算 NumPy "bitwise_" 开头的函数是位运算函数. NumPy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操作 b ...
- Shell学习笔记——算数运算与条件测试
算数运算 1. 使用let命令 #!/sbin/bash var1=2 var2=3 let sum=var1+var2 echo $sum 使用let命令式,变量前不需要加$号 只用于整数运算,不适 ...
- java的数组index[]方括号内是可以进行算数运算的
java的数组index[]方括号内饰可以进行算数运算的 如: String[] stringArray = testString.split("\\."); System.out ...
- linux bash编程之算数运算和测试类型(第二篇)
写在最前边:在bash中数据类型有两种,分别是数值型和字符型.其中字符型是默认的. 1.算数运算 · 运算符 · 语法 1.1.运算符:+.-.*./.%.** 注意:有些时候 *(乘号)需要转义 1 ...
- 10、numpy——位运算
NumPy 位运算 NumPy "bitwise_" 开头的函数是位运算函数. NumPy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操作 b ...
- pandas读书笔记 算数运算和数据对齐
pandas最重要的一个功能是,它可以对不同索引的对象进行算数运算.在对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集. Series s1=Series([,3.4,1.5],ind ...
随机推荐
- spring + quartz 定时
springConfig配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- 并发编程5 操作系统&进程
一.今日大纲 1.multiprocessing模块简单应用 2.for循环创建进程 3.进程传参方式和创建方式2 4.join方法 5.操作系统基础 二.今日内容 (1)操作系统简单介绍 多道技术: ...
- 定时备份DB和WEB文件
sql.sh #!/bin/bash ##备份数据库: 每4小时 date2=`date "+%Y-%m-%d---%H.%M.%S"` /alidata/server/mysql ...
- MAC OSX--docker
http://www.cnblogs.com/yjmyzz/p/docker-install-tutorial.html http://www.cnblogs.com/yjmyzz/p/docker- ...
- Java栈(Stack)和堆(Heap)
In the following code public void Method1() { int i = 4; int y = 2; class1 cls1 = new class1(); } He ...
- 吴超老师课程---ZooKeeper介绍和集群安装
1.ZooKeeper 1.1 zk可以用来保证数据在zk集群之间的数据的事务性一致.2.如何搭建ZooKeeper服务器集群 2.1 zk服务器集群规模不小于3个节点,要求各服务器之间系 ...
- 转:用unix socket加速php-fpm、mysql、redis的连接
图虫的服务器长期是单机运行.估计除了mysql之外,php-fpm和redis还可以在单机上共存很长时间.(多说服务器早就达成了单机每日2000万+动态请求,所以我对单机搞定图虫的大流量非常乐观) 如 ...
- Appium中的logger
原文地址http://blog.csdn.net/itfootball/article/details/45395901 appium中的log输出量很大,我们分析问题的时候会依赖于这些log,但是你 ...
- yii根据id查询一条数据
model层 public function selectone($ag_id=''){ return $this->findBySql("SELECT * FROM agency w ...
- 本地连不上远程mysql数据库(2)
Host is not allowed to connect to this MySQL server解决方法 今天在ubuntu上面装完MySQL,却发现在本地登录可以,但是远程登录却报错Host ...