可以通过以下任何数组创建或使用低级ndarray构造函数来构造新的 ndarray对象 。
它创建指定维度和dtype的未初始化数组。它使用以下构造函数-
numpy.empty(shape, dtype=float, order='C')
构造函数采用以下参数。
Sr.No. | Parameter & 描述 |
---|---|
1 | shape 链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-array-creation-routines.html 来源:LearnFk无涯教程网 int或int元组中的空数组的维度 |
2 | Dtype 所需的输出数据类型。可选的 |
3 | order " C"表示C样式的行主数组," F"表示FORTRAN样式的行主数组 |
以下代码显示了一个空数组的示例。
import numpy as np x = np.empty([3,2], dtype = int) print x
输出如下-
[[22649312 1701344351] [1818321759 1885959276] [16779776 156368896]]
注意-数组中的元素显示随机值,因为它们没有初始化。
返回指定大小的新数组,并用零填充。
numpy.zeros(shape, dtype=float, order='C')
构造函数采用以下参数。
Sr.No. | Parameter & 描述 |
---|---|
1 | shape 链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-array-creation-routines.html 来源:LearnFk无涯教程网 int或int序列中的空数组的维度 |
2 | Dtype 所需的输出数据类型。可选的 |
3 | order " C"表示C样式的行主数组," F"表示FORTRAN样式的行主数组 |
示例1
# 五个零的数组。默认 dtype 是 float import numpy as np x = np.zeros(5) print x
输出如下-
[ 0. 0. 0. 0. 0.]
示例2
import numpy as np x = np.zeros((5,), dtype = np.int) print x
现在,输出将如下所示:
[0 0 0 0 0]
示例3
# 自定义类型 import numpy as np x = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')]) print x
它应该产生以下输出-
[[(0,0)(0,0)] [(0,0)(0,0)]]
返回指定大小和类型的新数组,并填充为1。
numpy.ones(shape, dtype=None, order='C')
构造函数采用以下参数。
Sr.No. | Parameter & 描述 |
---|---|
1 | shape 链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-array-creation-routines.html 来源:LearnFk无涯教程网 int或int元组中的空数组的维度 |
2 | Dtype 所需的输出数据类型。可选的 |
3 | order " C"表示C样式的行主数组," F"表示FORTRAN样式的行主数组 |
示例1
# 五个一的数组。默认 dtype 是 float import numpy as np x = np.ones(5) print x
输出如下-
[ 1. 1. 1. 1. 1.]
示例2
import numpy as np x = np.ones([2,2], dtype = int) print x
现在,输出将如下所示:
[[1 1] [1 1]]
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)