将输入解释为矩阵并在 NumPy 中显示不同类型的输出
要将输入解释为矩阵,请在 Python NumPy 中使用 **numpy.asmatrix()** 方法。dtype 参数用于设置输出数组的类型。与矩阵不同,如果输入已经是矩阵或 ndarray,则 asmatrix 不会创建副本。等价于 matrix(data, copy=False)。
NumPy 提供了全面的数学函数、随机数生成器、线性代数例程、傅里叶变换等等。它支持各种硬件和计算平台,并且可以很好地与分布式、GPU 和稀疏数组库配合使用。
步骤
首先,导入所需的库 -
import numpy as np
创建一个二维数组 -
arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])
显示我们的数组 -
print("Array...
",arr)获取数据类型 -
print("
Array datatype...
",arr.dtype)获取数组的维度 -
print("
Array Dimensions...
",arr.ndim)获取数组的形状 -
print("
Our Array Shape...
",arr.shape)获取数组的元素数量 -
print("
Elements in the Array...
",arr.size)要将输入解释为矩阵,请使用 numpy.asmatrix() 方法。dtype 参数用于设置输出数组的类型 -
res = np.asmatrix(arr, dtype = float)
print("
Result...
",res)示例
import numpy as np
# Create a 2d array
arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69], [69, 80, 80, 99]])
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)
# To Interpret the input as a matrix, use the numpy.asmatrix() method in Python Numpy
# The dtype parameter is used to set the type of the output array
res = np.asmatrix(arr, dtype = float)
print("
Result...
",res)输出
Array... [[36 36 78 88] [92 81 98 45] [22 67 54 69] [69 80 80 99]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16 Result... [[36. 36. 78. 88.] [92. 81. 98. 45.] [22. 67. 54. 69.] [69. 80. 80. 99.]]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP