zoukankan      html  css  js  c++  java
  • NumPy数组创建例程

    NumPy - 数组创建例程

    新的ndarray对象可以通过任何下列数组创建例程或使用低级ndarray构造函数构造。

    numpy.empty

    它创建指定形状和dtype的未初始化数组。 它使用以下构造函数:

    numpy.empty(shape, dtype = float, order = 'C')
    

    构造器接受下列参数:

    序号参数及描述
    1. Shape 空数组的形状,整数或整数元组
    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

    返回特定大小,以 0 填充的新数组。

    numpy.zeros(shape, dtype = float, order = 'C')
    

    构造器接受下列参数:

    序号参数及描述
    1. Shape 空数组的形状,整数或整数元组
    2. Dtype 所需的输出数组类型,可选
    3. Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

    示例 1

    # 含有 5 个 0 的数组,默认类型为 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)]]
    

    numpy.ones

    返回特定大小,以 1 填充的新数组。

    numpy.ones(shape, dtype = None, order = 'C')
    

    构造器接受下列参数:

    序号参数及描述
    1. Shape 空数组的形状,整数或整数元组
    2. Dtype 所需的输出数组类型,可选
    3. Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

    示例 1

    # 含有 5 个 1 的数组,默认类型为 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]]
  • 相关阅读:
    性能测试之开源的性能监控软件
    POJ 2553 The Bottom of a Graph TarJan算法题解
    Spring AOP切面
    [2014.5.22][UBUNTU]Ubuntu与Windows系统时间不同步的问题
    MySQL bug:server-id默认被自己主动置为1
    [Android]Fragment源代码分析(二) 状态
    window nginx 启动无提示错误,却没有listen 80port
    Shell 命令--文件创建、搜索命令--总结自《Linux Shell 脚本攻略》
    freemarker 自己定义指令
    javascript Deferred和递归次数限制
  • 原文地址:https://www.cnblogs.com/navysummer/p/9640682.html
Copyright © 2011-2022 走看看