如何在Python中使用 NumPy 创建一个空矩阵?
术语空矩阵没有行也没有列。包含缺失值的矩阵至少有一行和一列,包含零的矩阵也是如此。 Numerical Python ( NumPy )为Python中的数值数组和矩阵运算提供了大量有用的特性和函数。如果你想在 NumPy 的帮助下创建一个空矩阵。我们可以使用一个函数:
- numpy.empty
- numpy.zeros
1. numpy.empty :它返回给定形状和类型的新数组,无需初始化条目。
Syntax : numpy.empty(shape, dtype=float, order=’C’)
Parameters:
- shape :int or tuple of int i.e shape of the array (5,6) or 5.
- dtype data-type, optional i.e desired output data-type for the array, e.g, numpy.int8. Default isnumpy.float64.
- order{‘C’, ‘F’}, optional, default: ‘C’ i.e whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
让我们从 NumPy 中的空函数开始,考虑一个例子,你想创建一个 5 x 5 的空矩阵
示例 1:创建一个 5 列 0 行的空矩阵:
Python3
import numpy as np
x = np.empty((0, 5))
print('The value is :', x)
# if we check the matrix dimensions
# using shape:
print('The shape of matrix is :', x.shape)
# by default the matrix type is float64
print('The type of matrix is :', x.dtype)
Python3
# import the library
import numpy as np
# Here 4 is the number of rows and 2
# is the number of columns
y = np.empty((4, 2))
# print the matrix
print('The matrix is : \n', y)
# print the matrix consist of 25 random numbers
z = np.empty(25)
# print the matrix
print('The matrix with 25 random values:', z)
Python3
import numpy as np
x = np.zeros((7, 5))
# print the matrix
print('The matrix is : \n', x)
# check the type of matrix
x.dtype
输出:
The value is : []
The shape of matrix is : (0, 5)
The type of matrix is : float64
在这里,矩阵由 0 行和 5 列组成,这就是结果是“[]”的原因。让我们再举一个 NumPy 中空函数的例子,考虑一个例子,你想用一些随机数创建一个 4 x 2 的空矩阵。
示例 2:使用预期的维度/大小初始化一个空数组:
蟒蛇3
# import the library
import numpy as np
# Here 4 is the number of rows and 2
# is the number of columns
y = np.empty((4, 2))
# print the matrix
print('The matrix is : \n', y)
# print the matrix consist of 25 random numbers
z = np.empty(25)
# print the matrix
print('The matrix with 25 random values:', z)
输出 :
The matrix is :
[[1.41200958e-316 3.99539825e-306]
[3.38460865e+125 1.06264595e+248]
[1.33360465e+241 6.76067859e-311]
[1.80734135e+185 6.47273003e+170]]
The matrix with 25 random values: [1.28430744e-316 8.00386346e-322 0.00000000e+000 0.00000000e+000
0.00000000e+000 1.16095484e-028 5.28595592e-085 1.04316726e-076
1.75300433e+243 3.15476290e+180 2.45128397e+198 9.25608172e+135
4.73517493e-120 2.16209963e+233 3.99255547e+252 1.03819288e-028
2.16209973e+233 7.35874688e+223 2.34783498e+251 4.52287158e+217
8.78424170e+247 4.62381317e+252 1.47278596e+179 9.08367237e+223
1.16466228e-028]
在这里,我们定义行数和列数,以便用随机数填充矩阵。
2. numpy.zeros :它返回一个给定形状和类型的新数组,用零填充。
Syntax : numpy.zeros(shape, dtype=float, order=’C’)
Parameters:
- shape : int or tuple of int i.e shape of the array (5,6) or 5.
- dtype data-type, optional i.e desired output data-type for the array, e.g, numpy.int8. Default is numpy.float64.
- order{‘C’, ‘F’}, optional, default: ‘C’ i.e whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
让我们开始使用 NumPy 中的 zeros函数,考虑一个您想要创建一个带有零的矩阵的示例。
示例:要创建 7 列 5 行的零矩阵:
蟒蛇3
import numpy as np
x = np.zeros((7, 5))
# print the matrix
print('The matrix is : \n', x)
# check the type of matrix
x.dtype
输出 :
The matrix is :
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
dtype('float64')