首页 > 编程语言 > 详细

数组的算数运算

时间:2019-08-23 00:52:38      阅读:73      评论:0      收藏:0      [点我收藏+]

除法

Key_Function

np.divide方法, 返回带小数位的结果

np.true_divide方法, 返回带小数位的结果

np.floor_divide方法, 返回只有整数位的结果

/ 运算符, 返回带小数位的结果

// 运算符, 返回只有整数位的结果

import numpy as np

a = np.array([2, 6, 5])
b = np.array([1, 2, 3])
print(np.divide(b, a))
# [ 0.5         0.33333333  0.6       ]
print(np.divide(a, b))
# [ 2.          3.          1.66666667]

print(np.true_divide(b, a))
# [ 0.5         0.33333333  0.6       ]
print(np.true_divide(a, b))
# [ 2.          3.          1.66666667]

print(np.floor_divide(b, a))    # np.floor_divide总是返回整数部分
# [0 0 0]
print(np.floor_divide(a, b))    # np.floor_divide总是返回整数部分
# [2 3 1]

print(b / a)
# [ 0.5         0.33333333  0.6       ]
print(a / b)
# [ 2.          3.          1.66666667]

print(b // a)   # 对应floor_divide
# [0 0 0]
print(a // b)
# [2 3 1]

 

模运算

模运算就是取余数

Key_Function

np.remainder函数, 逐个返回两个数组中元素相除后的余数, 如果第二个参数为0, 则直接返回0

np.mod函数,  与remainder函数功能完全一致

% 运算符, 是np.remainder的简写

np.fmod函数, 所得余数的正负由被除数决定, 与除数无关  

Code

a = np.arange(-4, 4)
print(a)
# [-4 -3 -2 -1  0  1  2  3]

print(np.remainder(a, 2))   # remainder函数逐个返回两个数组中元素相除后的余数
# [0 1 0 1 0 1 0 1]

print(np.mod(a, 2))     # mod函数与remainder函数功能完全一致
# [0 1 0 1 0 1 0 1]

print(a % 2)
# [0 1 0 1 0 1 0 1]     # % 是remainder的简写

print(np.fmod(a, 2))    # 所得余数的正负由被除数决定, 与除数的正负无关 
# [ 0 -1  0 -1  0  1  0  1]

 

数组的算数运算

原文:https://www.cnblogs.com/draven123/p/11397560.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!