Numpy 基本除法运算和模运算
基本算术运算符+、-和*隐式关联着通用函数add、subtract和multiply
在数组的除法运算中涉及三个通用函数divide、true_divide和floor_division,以及两个对应的运算符/和//
1. 数组的除法运算
import numpy as np
# divide函数在整数和浮点数除法中均只保留整数部分(python3中的np.divide == np.true_divide)
a = np.array([2,6,5])
b = np.array([1,2,3])
print (np.divide(a,b),np.divide(b,a))
# (array([2, 3, 1]), array([0, 0, 0]))
# true_divide函数与数学中的除法定义更为接近,即返回除法的浮点数结果而不作截断
print (np.true_divide(a,b),np.true_divide(b,a))
# (array([ 2. , 3. , 1.66666667]), array([ 0.5 , 0.33333333, 0.6 ]))
# floor_divide函数总是返回整数结果,相当于先调用divide函数再调用floor函数(floor函数将对浮点数进行向下取整并返回整数)
print (np.floor_divide(a,b),np.floor_divide(b,a))
# [2 3 1] [0 0 0]
c = 3.14 * b
print (np.floor_divide(c,b),np.floor_divide(b,c))
# [ 3. 3. 3.] [ 0. 0. 0.]
# /运算符相当于调用divide函数
print (a/b,b/a)
# (array([2, 3, 1]), array([0, 0, 0]))
# 运算符//对应于floor_divide函数
print (a//b,b//a)
# [2 3 1] [0 0 0]
print (c//b,b//c)
# [ 3. 3. 3.] [ 0. 0. 0.]
2. 模运算
# 计算模数或者余数,可以使用NumPy中的mod、remainder和fmod函数。也可以用%运算符
import numpy as np
# remainder函数逐个返回两个数组中元素相除后的余数
d = np.arange(-4,4)
print (np.remainder(d,2))
# [0 1 0 1 0 1 0 1]
# mod函数与remainder函数的功能完全一致
print (np.mod(d,2))
# [0 1 0 1 0 1 0 1]
# %操作符仅仅是remainder函数的简写(功能一样)
print ( d % 2 )
# [0 1 0 1 0 1 0 1]
# fmod函数处理负数的方式与remainder、mod和%不同。所得余数的正负由被除数决定,与除数的正负无关
print (np.fmod(d,2))
# [ 0 -1 0 -1 0 1 0 1]
Numpy 基本除法运算和模运算的更多相关文章
- Divide two numbers,两数相除求商,不能用乘法,除法,取模运算
问题描述:求商,不能用乘法,除法,取模运算. 算法思路:不能用除法,那只能用减法,但是用减法,超时.可以用位移运算,每次除数左移,相当于2倍. public class DividTwoInteger ...
- java 取模运算% 实则取余 简述 例子 应用在数据库分库分表
java 取模运算% 实则取余 简述 例子 应用在数据库分库分表 取模运算 求模运算与求余运算不同.“模”是“Mod”的音译,模运算多应用于程序编写中. Mod的含义为求余.模运算在数论和程序设计中 ...
- Java中"或"运算与"与"运算快慢的三三两两
先上结论 模运算比与运算慢20%到30% 这是通过实验的方式得到的结论.因为没有大大可以进行明确指导,所以我以最终运行的结果为准.欢迎指正. 测试代码 @Test public void test10 ...
- python负数除法与模运算
1.负数除法: >>> print 45/76>>> print -45/7-7 >>> print 45/-7-7 >>> p ...
- 数论 : 模运算法则(poj 1152)
题目:An Easy Problem! 题意:求给出数的最小进制. 思路:暴力WA: discuss中的idea: 给出数ABCD,若存在n 满足 (A* n^3 +B*n^2+C*n^1+D*n^0 ...
- (二)初识NumPy库(数组的操作和运算)
本章主要介绍的是ndarray数组的操作和运算! 一. ndarray数组的操作: 操作是指对数组的索引和切片.索引是指获取数组中特定位置元素的过程:切片是指获取数组中元素子集的过程. 1.一维数组的 ...
- mysql中的优化, 简单的说了一下垂直分表, 水平分表(有几种模运算),读写分离.
一.mysql中的优化 where语句的优化 1.尽量避免在 where 子句中对字段进行表达式操作select id from uinfo_jifen where jifen/60 > 100 ...
- poj 3980 取模运算
取模运算 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10931 Accepted: 6618 Description ...
- c++ 模运算
在数学里,"模运算"也叫"求余运算",用mod来表示模运算. 对于 a mod b 可以表示为 a = q(商)*b(模数) + r(余数),其中q表示商,b表 ...
随机推荐
- GUI库之认识Tkinter(一)
一.介绍 Tkinter是Python默认的GUI库,我们经常使用的IDLE就是用Tkinter设计出来的,因此我们在使用的时候直接导入Tkinter模块就好了. 1.特点:可移植性.灵活性高 2.构 ...
- 流程控制之if...else
# #如果:男的年龄>49,那么:小哥哥## age_of_boy = 50# if age_of_boy > 49:# print('小哥哥你好')### # 如果:女人的年龄>3 ...
- golang 与 c语言 之间传递指针的规则提案
https://go.googlesource.com/proposal/+/master/design/12416-cgo-pointers.md https://github.com/golang ...
- linux配置基本业务
1.安装一些必备软件 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel ...
- Android 7.0下,拍摄照片报错
对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI.如果一项包含文件 URI 的 intent 离开 ...
- 2019.03.30 Head first
第一节 认识python python.exe -V python 会进入解释器 quit()命令会退出解释器 IDEL,一个python的集成开发环境,能够利用颜色突出语法的编辑器,一个调试工具,P ...
- vue弹窗组件
文件结构 component.vue <template> <div class="_vuedals" v-show="show"> & ...
- ida调试ios应用
收集,整理http://www.cnblogs.com/fply/p/8488842.html 这个文章讲了ios上debugserver相关配置 http://iphonedevwiki.net/i ...
- end to end
深度学习中的end to end是什么意思? 端到端就是输入一个数据进入模型,然后模型直接可以输出你想要的结果,也就是一体性. 简单讲就是,Input--->系统(这里指神经网络)---> ...
- python number
一.number类型转换 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real ...