如何使用 NumPy 在Python中创建常量矩阵?
矩阵表示按行和列的顺序排列的数字集合。有必要用圆括号或方括号将矩阵的元素括起来。常量矩阵是一种元素相同的矩阵,即元素不会改变而与任何索引值无关,因此充当常量。
例子:
M = [[ x, x, x ]
[ x ,x ,x]
[ x, x, x]]
Here M is the constant matrix and x is the constant element.
Below are some examples of Constant Matrix:
A = [[ 5 , 5]
[ 5, 5]]
B = [[ 12, 12, 12, 12, 12, 12]]
numpy模块中有多种方法,可用于创建常量矩阵,例如 numpy.full()、numpy.ones() 和 numpy.zeroes()。
使用numpy.full()方法
句法:
numpy.full(shape, fill_value, dtype = None, order = ‘C’)
Parameters:
- shape: Number of rows
- order: C_contiguous or F_contiguous
- dtype: [optional, float(by Default)] Data type of returned array.
- fill_value: [bool, optional] Value to fill in the array.
Returns: ndarray of a given constant having given shape, order and datatype.
示例 1:
在这里,我们将创建一个大小为(2,2) (行 = 2,列 = 2)的常量矩阵,常量值为6.3
Python3
# import required module
import numpy as np
# use full() with a
# constant value of 6.3
array = np.full((2, 2), 6.3)
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use full() with a
# constant value of 60
array = np.full((4, 3), 60)
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use ones()
array = np.ones((2,2))
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use ones() with integer constant
array = np.ones((2, 2), dtype=np.uint8)
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use ones() with integer constant
array = np.ones((5), dtype=np.uint8)
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use zeroes()
array = np.zeros((2,2))
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use zeroes() with integer constant
array = np.zeros((2,2), dtype=np.uint8)
# display matrix
print(array)
Python3
# import required module
import numpy as np
# use zeroes() with integer constant
array = np.zeros((5), dtype=np.uint8)
# display matrix
print(array)
输出:
[[6.3 6.3]
[6.3 6.3]]
示例 2:
与上面显示的示例类似的示例
蟒蛇3
# import required module
import numpy as np
# use full() with a
# constant value of 60
array = np.full((4, 3), 60)
# display matrix
print(array)
输出:
[[60 60 60]
[60 60 60]
[60 60 60]
[60 60 60]]
使用 numpy.ones() 方法
句法:
numpy.ones(shape, dtype = None, order = ‘C’)
Parameters:
- shape: integer or sequence of integers
- order: C_contiguous or F_contiguous
- dtype: Data type of returned array.
Returns: ndarray of ones having given shape, order and datatype.
示例 1:
现在,假设我们要打印一个仅由 1(1) 组成的矩阵。
蟒蛇3
# import required module
import numpy as np
# use ones()
array = np.ones((2,2))
# display matrix
print(array)
输出:
[[1. 1.]
[1. 1.]]
默认情况下,数据类型是浮点数,因此所有数字都写为1。对上述代码的更改。现在,我们希望数据类型为整数。
蟒蛇3
# import required module
import numpy as np
# use ones() with integer constant
array = np.ones((2, 2), dtype=np.uint8)
# display matrix
print(array)
输出:
[[1 1]
[1 1]]
注意最后两个输出的变化,其中一个显示为1。另一个只显示1 ,这意味着我们在第二个输出中将数据类型转换为整数。 uint8代表一个无符号的 8 位整数,可以表示 0 到 255 之间的值。
示例 2:
这里我们创建了一个只有 1 的一维矩阵。
蟒蛇3
# import required module
import numpy as np
# use ones() with integer constant
array = np.ones((5), dtype=np.uint8)
# display matrix
print(array)
输出:
[1 1 1 1 1]
使用 numpy.zeroes() 方法
句法:
numpy.zeros(shape, dtype = None, order = ‘C’)
Parameters:
- shape: integer or sequence of integers
- order: C_contiguous or F_contiguous
- dtype: Data type of returned array.
Returns: ndarray of zeros having given shape, order and datatype.
示例 1:
现在我们制作了一个由 1 组成的矩阵,让我们为 0 制作 1。
蟒蛇3
# import required module
import numpy as np
# use zeroes()
array = np.zeros((2,2))
# display matrix
print(array)
输出:
[[0. 0.]
[0. 0.]]
要将其更改为整数类型,
蟒蛇3
# import required module
import numpy as np
# use zeroes() with integer constant
array = np.zeros((2,2), dtype=np.uint8)
# display matrix
print(array)
输出:
[[0 0]
[0 0]]
示例 2:
这是创建一个恒定的一维零矩阵的另一个示例。
蟒蛇3
# import required module
import numpy as np
# use zeroes() with integer constant
array = np.zeros((5), dtype=np.uint8)
# display matrix
print(array)
输出:
[0 0 0 0 0]