NumPy学习笔记(一)

发布时间:2019-05-20 22:58:24编辑:auto阅读(1956)

    # NumPy
    ### 安装
    - 通过安装Anaconda安装NumPy,一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项,包含了大量的科学计算相关的包,其中就包括NumPy
    - 通过pip安装,
    - 在windows中,控制台中输入命令安装
    ```python
    >pip install numpy
    ```
    - 在ubuntu中,控制台输入命令安装
    ```python
    XXX:~/Desktop$sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
    ```
    - 安装验证,进入python交互终端,输入命令,没有报错则安装成功
    ```python
    >python
    >>> import numpy as np
    ```
    ### numpy中最重要的对象---ndarray:
    **Ndarray对象指的是用于存放同类型元素的多维数据,它是一个多维容器,N代表着它的维度**


    #### 创建ndarray对象
    - 通过array方法创建
    - 参数说明
    - 必选参数
    - object 数组或嵌套的数列
    - 可选参数
    - dtype 数组元素的数据类型
    - copy 对象是否需要复制
    - order 创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)
    - subok 默认返回一个与基类类型一致的数组
    - ndmin 指定生成数组的最小维度
    - demo
    ```python
    >>> from numpy as np
    >>> x = np.array([[1, 2, 3], [4, 5, 6]],dtype=np.int32,copy=True,order=None,subok=False,ndmin=0)
    >>> type(x) # 查看x类型
    <type 'numpy.ndarray'>
    >>> x.shape # 查看ndarray对象的维度
    (2, 3)
    >>> x.dtype # 查看x里的数据类型
    dtype('int32')
    ```
    - 通过zeros/ones方法创建(创建指定大小的数组,数组元素以 0/1 来填充,)
    - 参数说明
    - 必要参数
    - shape 数组形状
    - 可选参数
    - dtype 数据类型
    - order 'C' 用于 C 的行数组,或者 'F' 用于 FORTRAN 的列数组
    - demo
    ```python
    >>> np.zeros(5)
    array([0., 0., 0., 0., 0.])
    >>> np.zeros((3,3))
    array([[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]])
    ```
    - 通过empty方法创建(创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:)
    - 参数说明
    - 必要参数
    - shape 数组形状
    - 可选参数
    - dtype 数据类型
    - 有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序
    - demo
    ```python
    >>> x = np.empty((3,2),dtype=int)
    >>> x
    array([[0, 0],
    [0, 0],
    [0, 0]])
    >>> y = np.empty([3,2],dtype=int)
    >>> y
    array([[0, 0],
    [0, 0],
    [0, 0]])
    ```
    - 其他方法:
    - 通过full方法创建(创建一个填充给定值的n * n数组)
    - demo
    ```python
    >>> np.full([3,3],3)
    array([[3, 3, 3],
    [3, 3, 3],
    [3, 3, 3]])
    ```
    - 通过eye方法创建(创建一个对角线是1,其余是0的多维数组)
    - demo
    ```python
    >>> np.eye(3)
    array([[1., 0., 0.],
    [0., 1., 0.],
    [0., 0., 1.]])
    >>> np.eye(1)
    array([[1.]])
    ```
    - 通过linspace方法创建(创建一个在指定的时间间隔内返回均匀间隔的数字的数组)
    - demo
    ```python
    >>> np.linspace(0,8.8,num=5)
    array([0. , 2.2, 4.4, 6.6, 8.8])
    ```
    - 通过random方法创建(创建一个填充0到1之间随机值的数组)
    - demo
    ```python
    >>> np.random.random([3,3])
    array([[0.17647511, 0.79086009, 0.26275058],
    [0.83484953, 0.6386956 , 0.53928901],
    [0.26020885, 0.58836421, 0.39308341]])
    ```
    // TODO :补充
    #### NumPy支持的数据类型(ndarray对象支持的数据类型)
    名称 | 描述
    ---|---
    bool_ | 布尔型数据类型(True 或者 False)
    int_ | 默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
    intc | 与 C 的 int 类型一样,一般是 int32 或 int 64
    intp | 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
    int8 | 字节(-128 to 127)
    int16 | 整数(-32768 to 32767)
    int32 | 整数(-2147483648 to 2147483647)
    int64 | 整数(-9223372036854775808 to 9223372036854775807)
    uint8 | 无符号整数(0 to 255)
    uint16 | 无符号整数(0 to 65535)
    uint32 | 无符号整数(0 to 4294967295)
    uint64 | 无符号整数(0 to 18446744073709551615)
    float_ | float64 类型的简写
    float16 | 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
    float32 | 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
    float64 | 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
    complex_ | complex128 类型的简写,即 128 位复数
    complex64 | 复数,表示双 32 位浮点数(实数部分和虚数部分)
    complex128 | 复数,表示双 64 位浮点数(实数部分和虚数部分)


    #### ndarray对象的属性
    属性 | 说明
    ---|---
    ndarray.ndim | 秩,即轴的数量或维度的数量
    ndarray.shape | 数组的维度,对于矩阵,n 行 m 列
    ndarray.size | 数组元素的总个数,相当于 .shape 中 n*m 的值
    ndarray.dtype | ndarray 对象的元素类型
    ndarray.itemsize| ndarray 对象中每个元素的大小,以字节为单位
    ndarray.flags | ndarray 对象的内存信息
    ndarray.real | ndarray元素的实部
    ndarray.imag | ndarray 元素的虚部
    ndarray.data | 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。
    demo
    ```python
    >>> a = np.full((3,3),3)
    >>> a
    array([[3, 3, 3],
    [3, 3, 3],
    [3, 3, 3]])
    >>> a.ndim
    2
    >>> a.shape
    (3, 3)
    >>> a.size
    9
    >>> a.dtype
    dtype('int32')
    >>> a.itemsize
    4
    >>> a.flags
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    WRITEBACKIFCOPY : False
    UPDATEIFCOPY : False
    >>> a.real
    array([[3, 3, 3],
    [3, 3, 3],
    [3, 3, 3]])
    >>> a.imag
    array([[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]])
    >>> a.data
    <memory at 0x0FE0E990>

    ```
    #### ndarray对象的的基本操作
    - 加减乘除四则运算
    ```python
    >>> a = np.full((3,3),3)
    >>> a
    array([[3, 3, 3],
    [3, 3, 3],
    [3, 3, 3]])
    >>> a+1
    array([[4, 4, 4],
    [4, 4, 4],
    [4, 4, 4]])
    >>> a-2
    array([[1, 1, 1],
    [1, 1, 1],
    [1, 1, 1]])
    >>> a*5
    array([[15, 15, 15],
    [15, 15, 15],
    [15, 15, 15]])
    >>> a/3
    array([[1., 1., 1.],
    [1., 1., 1.],
    [1., 1., 1.]])
    >>> a
    array([[3, 3, 3],
    [3, 3, 3],
    [3, 3, 3]])

    >>> b = np.full((3,3),3)
    >>> b
    array([[3, 3, 3],
    [3, 3, 3],
    [3, 3, 3]])
    >>> a+b
    array([[6, 6, 6],
    [6, 6, 6],
    [6, 6, 6]])
    >>> a += b
    >>> a
    array([[6, 6, 6],
    [6, 6, 6],
    [6, 6, 6]])

    ```
    **注意:虽然可以对两个ndarray对象进行操作,但是如果没有赋值,不会改变原来的ndarray对象**

    ##### 当对两个ndarray对象数据类型精度不一样进行操作时,结果的精度为更精确的那个数据类型
    ```python
    >>> a = np.array([[0.1,0.2,0.3],[0.3,0.2,0.1]],dtype=np.float32)
    >>> a
    array([[0.1, 0.2, 0.3],
    [0.3, 0.2, 0.1]], dtype=float32)
    >>> b = np.array([[0.3,0.2,0.1],[0.1,0.2,0.3]],dtype=np.float64)
    >>> b
    array([[0.3, 0.2, 0.1],
    [0.1, 0.2, 0.3]])
    >>> c = a+b
    >>> c
    array([[0.4 , 0.4 , 0.40000001],
    [0.40000001, 0.4 , 0.4 ]])
    >>> c.dtype
    dtype('float64')

    ```
    - 常用数学函数sum、min、max等
    ```python
    >>> a = np.arange(12).reshape(4,3) # reshape可以设置输出时的维度
    >>> a
    array([[ 0, 1, 2],
    [ 3, 4, 5],
    [ 6, 7, 8],
    [ 9, 10, 11]])
    >>> a.sum()
    66
    >>> a.sum(axis=0) # axis=0表示求列的相关操作
    array([18, 22, 26]) # axis=1表示求行的相关操作
    >>> a.sum(axis=1)
    array([ 3, 12, 21, 30])
    >>> a.min()
    0
    >>> a.min(axis=0)
    array([0, 1, 2])
    >>> a.min(axis=1)
    array([0, 3, 6, 9])
    >>> a.max()
    11
    >>> a.max(axis=0)
    array([ 9, 10, 11])
    >>> a.max(axis=1)
    array([ 2, 5, 8, 11])
    >>> np.sin(a)
    array([[ 0. , 0.84147098, 0.90929743],
    [ 0.14112001, -0.7568025 , -0.95892427],
    [-0.2794155 , 0.6569866 , 0.98935825],
    [ 0.41211849, -0.54402111, -0.99999021]])
    >>> np.cos(a)
    array([[ 1. , 0.54030231, -0.41614684],
    [-0.9899925 , -0.65364362, 0.28366219],
    [ 0.96017029, 0.75390225, -0.14550003],
    [-0.91113026, -0.83907153, 0.0044257 ]])
    >>> np.tan(a)
    array([[ 0.00000000e+00, 1.55740772e+00, -2.18503986e+00],
    [-1.42546543e-01, 1.15782128e+00, -3.38051501e+00],
    [-2.91006191e-01, 8.71447983e-01, -6.79971146e+00],
    [-4.52315659e-01, 6.48360827e-01, -2.25950846e+02]])
    ```
    - 类似于python中列表的操作
    - 索引,一维数组的索引和列表一样,多维数组的索引需要根据维度索引
    ```python
    >>> a = np.arange(12)
    >>> a
    array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
    >>> a[0]
    0
    >>> a[11]
    11
    >>> b = np.arange(12).reshape(2,6)
    >>> b[0,0]
    0
    >>> b[5,5]
    >>> b[1,5]
    11
    ```
    - 切片,与python中的列表相似,也是左包含右不包含
    ```python
    >>> b[:,5]
    array([ 5, 11])
    >>> b[1,1:2]
    array([7])
    >>> b[1,1:5]
    array([ 7, 8, 9, 10])
    ```
    **多维数组切片时,一定要注意好维度,根据维度来切片**
    - 迭代,与python的列表相似,都可以用for in 来遍历ndarray对象,一维数组遍历和列表一样,多维数组遍历会得到次维的数组
    ```python
    >>> for i in b:
    ... print(i)
    ...
    [0 1 2 3 4 5]
    [ 6 7 8 9 10 11]
    ```

关键字