zoukankan      html  css  js  c++  java
  • Numpy 的常用属性 和创建数组

    1、集中常见的 numpy 的属性

      ndim:维度

      shape:行数和列数

      size:元素的个数

    >>> import numpy as np # 导入 numpy 模块。np是为了使用方便的简写
    >>> array = np.array([[1,2,3],[2,3,4]]) # 列表转换为矩阵
    >>> print(array)
        [[1 2 3]
          [2 3 4] ]
    >>>print(‘number of ndim:’, array.ndim)  # 维度
    number of mdim: 2
    >>> print('shape:',array.shape) # 行数和列数
    shape:(2, 3)
    >>>print(‘size:’,array.aize) # 元素个数
    size : 6

    2、Numpy 创建 array

    2.1、关键字

      array:创建数组

      dtype:制定数据类型

      zeros:创建数据全为 0 

      ones:创建数据全为 1

      empty:创建数据接近 0

      arrange:按指定范围创建数据

      linspace:创建线段

    # 创建数组
    >>> a = np.array([2, 23, 4])
    >>> print(a)
    [2, 23, 4]

    # 指定类型
    >>> a = np.array([2, 23, 4], dtype = np.int)
    >>> print(a.dtype)
    int32

    >>>a = np.array([2, 23, 4], dtype = np.float32)
    >>> print(a.dtype)
    float32

    # 创建特定数据
    >>> a = np.array([[2, 23, 4],[2, 32, 4]]) # 2d 矩阵 2行3列
    >>> print(a)
    [[2 23 4]
      [2 32 4]]

    # 创建全零数组
    >>> a = np.zeros((3, 4)) # 数据全部为 0 3 行 4 列
    >>> print(a)
    [[0. 0. 0. 0.]
      [0. 0. 0. 0.]
      [0. 0. 0. 0.]]

    # 创建全 1 的数组
    >>> a = np.ones((3, 4), dtype = np.int) # 数据为 1 3行4列
    >>> print(a)
    {[1 1 1 1]
    [1 1 1 1]
    [1 1 1 1]}

    # 创建全空数组 其实每个值都是接近于零的数
    >>> a = np.empty((3, 4)) # 数据为 empty 3行 4列
    >>> print(a)
     [[0. 0. 0. 0.]
      [0. 0. 0. 0.]
      [0. 0. 0. 0.]]

    # 用 arange 创建连续数组:
    >>> a = np.arange(10, 20, 2) # 10 - 19 的数据,步长为 2
    >>> print(a)
    [10 12 14 16 18]

    # 使用 linspace 创建线段型数据:
    >>>a = np.linspace(1, 10, 20) # 开始端为1, 结束端为 10, 且分割为 20 个数据,生成线段
    >>> print(a)
    [ 1.          1.47368421  1.94736842  2.42105263  2.89473684  3.36842105  3.84210526  4.31578947  4.78947368  5.26315789  5.73684211  6.21052632
     6.68421053  7.15789474  7.63157895  8.10526316  8.57894737  9.05263158
     9.52631579 10.        ]

    # 同样也能进行 reshape 工作:

      >>> a = np.linspace(1, 10, 20).reshape((5, 4)) # 更改shape

      >>> print(a)

      

     [[ 1.          1.47368421  1.94736842  2.42105263]
      [ 2.89473684  3.36842105  3.84210526  4.31578947]
      [ 4.78947368  5.26315789  5.73684211  6.21052632]
      [ 6.68421053  7.15789474  7.63157895  8.10526316]
      [ 8.57894737  9.05263158  9.52631579 10.        ]]
  • 相关阅读:
    关于hive Metadata 使用 MsSQL
    hdp 2.06 安装备忘
    对于自我管理 ObjectContextManager的测试
    关于 Linq to EF 的内存泄漏问题
    使用过多的递归出现错误,“System.StackOverflowException”类型的未经处理的异常在 mscorlib.dll 中发生
    PowerShell 如何 远程连接【转】
    win7系统浏览器老是自动弹出网页怎么办
    win10如何深度清理C盘
    Win7电脑系统崩溃怎么解决?
    win7磁盘打不开如何解决
  • 原文地址:https://www.cnblogs.com/jcjc/p/10794456.html
Copyright © 2011-2022 走看看