• NumPy基础:多维数组对象


    • 创建ndarray
      • array:将输入数据转换为ndarry
      • arange:类似于range,返回一个ndarray
      • ones、ones_like:根据指定形状和dtype创建一个全1数组。ones_like以另一个数组为参照,根据其形状和dtype创建一个全1数组
      • zeros、zeros_like:根据指定形状和dtype创建一个全0数组。zeros_like以另一个数组为参照,根据其形状和dtype创建一个全0数组
      • empty、empty_like:创建新数组,只分配内存空间但不填充任何值
      • eye、identity:创建一个正方的N*N单位矩阵(对角线为1,其余为0)
    import numpy as np
    data1 = np.array([1,9,8,8,0,4,1,6])
    print(data1)
    '''
    [1 9 8 8 0 4 1 6]
    '''
    
    data2 = np.array([[1,2,3],[4,5,6]])
    print(data2)
    '''
    [[1 2 3]
     [4 5 6]]
     '''
    data3 = np.arange(10)
    print(data3)
    '''
    [0 1 2 3 4 5 6 7 8 9]
    '''
    data4 = np.ones(3)
    print(data4)
    '''
    [1. 1. 1.]
    '''
    data_like = [[1,2,3],[4,5,6]]
    data5 = np.ones_like(data_like)
    print(data5)
    '''
    [[1 1 1]
     [1 1 1]]
    '''
    data4 = np.ones(3)
    print(data4)
    '''
    [1. 1. 1.]
    '''
    data6 = np.zeros(3)
    print(data6)
    '''
    [0. 0. 0.]
    '''
    data_like = [[1,2,3],[4,5,6]]
    data7 = np.zeros_like(data_like)
    print(data7)
    '''
    [[0 0 0]
     [0 0 0]]
    '''
    data8 = np.empty(3)
    print(data8)
    '''
    [4.24399158e-314 8.48798317e-314 1.27319747e-313]
    '''
    data9 = np.eye(3)
    print(data9)
    '''
    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]
    '''
    • ndarray的数据类型
    import numpy as np
    
    # np.array 会尝试为新建的数组推断一个较为合适的数据类型
    arr1 = np.array([1,2,3])
    print(arr1.dtype)
    '''
    int32
    '''
    
    # 可使用dtype数据类型
    arr2 = np.array([1,2,3],dtype=np.float)
    print(arr2.dtype)
    '''
    float64
    '''
    
    arr3 = np.array([1.1,2,3])
    print(arr3.dtype)
    '''
    float64
    '''
    
    arr4 = np.array([1.1,2,3],dtype=np.int32)
    print(arr4.dtype)
    '''
    int32
    '''
    
    # 通过astype方法可转换dtype
    # 调用astype会创建出一个新数组(原始数据的一份拷贝)
    arr5 = np.array([1,2,3])
    float_arr = arr5.astype(np.float64)
    print(float_arr.dtype)
    '''
    float64
    '''
    
    # 注意:如果浮点数转换为整数,则小数部分将会被截断
    arr6 = np.array([1.2,2,3])
    int_arr = arr6.astype(np.int32)
    print(int_arr.dtype)
    '''
    int32
    '''
    print(int_arr)
    '''
    [1 2 3]
    '''
    • 数组和标量之间的运算
    import numpy as np
    
    arr = np.array([[1,2,3],[4,5,6]])
    
    print(arr+arr)
    '''
    [[ 2  4  6]
     [ 8 10 12]]
    '''
    print(arr-arr)
    '''
    [[0 0 0]
     [0 0 0]]
    '''
    print(arr*arr)
    '''
    [[ 1  4  9]
     [16 25 36]]
    '''
    print(arr/arr)
    '''
    [[1. 1. 1.]
     [1. 1. 1.]]
    '''
    print(arr**arr)
    '''
    [[    1     4    27]
     [  256  3125 46656]]
    '''
    print(arr+1)
    '''
    [[2 3 4]
     [5 6 7]]
    '''
    • 基本的索引和切片
    import numpy as np
    
    # 一维数组
    arr = np.arange(10)
    print(arr)
    '''
    [0 1 2 3 4 5 6 7 8 9]
    '''
    print(arr[5])
    '''
    5
    '''
    print(arr[5:8])
    '''
    [5 6 7]
    '''
    # 当你将一个标量赋值给一个切片时,该值会自动传播到整个选区
    arr[5:8] = 12
    print(arr)
    '''
    [ 0  1  2  3  4 12 12 12  8  9]
    '''
    # 数组切片是原始数据视图。这意味着数据不会被复制,任何修改直接反应到源数组上
    arr_slice = arr[5:8]
    arr_slice[1] = 12345
    print(arr)
    '''
    [    0     1     2     3     4    12 12345    12     8     9]
    '''
    arr_slice[:] = 64
    print(arr)
    '''
    [ 0  1  2  3  4 64 64 64  8  9]
    '''
    
    # 二维数组
    arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
    print(arr2d)
    '''
    [[1 2 3]
     [4 5 6]
     [7 8 9]]
    '''
    print(arr2d[2])
    '''
    [7 8 9]
    '''
    print(arr2d[0][2])
    '''
    3
    '''
    print(arr2d[0,2])
    '''
    3
    '''
    # 多维数组
    arr3d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
    print(arr3d)
    '''
    [[[ 1  2  3]
      [ 4  5  6]]
    
     [[ 7  8  9]
      [10 11 12]]]
    '''
    print(arr3d[0])
    '''
    [[1 2 3]
     [4 5 6]]
    '''
    old_values = arr3d[0].copy()
    arr3d[0] = 42
    print(arr3d)
    '''
    [[[42 42 42]
      [42 42 42]]
    
     [[ 7  8  9]
      [10 11 12]]]
    '''
    arr3d[0] = old_values
    print(arr3d)
    '''
    [[[ 1  2  3]
      [ 4  5  6]]
    
     [[ 7  8  9]
      [10 11 12]]]
    
    '''
    print(arr3d[1,0])
    '''
    [7 8 9]
    '''
    • 切片索引
    import numpy as np
    
    # 一维数组
    arr = np.arange(10)
    print(arr[1:6])
    '''
    [1 2 3 4 5]
    '''
    
    # 二维数组
    arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
    print(arr2d[:2])
    '''
    [[1 2 3]
     [4 5 6]]
    '''
    print(arr2d[:2,1:])
    '''
    [[2 3]
     [5 6]]
    '''
    # 索引与切片混合
    print(arr2d[1,:2])
    '''
    [4 5]
    '''
    # 只有冒号表示选区整个轴
    print(arr2d[:,:2])
    '''
    [[1 2]
     [4 5]
     [7 8]]
    '''
    # 对切片赋值也会扩散到整个选区
    arr2d[:,:2]=0
    print(arr2d)
    '''
    [[0 0 3]
     [0 0 6]
     [0 0 9]]
    '''
    • 布尔型索引
    import numpy as np
    from numpy.matlib import randn
    
    names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
    
    # 等于
    print(names=="Bob")
    '''
    [ True False False  True False False False]
    '''
    # 不等于
    print(names!="Bob")
    '''
    [False  True  True False  True  True  True]
    '''
    # 组合多个条件使用&、|
    print((names=="Bob")|(names=="Will"))
    '''
    [ True False  True  True  True False False]
    '''
    data = randn(7,3)
    print(data)
    '''
    [[ 0.35234481  0.68539956  0.2206396 ]
     [-1.3719165  -0.42694698  1.28509104]
     [-0.95479498 -0.65378008 -0.1673056 ]
     [-1.79677508  0.18923784  1.67064335]
     [-1.24383276 -0.50056086 -0.7917794 ]
     [-0.92646918  0.47489349 -0.62463223]
     [ 0.0995606  -1.20420049 -1.55692415]]
    '''
    mask =(names=="Bob")  # 0和3为Ture,取0和3行
    print(data[mask])
    '''
    [[ 0.35234481  0.68539956  0.2206396 ]
     [-1.79677508  0.18923784  1.67064335]]
    '''
    #
    mask =(names=="Bob")|(names=="Will") # 0、2、3、4为Ture,取这4行
    print(data[mask])
    '''
    [[ 0.35234481  0.68539956  0.2206396 ]
     [-0.95479498 -0.65378008 -0.1673056 ]
     [-1.79677508  0.18923784  1.67064335]
     [-1.24383276 -0.50056086 -0.7917794 ]]
    '''
    # 将data中负值置为0
    data[data<0] = 0
    print(data)
    '''
    [[0.35234481 0.68539956 0.2206396 ]
     [0.         0.         1.28509104]
     [0.         0.         0.        ]
     [0.         0.18923784 1.67064335]
     [0.         0.         0.        ]
     [0.         0.47489349 0.        ]
     [0.0995606  0.         0.        ]]
    '''
    # 通过布尔数组设置整条或整列的值
    data[names=="Bob"] = 7
    print(data)
    '''
    [[7.         7.         7.        ]
     [0.         0.5615304  0.        ]
     [0.         1.05144009 0.04887547]
     [7.         7.         7.        ]
     [0.5067665  0.         0.        ]
     [2.0465758  0.78871507 0.68937188]
     [1.58986486 0.86841601 1.46533603]]
    '''
    • 花式索引
    import numpy as np
    
    # 花式索引:利用整数数组进行索引
    '''
    arr = np.empty((8,4))
    for i in range(8):
        arr[i] = i
    '''
    arr = np.arange(16).reshape((4,4))
    print(arr)
    '''
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    '''
    # 为了特定顺序选取行子集,需传入一个用于指定顺序的整数列
    print(arr[[1,0,2]])
    '''
    [[ 4  5  6  7]
     [ 0  1  2  3]
     [ 8  9 10 11]]
    '''
    # 传入多个索引
    print(arr[[1,0,2],[0,1,2]]) 
    '''
    [ 4  1 10]
    '''
    # 使用负数索引将会从末尾开始选行
    print(arr[[-3,-2,-1]])
    '''
    [[ 4  5  6  7]
     [ 8  9 10 11]
     [12 13 14 15]]
    '''
    • 数组的转置与轴对换
    import numpy as np
    
    arr = np.arange(15).reshape((3,5))
    print(arr)
    '''
    [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]
    '''
    print(arr.T)
    '''
    [[ 0  5 10]
     [ 1  6 11]
     [ 2  7 12]
     [ 3  8 13]
     [ 4  9 14]]
    '''
    # 矩阵内积
    print(np.dot(arr.T,arr))
    '''
    [[125 140 155 170 185]
     [140 158 176 194 212]
     [155 176 197 218 239]
     [170 194 218 242 266]
     [185 212 239 266 293]]
    '''
    
    arr2 = np.arange(16).reshape((2,2,4))
    print(arr2)
    '''
    [[[ 0  1  2  3]
      [ 4  5  6  7]]
    
     [[ 8  9 10 11]
      [12 13 14 15]]]
    '''
    print(arr2.transpose((1,0,2))) #行列索引值对换
    '''
    [[[ 0  1  2  3]
      [ 8  9 10 11]]
    
     [[ 4  5  6  7]
      [12 13 14 15]]]
    '''
    
    arr3 = np.arange(16).reshape((2,2,4))
    print(arr3)
    '''
    [[[ 0  1  2  3]
      [ 4  5  6  7]]
    
     [[ 8  9 10 11]
      [12 13 14 15]]]
    '''
    print(arr3.swapaxes(1,2))
    '''
    [[[ 0  4]
      [ 1  5]
      [ 2  6]
      [ 3  7]]
    
     [[ 8 12]
      [ 9 13]
      [10 14]
      [11 15]]]
    '''
  • 相关阅读:
    Understanding about Baire Category Theorem
    Isometric embedding of metric space
    Convergence theorems for measurable functions
    Mindmap for "Principles of boundary element methods"
    Various formulations of Maxwell equations
    Existence and uniqueness theorems for variational problems
    Kernels and image sets for an operator and its dual
    [loj6498]农民
    [luogu3781]切树游戏
    [atAGC051B]Three Coins
  • 原文地址:https://www.cnblogs.com/nicole-zhang/p/12931168.html
Copyright © 2020-2023  润新知