美文网首页我爱编程
numpy基础加强版

numpy基础加强版

作者: 且行歌 | 来源:发表于2018-02-05 21:15 被阅读76次

    数据类型

    import numpy as np
    arr1 = np.array([1,2,3],dtype=np.float64)
    
    arr1
    
    array([1., 2., 3.])
    
    arr2 = np.array([1,2,3],dtype=np.int32)
    
    arr2
    
    array([1, 2, 3], dtype=int32)
    
    arr1.dtype
    
    dtype('float64')
    
    arr2.dtype
    
    dtype('int32')
    

    新手不必担心,用时再考虑即可

    2.数据类型转换

    arr = np.array([1,2,3,4,5])
    
    arr.dtype
    
    dtype('int64')
    
    float_arr = arr.astype(np.float64) #转换方法
    
    float_arr.dtype
    
    dtype('float64')
    

    浮点数转换为整型时,小数点会被省略

    arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])
    
    arr
    
    array([ 3.7, -1.2, -2.6,  0.5, 12.9, 10.1])
    
    arr.astype(np.int32) #转换方法
    
    array([ 3, -1, -2,  0, 12, 10], dtype=int32)
    

    字符串转换为浮点数

    n_s = np.array(['1.25','-9.6','42'],dtype=np.string_)
    
    n_s.astype(float)#转换
    
    array([ 1.25, -9.6 , 42.  ])
    

    2.3 直接替换

    int_array = np.arange(10)
    
    c_a = np.array([.22,.270,.357,.380,.44,.50],dtype=np.float64)
    
    int_array.astype(c_a.dtype) #将c_a的数据类型应用到int_array
    
    array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
    

    运用简称可减少代码量

    运算

     arr = np.array([[1.,2.,3.,],[4.,5.,6.]])
    
    arr
    
    array([[1., 2., 3.],
           [4., 5., 6.]])
    
    arr * arr #自己*自己
    
    array([[ 1.,  4.,  9.],
           [16., 25., 36.]])
    
    arr - arr
    
    array([[0., 0., 0.],
           [0., 0., 0.]])
    
    1 / arr #标量
    
    array([[1.        , 0.5       , 0.33333333],
           [0.25      , 0.2       , 0.16666667]])
    
    arr * 0.5 #应用到每个元素
    
    array([[0.5, 1. , 1.5],
           [2. , 2.5, 3. ]])
    
    arr2 = np.array([[0.,4.,1.],[7.,2.,12.]])
    
    arr2 > arr #比较,必须在相同size
    
    array([[False,  True, False],
           [ True, False,  True]])
    

    索引和切片

    arr = np.arange(10)
    
    arr
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    arr[5] #选取,从0开始
    
    5
    
    arr[5:8] #切片
    
    array([5, 6, 7])
    
    arr[5:8] = 12 #赋值
    
    arr
    
    array([ 0,  1,  2,  3,  4, 12, 12, 12,  8,  9])
    
    arr_slice = arr[5:8] #构建新对象
    
    arr_slice
    
    array([12, 12, 12])
    
    arr_slice[1] = 12345 #新对象的变化会影响原来对象
    
    arr
    
    array([    0,     1,     2,     3,     4,    12, 12345,    12,     8,
               9])
    
    arr_slice[:] = 64 #特殊选取方法
    
    arr
    
    array([ 0,  1,  2,  3,  4, 64, 64, 64,  8,  9])
    
    arr[5:8].copy #显性表示复制
    
    <function ndarray.copy>
    

    多维亦是如此.相应联想

    arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
    
    arr2d[2]#索引
    
    array([7, 8, 9])
    
    arr2d[2][2]
    
    9
    
    arr2d[2,2] #同等方法
    
    9
    

    两维时,axis 0 代表 rows(行),axis 1 代表 columns(列)

    arr3d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) #2 2 3
    
    arr3d
    
    array([[[ 1,  2,  3],
            [ 4,  5,  6]],
    
           [[ 7,  8,  9],
            [10, 11, 12]]])
    
    arr3d[0] #依旧为 2 X  3的array对象
    
    array([[1, 2, 3],
           [4, 5, 6]])
    
    old_values = arr3d[0].copy()
    
    arr3d[0]=42
    
    arr3d
    
    array([[[42, 42, 42],
            [42, 42, 42]],
    
           [[ 7,  8,  9],
            [10, 11, 12]]])
    
    arr3d[0] = old_values  #copy对象不影响原对象
    arr3d    
    
    array([[[ 1,  2,  3],
            [ 4,  5,  6]],
    
           [[ 7,  8,  9],
            [10, 11, 12]]])
    
    arr3d[1,0,0] #嵌套选取
    
    7
    

    切片索引

    arr
    
    array([ 0,  1,  2,  3,  4, 64, 64, 64,  8,  9])
    
    arr[1:6]
    
    array([ 1,  2,  3,  4, 64])
    
    arr[:6]
    
    array([ 0,  1,  2,  3,  4, 64])
    
    arr2d
    
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    
    arr2d[:2] #止于2,不包括第2,选取前两行
    
    array([[1, 2, 3],
           [4, 5, 6]])
    
    arr2d[:2,1:] #选取前两行的后两列
    
    array([[2, 3],
           [5, 6]])
    
    arr2d[1,:2] #选取第一行的前两列
    
    array([4, 5])
    
    arr2d[:,:1] #冒号代表全部选中
    
    array([[1],
           [4],
           [7]])
    
    arr2d[:2,1:] = 0 #替换依然可行
    arr2d
    
    array([[1, 0, 0],
           [4, 0, 0],
           [7, 8, 9]])
    

    布尔值,即真假,原理是真假对用1和0,只有两个,便于判断

    names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
    
    data = np.random.randn(7,4) #长度必须相等
    
    names
    
    array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
    
    data
    
    array([[ 0.03317879, -0.64295019, -0.07074687, -0.47557693],
           [-0.38937679, -1.12534115, -1.81676011, -1.15545174],
           [-0.35255783, -0.20309579, -0.78609489, -0.83429794],
           [-0.72824954, -0.01455318,  2.40890211,  0.45262685],
           [-1.50436899, -0.01719232, -0.70454368,  2.29021736],
           [-1.0090259 , -1.0858159 , -0.55730015,  0.29311532],
           [ 0.24161904,  0.04861702,  0.23810033, -0.78593455]])
    
    names == 'Bob' #两个等号表示数学意义上的相等,一个等号表示逻辑上的赋值
    
    array([ True, False, False,  True, False, False, False])
    
    data[names == 'Bob']
    
    array([[ 0.03317879, -0.64295019, -0.07074687, -0.47557693],
           [-0.72824954, -0.01455318,  2.40890211,  0.45262685]])
    
    data[names == 'Bob', 2:] #高级选取
    
    array([[ 0.53825689,  0.08749286],
           [-0.85205114, -0.772436  ]])
    
    data[names == 'Bob',3]
    
    array([ 0.08749286, -0.772436  ])
    
    names != 'Bob' #取反
    
    array([False,  True,  True, False,  True,  True,  True])
    
    data[~(names == 'Bob')] #取反
    
    array([[-1.119334  , -0.99315582, -0.14021638, -0.68652541],
           [ 0.66135045, -0.27269187,  0.25154645,  0.26779766],
           [-0.42776762, -0.84351095, -0.79780832,  1.80460797],
           [-1.1681968 ,  0.13817726,  0.41940656, -0.15786619],
           [ 0.42844477, -1.58866021,  0.31450085, -0.03915103]])
    
    cond = names == 'Bob' #简化操作
    data[~cond]
    
    array([[-1.119334  , -0.99315582, -0.14021638, -0.68652541],
           [ 0.66135045, -0.27269187,  0.25154645,  0.26779766],
           [-0.42776762, -0.84351095, -0.79780832,  1.80460797],
           [-1.1681968 ,  0.13817726,  0.41940656, -0.15786619],
           [ 0.42844477, -1.58866021,  0.31450085, -0.03915103]])
    
    mask = (names == 'Bon') | (names == 'Will')  #或
    mask
    
    
    array([False, False,  True, False,  True, False, False])
    
    data[mask]
    
    array([[ 0.66135045, -0.27269187,  0.25154645,  0.26779766],
           [-0.42776762, -0.84351095, -0.79780832,  1.80460797]])
    
    mask = (names == 'Bob') & (names == 'Will') #和
    
    mask
    
    array([False, False, False, False, False, False, False])
    
    data[mask]
    
    array([], shape=(0, 4), dtype=float64)
    

    根据判断条件批量赋值

    data[data < 0] = 0
    data
    
    array([[0.        , 0.        , 0.53825689, 0.08749286],
           [0.        , 0.        , 0.        , 0.        ],
           [0.66135045, 0.        , 0.25154645, 0.26779766],
           [1.34030771, 1.53001014, 0.        , 0.        ],
           [0.        , 0.        , 0.        , 1.80460797],
           [0.        , 0.13817726, 0.41940656, 0.        ],
           [0.42844477, 0.        , 0.31450085, 0.        ]])
    
    data[names != 'Joe'] = 7
    data
    
    array([[7.        , 7.        , 7.        , 7.        ],
           [0.        , 0.        , 0.        , 0.        ],
           [7.        , 7.        , 7.        , 7.        ],
           [7.        , 7.        , 7.        , 7.        ],
           [7.        , 7.        , 7.        , 7.        ],
           [0.        , 0.13817726, 0.41940656, 0.        ],
           [0.42844477, 0.        , 0.31450085, 0.        ]])
    

    花式索引

    arr = np.empty((8,4))
    
    for  i in range(8):
        arr[i] = i #按行赋值
    
    arr
    
    array([[0., 0., 0., 0.],
           [1., 1., 1., 1.],
           [2., 2., 2., 2.],
           [3., 3., 3., 3.],
           [4., 4., 4., 4.],
           [5., 5., 5., 5.],
           [6., 6., 6., 6.],
           [7., 7., 7., 7.]])
    
    arr[[4,3,0,6]] #直接选取行
    
    array([[4., 4., 4., 4.],
           [3., 3., 3., 3.],
           [0., 0., 0., 0.],
           [6., 6., 6., 6.]])
    
    arr[[-3,-5,-7]] #倒序也可以
    
    array([[5., 5., 5., 5.],
           [3., 3., 3., 3.],
           [1., 1., 1., 1.]])
    
    arr = np.arange(32).reshape((8,4))
    
    arr
    
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15],
           [16, 17, 18, 19],
           [20, 21, 22, 23],
           [24, 25, 26, 27],
           [28, 29, 30, 31]])
    
    arr[[1,5,7,2],[0,3,1,2]] #行列对应选取,依次为第一行第0个,第五行第3个,,,,
    
    array([ 4, 23, 29, 10])
    

    见证奇迹的时刻,选取为矩阵

    arr[[1,5,7,2]][:,[0,3,1,2]]
    
    array([[ 4,  7,  5,  6],
           [20, 23, 21, 22],
           [28, 31, 29, 30],
           [ 8, 11,  9, 10]])
    

    行列转置

    arr = np.arange(15).reshape((3,5))
    
    arr
    
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])
    
    arr.T
    
    array([[ 0,  5, 10],
           [ 1,  6, 11],
           [ 2,  7, 12],
           [ 3,  8, 13],
           [ 4,  9, 14]])
    

    np.dot 内积

    arr = np.random.randn(6,3)
    
    arr
    
    array([[ 0.14632913,  0.52363917, -0.61383088],
           [-0.43408235, -0.23481596,  0.50205854],
           [ 0.58277602,  0.39006049,  2.44427147],
           [-1.60012924,  1.13015109,  1.87930724],
           [ 0.06145772, -1.50099567, -0.21680612],
           [-0.12640319,  1.1378038 ,  0.33948385]])
    
    np.dot(arr.T,arr)
    
    array([[ 3.12963599, -1.63858658, -1.94666402],
           [-1.63858658,  5.30631065,  3.34968866],
           [-1.94666402,  3.34968866, 10.29736402]])
    

    多维重排

    arr = np.arange(16).reshape((2,2,4))
    
    arr
    
    array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7]],
    
           [[ 8,  9, 10, 11],
            [12, 13, 14, 15]]])
    
    arr.transpose((1,0,2)) #重排方法
    
    array([[[ 0,  1,  2,  3],
            [ 8,  9, 10, 11]],
    
           [[ 4,  5,  6,  7],
            [12, 13, 14, 15]]])
    
    arr
    
    array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7]],
    
           [[ 8,  9, 10, 11],
            [12, 13, 14, 15]]])
    
    arr.swapaxes(1,2) #另一方法
    
    array([[[ 0,  4],
            [ 1,  5],
            [ 2,  6],
            [ 3,  7]],
    
           [[ 8, 12],
            [ 9, 13],
            [10, 14],
            [11, 15]]])
    

    ufunc 通用函数

    arr = np.arange(10)
    
    arr
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    np.sqrt(arr) #平方根
    
    array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
           2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ])
    
    np.exp(arr)
    
    array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,
           5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
           2.98095799e+03, 8.10308393e+03])
    

    二元

    x = np.random.randn(8)
    
    y = np.random.randn(8)
    
    x
    
    array([ 0.76619326, -0.51913337, -0.61943584, -0.51367405, -1.81071209,
            0.20368937, -0.20443369, -0.99255842])
    
    y
    
    array([ 0.69034544,  0.05340984, -0.96784714, -1.6425589 , -0.06741981,
            0.71306132, -0.58463922, -1.3921061 ])
    
    np.maximum(x,y) #取最大值,元素比较
    
    array([ 0.76619326,  0.05340984, -0.61943584, -0.51367405, -0.06741981,
            0.71306132, -0.20443369, -0.99255842])
    
    arr = np.random.randn(7) * 5
    
    arr
    
    array([ 1.75351542, -6.57187357,  9.2276292 ,  7.32215024,  5.6253457 ,
           -5.31130902, -2.07810797])
    
    r_m,w_p = np.modf(arr) #分离为整数部分和小数部分
    
    r_m #小数部分
    
    array([ 0.75351542, -0.57187357,  0.2276292 ,  0.32215024,  0.6253457 ,
           -0.31130902, -0.07810797])
    
    w_p #整数部分
    
    array([ 1., -6.,  9.,  7.,  5., -5., -2.])
    
    arr
    
    array([ 1.75351542, -6.57187357,  9.2276292 ,  7.32215024,  5.6253457 ,
           -5.31130902, -2.07810797])
    
    np.sqrt(arr) #负数不能开根
    
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt
      """Entry point for launching an IPython kernel.
    
    
    
    
    
    array([1.32420369,        nan, 3.0377013 , 2.7059472 , 2.37178112,
                  nan,        nan])
    
    np.sqrt(arr,arr)
    
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt
      """Entry point for launching an IPython kernel.
    
    
    
    
    
    array([1.32420369,        nan, 3.0377013 , 2.7059472 , 2.37178112,
                  nan,        nan])
    
    arr #新array对象
    
    array([1.32420369,        nan, 3.0377013 , 2.7059472 , 2.37178112,
                  nan,        nan])
    

    abs,fabs 绝对值
    square 平方
    log,log10,log2,log1p
    isnan 判断

    二元:add,subtract,multiply,divide, 加减乘除
    maximum,minimum,
    mod 取余
    greater(_equal)/less 比较
    logical_and/or/xor &/|/^

    meshgrid

    points = np.arange(-5,5,0.01)
    
    xs,ys = np.meshgrid(points,points)
    
    xs
    
    array([[-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
           [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
           [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
           ...,
           [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
           [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],
           [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99]])
    
    ys
    
    array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],
           [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],
           [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],
           ...,
           [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],
           [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],
           [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]])
    
    z = np.sqrt(xs ** 2 + ys ** 2)
    
    z
    
    array([[7.07106781, 7.06400028, 7.05693985, ..., 7.04988652, 7.05693985,
            7.06400028],
           [7.06400028, 7.05692568, 7.04985815, ..., 7.04279774, 7.04985815,
            7.05692568],
           [7.05693985, 7.04985815, 7.04278354, ..., 7.03571603, 7.04278354,
            7.04985815],
           ...,
           [7.04988652, 7.04279774, 7.03571603, ..., 7.0286414 , 7.03571603,
            7.04279774],
           [7.05693985, 7.04985815, 7.04278354, ..., 7.03571603, 7.04278354,
            7.04985815],
           [7.06400028, 7.05692568, 7.04985815, ..., 7.04279774, 7.04985815,
            7.05692568]])
    

    where 条件判断

    xarr = np.array([1.1,1.2,1.3,1.4,1.5])
    
    yarr = np.array([2.1,2.2,2.3,2.4,2.5])
    
    cond = np.array([True,False,True,True,False])
    
    result
    
    [1.1, 2.2, 1.3, 1.4, 2.5]
    
    result = np.where(cond,xarr,yarr) #谁真取谁
    
    result
    
    array([1.1, 2.2, 1.3, 1.4, 2.5])
    

    正负值替换

    arr = np.random.randn(4,4)
    
    arr
    
    array([[-0.14266064, -0.16213865, -0.58804574,  0.45092737],
           [ 0.8518436 , -0.78783841, -0.62372951,  1.13642129],
           [ 0.06346719,  1.20635278, -0.09238897,  1.73370433],
           [ 1.62183372,  0.08720613, -0.63442454,  2.27619414]])
    
    cond = arr > 0
    
    np.where(cond, 2, -2) #符合条件为2,不符合为-2
    
    array([[-2, -2, -2,  2],
           [ 2, -2, -2,  2],
           [ 2,  2, -2,  2],
           [ 2,  2, -2,  2]])
    
    np.where(cond,2,arr) #不符合保持原貌
    
    array([[-0.14266064, -0.16213865, -0.58804574,  2.        ],
           [ 2.        , -0.78783841, -0.62372951,  2.        ],
           [ 2.        ,  2.        , -0.09238897,  2.        ],
           [ 2.        ,  2.        , -0.63442454,  2.        ]])
    

    数学和统计方法

    arr = np.random.randn(5,4)
    
    arr
    
    array([[-0.68218677,  1.10310301, -0.21057012, -0.55508466],
           [ 0.91256439, -0.20883975, -0.09176431, -0.66631307],
           [-1.04120502, -1.11187463, -0.03471294, -0.11266913],
           [ 1.71228664,  0.23509329,  0.31352707, -2.61562304],
           [-0.91986032,  0.5552931 ,  0.48340178, -0.20594334]])
    
    arr.mean() #平均值
    
    -0.15706889233659155
    
    np.mean(arr) #平均值
    
    -0.15706889233659155
    
    arr.sum() #和
    
    -3.1413778467318307
    

    行列统计

    arr.mean(axis=1) #按列取均值
    
    array([-0.08618463, -0.01358819, -0.57511543, -0.08867901, -0.0217772 ])
    
    arr.sum(axis=0) #按行取和
    
    array([-0.01840108,  0.57277501,  0.45988147, -4.15563325])
    

    其他方法

    arr = np.array([0,1,2,3,4,5,6,7])
    
    arr
    
    array([0, 1, 2, 3, 4, 5, 6, 7])
    
    arr.cumsum() #累计和
    
    array([ 0,  1,  3,  6, 10, 15, 21, 28])
    

    多维

    arr = np.array([[0,1,2],[3,4,5],[6,7,8]])
    
    arr
    
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    arr.cumsum(0) #按行取累计和
    
    array([[ 0,  1,  2],
           [ 3,  5,  7],
           [ 9, 12, 15]])
    
    arr.cumprod(1) #按列
    
    array([[  0,   0,   0],
           [  3,  12,  60],
           [  6,  42, 336]])
    

    std 标准差
    var 方差
    min,max 大小
    argmin,argmax 返回索引

    布尔值方法

    arr = np.random.randn(100)
    
    (arr > 0).sum() #判断条件
    
    63
    
    bools = np.array([False,False,True,False])
    
    bools.any() #一真则真,全假则假
    
    True
    
    bools.all() #全真为真,一假则假
    
    False
    

    排序

    arr = np.random.randn(6)
    
    arr
    
    array([-1.19241559,  1.86955897, -1.47747941, -0.49292942,  1.38922409,
           -0.07618703])
    
    arr.sort() #排序
    
    arr
    
    array([-1.47747941, -1.19241559, -0.49292942, -0.07618703,  1.38922409,
            1.86955897])
    

    多维行列排序

    arr = np.random.randn(5,3)
    
    arr
    
    array([[-0.18284374, -1.92183403, -1.33962176],
           [-0.35250898,  0.83480866,  0.43398843],
           [ 0.04709364, -0.08379553, -0.09993549],
           [ 0.87922406,  1.75488263, -0.57784417],
           [-0.93287139, -0.11406265,  0.71589254]])
    
    arr.sort(1) #列
    
    arr
    
    array([[-1.92183403, -1.33962176, -0.18284374],
           [-0.35250898,  0.43398843,  0.83480866],
           [-0.09993549, -0.08379553,  0.04709364],
           [-0.57784417,  0.87922406,  1.75488263],
           [-0.93287139, -0.11406265,  0.71589254]])
    

    特定位置

    l_a = np.random.randn(1000)
    
    l_a.sort()
    
    l_a[int(0.05 * len(l_a))] #5%
    
    -1.7231654793957891
    

    独一无二

    names
    
    array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
    
    np.unique(names)
    
    array(['Bob', 'Joe', 'Will'], dtype='<U4')
    
    ints = np.array([3,3,3,2,2,1,1,4,4])
    
    np.unique(ints)
    
    array([1, 2, 3, 4])
    

    pure python
    sorted(set(names))

    检验

    values = np.array([6,0,0,3,2,5,6])
    
    np.in1d(values,[2,3,6]) #检验values中的值是否在后面的list中
    
    array([ True, False, False,  True,  True, False,  True])
    

    文件输入与输出

    arr = np.arange(10)
    
    np.save('/Users/meininghang/Desktop/s_a.npy',arr) #存储文件
    
    np.load('/Users/meininghang/Desktop/s_a.npy') #读取
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    np.savez('/Users/meininghang/Desktop/s_a.npz',a=arr,b=arr) #存储文件为.npz
    
    arch = np.load('/Users/meininghang/Desktop/s_a.npz') #读取文件
    
    arch['b']
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    arch['a']
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    np.savez_compressed('/Users/meininghang/Desktop/s_a2.npz',a = arr,b=arr) #存储为压缩文件
    

    线代

    x = np.array([[1,2,3],[4,5,6]])
    
    y = np.array([[6,23,],[-1,7],[8,9]])
    
    x
    
    array([[1, 2, 3],
           [4, 5, 6]])
    
    y
    
    array([[ 6, 23],
           [-1,  7],
           [ 8,  9]])
    
    x.dot(y)
    
    array([[ 28,  64],
           [ 67, 181]])
    
    np.dot(x,y) #等价
    
    array([[ 28,  64],
           [ 67, 181]])
    
    x @ np.ones(3) #特殊用法
    
    array([ 6., 15.])
    

    等价于np.dot(x,np.ones(3))

    np.ones(3)
    
    array([1., 1., 1.])
    

    其他

    from numpy.linalg import inv,qr
    
    X = np.random.randn(5,5)
    
    mat = X.T.dot(X)
    
    inv(mat) #取反
    
    array([[ 0.3153804 ,  0.06572043,  0.09295758, -0.05295502,  0.10039062],
           [ 0.06572043,  0.14119364,  0.0532526 , -0.04233026,  0.02636569],
           [ 0.09295758,  0.0532526 ,  0.41001573,  0.00487669,  0.00854835],
           [-0.05295502, -0.04233026,  0.00487669,  0.13017746, -0.01152687],
           [ 0.10039062,  0.02636569,  0.00854835, -0.01152687,  0.1324641 ]])
    
    mat.dot(inv(mat))
    
    array([[ 1.00000000e+00,  1.38314235e-18, -2.30089733e-17,
            -2.31625676e-18,  0.00000000e+00],
           [-4.36359852e-17,  1.00000000e+00,  1.24809158e-17,
            -6.64902607e-17, -2.77555756e-17],
           [ 4.67942395e-18,  1.06484372e-17,  1.00000000e+00,
             5.01064367e-19, -1.38777878e-17],
           [ 6.45223305e-17, -1.90564222e-17,  1.49753705e-17,
             1.00000000e+00,  2.77555756e-17],
           [ 0.00000000e+00, -5.55111512e-17,  1.38777878e-17,
             1.38777878e-17,  1.00000000e+00]])
    
    q,r = qr(mat)
    
    q
    
    array([[-0.78454422,  0.03130395, -0.09538485,  0.14529988,  0.59439053],
           [ 0.13807729, -0.9415313 , -0.09623544,  0.24662385,  0.15610539],
           [ 0.15150193,  0.11585421, -0.97871075, -0.05646742,  0.05061286],
           [-0.23236293, -0.28614052, -0.01989414, -0.92686434, -0.06824804],
           [ 0.53710302,  0.13130224,  0.15287236, -0.23621718,  0.78429048]])
    
    r
    
    array([[-6.32722241,  0.7479685 ,  1.57470985, -3.43964677,  8.30010971],
           [ 0.        , -9.16278613,  1.48489447, -5.03119444,  2.28135854],
           [ 0.        ,  0.        , -2.41505909,  0.05405567,  1.31462198],
           [ 0.        ,  0.        ,  0.        , -7.33442291, -2.42148731],
           [ 0.        ,  0.        ,  0.        ,  0.        ,  5.92077767]])
    

    random

    samples = np.random.normal(size=(4,4))
    
    samples
    
    array([[ 0.64867627,  0.53180508, -0.54734626, -0.45303299],
           [-0.03251697,  0.39046217,  1.98828937,  0.57414828],
           [-2.342141  , -0.16672602, -0.08988844, -1.1986827 ],
           [-1.52778635,  0.50663291, -1.06283763, -1.43748063]])
    
    np.random.seed(1234)
    
    rng = np.random.RandomState(1234) #isolate
    
    rng.randn(10)
    
    array([ 0.47143516, -1.19097569,  1.43270697, -0.3126519 , -0.72058873,
            0.88716294,  0.85958841, -0.6365235 ,  0.01569637, -2.24268495])
    

    随机漫步

    pure Python

    import random
    position = 0
    walk = [position]
    steps = 1000
    for  i in range(steps):
        step = 1 if random.randint(0,1) else -1
        position += step 
        walk.append(position)
    
    import matplotlib.pyplot as plt
    
    plt.plot(walk[:100])
    
    [<matplotlib.lines.Line2D at 0x112edb748>]
    

    [图片上传失败...(image-2e107-1517836465428)]

    nsteps = 1000
    draws = np.random.randint(0,2,size = nsteps)
    steps = np.where(draws>0,1,-1)
    walk = steps.cumsum()
    
    walk.min()
    
    -9
    
    walk.max()
    
    60
    
    (np.abs(walk) >= 10).argmax()
    
    297
    

    相关文章

      网友评论

        本文标题:numpy基础加强版

        本文链接:https://www.haomeiwen.com/subject/duoizxtx.html