python基础知识——numpy

来自:http://cs231n.github.io/python-numpy-tutorial/
更多:https://docs.scipy.org/doc/numpy/reference/

数组初始化方式:
    一个numpy数组是一个值的矩阵,所有类型相同,并且索引是非负整数。维数是阵列的等级;
    数组的形状是一个整数的元组,它给出了每个维度上数组的大小。
    我们可以从嵌套的Python列表初始化numpy数组,并使用方括号来访问元素:
        a = np.array([1, 2, 3])
    可以通过a.shape来得到矩阵的形状

    Numpy还提供了许多功能来初始化数组:
    初始化一个全零矩阵,参数为矩阵形状:
        a = np.zeros((2,2))
    初始化一个全一矩阵,参数为矩阵形状:
        b = np.ones((1,2))
    初始化一个常数矩阵,参数第一个是形状,第二个是要被初始化的常数:
        c = np.full((2,2), 7)
    初始化一个单位矩阵,参数是矩阵的维数:
        d = np.eye(2)
    初始化一个随机数矩阵,参数是矩阵形状:
        e = np.random.random((2,2))
    初始化一个与之前矩阵形状相同的矩阵,参数是被参考形状的矩阵,值为随机数:
        f = np.empty_like(a)
    初始化一个矩阵,内容将向量a在一行内复制y次,再将该行赋值x次:
        g = np.tile(a, (x, y))

数组索引:
    1.切片:
        与Python列表类似,numpy数组可以被切片。由于数组可能是多维的,因此必须为数组
        的每个维度指定一个切片。索引a后得到的结果赋值给其他的存储变量b,b的存储地址
        仍指向a的存储地址。

        也可以将整数索引与切片索引混合在一起。但是,这样做会产生比原始数组更低级别的数
        组。这与MATLAB处理数组切片的方式完全不同。

    2.整数数组索引:
        当使用切片将数组索引到numpy数组时,得到的数组将始终是原始数组的子数组。相比之
        下,整型数组索引允许您使用另一个数组中的数据构造任意数组。
        a[[0, 1, 2], [0, 1, 0]]
        的结果等同于
        [a[0, 0], a[1, 1], a[2, 0]]

        整数数组索引的一个有用技巧是从矩阵的每一行中选择或变异一个元素:
            a = np.array([0, 2, 0, 1])
            b[np.arange(4), b] += 10  # 数组b中0到3行的第0,2,0,1元素
                                      # 的值分别加10

    3.布尔数组索引:
        布尔数组索引可以挑出数组的任意元素。这种类型的索引用于索引满足某些条件的数组元
        素:
            bool_idx = (a > 2)  # 返回矩阵中的元素说明矩阵a中对应
                                # 位置的值是否大于二,如果满足条件则为True,
                                # 否则为False

            a[bool_idx]  # 返回数组的内容为bool_idx中值为True的位置上数组a中
                         # 对应的值

            a[a > 2]  # 效果等同于以上两步的合并

数据类型:
    每个numpy数组都是相同类型的元素的矩阵。 Numpy提供了一组可用于构造数组的数字数据
    类型。 Numpy在创建数组时尝试猜测数据类型,但构造数组的函数通常还包含可选参数以明
    确指定数据类型:
        x = np.array([1, 2], dtype=np.int64)  # 强制一个特定的数据类型

数组运算:
    矩阵加法:x + y
            np.add(x, y)
    矩阵减法:x - y
            np.subtract(x, y)
    矩阵叉乘:x * y
            np.multiply(x, y)
    矩阵除法:x / y
            np.divide(x, y)
    矩阵各元素开方:np.sqrt(x)
    矩阵点乘:
        x = np.array([[1,2],[3,4]])
        y = np.array([[5,6],[7,8]])

        v = np.array([9,10])
        w = np.array([11, 12])
        向量点乘:v.dot(w)
                np.dot(v, w)
        矩阵点乘列向量:x.dot(v)
                     np.dot(x, v)
        矩阵点乘矩阵:x.dot(y)
                   np.dot(x, y)
    求矩阵内元素的和:
        所有元素和:np.sum(x)
        所有行每一列的和:np.sum(x, axis=0)
        所有列每一行的和:np.sum(x, axis=1)
    矩阵转置矩阵:x.T(向量的转置还是向量)

广播:
    广播允许将向量加到矩阵的每一行并且不对向量执行复制操作:
        import numpy as np

        # We will add the vector v to each row of the matrix x,
        # storing the result in the matrix y
        x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
        v = np.array([1, 0, 1])
        y = x + v  # Add v to each row of x using broadcasting
        print(y)  # Prints "[[ 2  2  4]
                  #          [ 5  5  7]
                  #          [ 8  8 10]
                  #          [11 11 13]]"

    向量广播的基本原则:(不是很懂)
        1.If the arrays do not have the same rank, prepend the shape of
            the lower rank array with 1s until both shapes have the same
            length.
        2.The two arrays are said to be compatible in a dimension if they
            have the same size in the dimension, or if one of the arrays has
            size 1 in that dimension.
        3.The arrays can be broadcast together if they are compatible in
            all dimensions.
        4.After broadcasting, each array behaves as if it had shape equal
            to the elementwise maximum of shapes of the two input arrays.
        5.In any dimension where one array had size 1 and the other array
            had size greater than 1, the first array behaves as if it were
            copied along that dimension

猜你喜欢

转载自blog.csdn.net/qq_32652679/article/details/80977219