返回一个用零填充的指定形状的新数组,并在NumPy中设置所需的输出
要在Python NumPy中返回一个具有给定形状和类型的用零填充的新数组,请使用**np.zeros()**方法。第一个参数设置数组的形状。第二个参数是数组所需的数据类型。
dtype是数组所需的数据类型,例如numpy.int8。默认为numpy.float64。order参数建议是否以行优先(C风格)或列优先(Fortran风格)的顺序在内存中存储多维数据。like参数是参考对象,允许创建非NumPy数组的数组。如果作为like传入的类数组支持__array_function__协议,则结果将由其定义。在这种情况下,它确保创建一个与通过此参数传入的类数组兼容的数组对象。
步骤
首先,导入所需的库:
import numpy as np
要在Python NumPy中返回一个具有给定形状和类型的用零填充的新数组,请使用np.zeros()方法。第二个参数是数组所需的数据类型:
arr = np.zeros((6,6), dtype = int)
显示数组:
print("Array...
",arr)获取数据类型:
print("
Array datatype...
",arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
获取数组的元素个数:
print("
Number of elements in the Array...
",arr.size)示例
import numpy as np
import numpy.ma as ma
# To return a new array of given shape and type, filled with zeros, use the np.zeros() method in Python Numpy
# The 1st parameter sets the shape of the array
# The 2nd parameter is the desired data-type for the array
arr = np.zeros((6,6), dtype = int)
# Displaying the 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("
Number of elements in the Array...
",arr.size)输出
Array... [[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 0]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (6, 6) Number of elements in the Array... 36
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP