使用NumPy进行模运算的元素级余数计算
要返回使用模运算的除法的元素级余数,请在Python NumPy中使用**numpy.mod()**方法。这里,第一个参数是被除数数组。第二个参数是除数数组。
计算相对于floor_divide函数的余数。它等同于Python模运算符“x1 % x2”,并且与除数x2具有相同的符号。等效于np.remainder的MATLAB函数是mod。
步骤
首先,导入所需的库:
import numpy as np
创建一个数组。这是被除数数组:
arr = np.array([3, 9, 14, 22, 76, 81, 91])
显示数组:
print("Array...
", arr)获取数组的类型:
print("
Our Array type...
", arr.dtype)
获取数组的维度:
print("
Our Array Dimension...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
除数数组:
divisor = np.array([5, 7, 3, 2, 2, 7, 7])
要返回使用模运算的除法的元素级余数,请使用numpy.mod()方法。这里,第一个参数是被除数数组。第二个参数是除数数组。
print("
Result...
",np.mod(arr, divisor))
示例
import numpy as np
# Create an array
# This is the dividend array
arr = np.array([3, 9, 14, 22, 76, 81, 91])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimension...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Divisor array
divisor = np.array([5, 7, 3, 2, 2, 7, 7])
# To return the element-wise remainder of division with mod operation, use the numpy.mod() method in Python Numpy
# Here, the 1st parameter is the Dividend array
# The 2nd parameter is the Divisor array
print("
Result...
",np.mod(arr, divisor))输出
Array... [ 3 9 14 22 76 81 91] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (7,) Result... [3 2 2 0 0 4 0]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP