首页 > 其他 > 详细

numpy的使用

时间:2020-08-03 23:09:26      阅读:72      评论:0      收藏:0      [点我收藏+]

numpy的使用

一、创建ndarray

1. 使用np.array()由python list创建

注意:

  • numpy默认ndarray的所有元素的类型是相同的
  • 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
import numpy as np

l = [1,2,3,4,5,6,7]
print(type(l))  # list

nd = np.array(l)
print(type(nd))  # numpy.ndarray
  1. 求和

    nd.sum()
    
  2. 求均方差

    nd.var()
    
  3. 求标准差

    nd.std()
    
  4. 随机数生成

    x = np.arange(0,100000,1)
    print(x)  # array([    0,     1,     2, ..., 99997, 99998, 99999])
    

2. 使用np的routines函数创建

  1. np.ones(shape, dtype=None, order=‘C‘)  # shape 形状 dtype 数据类型
    
    x = np.ones(shape = (5,5),dtype=np.int8)
    print(x)
    # array([[1, 1, 1, 1, 1],
    #       [1, 1, 1, 1, 1],
    #       [1, 1, 1, 1, 1],
    #       [1, 1, 1, 1, 1],
    #       [1, 1, 1, 1, 1]], dtype=int8)
    
  2. np.zeros(shape, dtype=float, order=‘C‘)
    
    x = np.zeros(shape = (2,3,4),dtype=np.float16)
    print(x)
    #array([[[0., 0., 0., 0.],
    #        [0., 0., 0., 0.],
    #        [0., 0., 0., 0.]],
    #
    #       [[0., 0., 0., 0.],
    #        [0., 0., 0., 0.],
    #        [0., 0., 0., 0.]]], dtype=float16)
    
  3. np.full(shape, fill_value, dtype=None, order=‘C‘)
    
    x = np.full(shape = (3,5),fill_value=3.14)
    print(x)
    #array([[3.14, 3.14, 3.14, 3.14, 3.14],
    #       [3.14, 3.14, 3.14, 3.14, 3.14],
    #       [3.14, 3.14, 3.14, 3.14, 3.14]])
    
  4. np.eye(N, M=None, k=0, dtype=float)  
    # 对角线为1其他的位置为0
    
    # 单位矩阵
    x = np.eye(N = 5)
    print(x)
    #array([[1., 0., 0., 0., 0.],
    #       [0., 1., 0., 0., 0.],
    #       [0., 0., 1., 0., 0.],
    #       [0., 0., 0., 1., 0.],
    #       [0., 0., 0., 0., 1.]])
    
  5. np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
    
    # 等差数列
    np.linspace(0,100,num = 21)
    #array([  0.,   5.,  10.,  15.,  20.,  25.,  30.,  35.,  40.,  45.,  50.,
    #        55.,  60.,  65.,  70.,  75.,  80.,  85.,  90.,  95., 100.])
    
  6. np.arange([start, ]stop, [step, ]dtype=None)
    
    np.arange(0,100,3)
    #array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48,
    #       51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99])
    
  7. np.random.randint(low, high=None, size=None, dtype=‘l‘)
    
    np.random.randint(0,100,size = (5,5))
    #array([[ 4, 45, 66, 55, 42],
    #       [28,  5, 75, 71, 44],
    #       [ 0, 17, 89, 81, 66],
    #       [95, 96, 89, 31,  6],
    #       [75, 31, 51, 81, 38]])
    
  8. np.random.randn(d0, d1, ..., dn)  
    # 标准正态分布
    
    # normal 正常,正太
    # dimession 维度
    # 平均值是0,方差是1
    np.random.randn(4,5)
    #array([[-1.26185332,  0.29715466,  0.52047771, -1.55183841, -0.83663771],
    #       [ 0.40776138, -0.7380327 ,  0.22623508,  1.12275365, -0.38189704],
    #       [ 0.67816239,  0.91695635,  0.13487838,  0.13769114,  0.68426452],
    #       [ 0.00935704,  0.49087787, -0.34920945,  0.15688878, -0.98320155]])
    
  9. np.random.normal(loc=0.0, scale=1.0, size=None)
    
    np.random.normal(loc = 175,scale=10,size = 10000).round(2)
    #array([180.02, 162.26, 184.18, ..., 179.19, 182.83, 174.51])
    
  10. np.random.random(size=None)  
    # 生成0到1的随机数,左闭右开
    
    np.random.random(10)
    #array([0.80403643, 0.60631454, 0.22301424, 0.03813725, 0.14537585,
    #       0.00946211, 0.39063408, 0.5558176 , 0.39426771, 0.74874309])
    

二、ndarray的属性

4个必记参数:

  1. ndim:维度
  2. shape:形状(各维度的长度)
  3. size:总长度
  4. dtype:元素类型

三、ndarray的基本操作

1. 索引

一维与列表完全一致,多维时同理

nd2 = np.random.randint(0,150,size = (4,5))
print(nd2)
#array([[124,  91,  52,  23,  16],
#       [122, 106, 143,  88,  85],
#       [100,   0, 141, 101,  72],
#       [ 26,  93, 123,   4,  31]])

nd2[1,1]  # 106
nd2[2]  # array([100,   0, 141, 101,  72])

2. 切片

一维与列表完全一致,多维时同理

nd2
array([[124,  91,  52,  23,  16],
       [122, 106, 143,  88,  85],
       [100,   0, 141, 101,  72],
       [ 26,  93, 123,   4,  31]])      
nd2[0:3]
array([[124,  91,  52,  23,  16],
       [122, 106, 143,  88,  85],
       [100,   0, 141, 101,  72]])
nd2[-2:]
array([[100,   0, 141, 101,  72],
       [ 26,  93, 123,   4,  31]])
nd2[0:3,0:3]
array([[124,  91,  52],
       [122, 106, 143],
       [100,   0, 141]])
  • 将数据反转,例如[1,2,3]---->[3,2,1]

    nd3 = nd[:10]
    nd3
    array([189.04, 166.26, 172.39, 172.1 , 173.  , 176.82, 176.  , 177.74,
           162.46, 176.13])
    nd3[::-1]
    array([176.13, 162.46, 177.74, 176.  , 176.82, 173.  , 172.1 , 172.39,
           166.26, 189.04])
    ## 两个::进行切片
    

3. 变形

使用reshape函数,注意参数是一个tuple!

nd2
array([[124,  91,  52,  23,  16],
       [122, 106, 143,  88,  85],
       [100,   0, 141, 101,  72],
       [ 26,  93, 123,   4,  31]])
nd2.reshape(2,10)
array([[124,  91,  52,  23,  16, 122, 106, 143,  88,  85],
       [100,   0, 141, 101,  72,  26,  93, 123,   4,  31]])
nd2.reshape(5,4)
array([[124,  91,  52,  23],
       [ 16, 122, 106, 143],
       [ 88,  85, 100,   0],
       [141, 101,  72,  26],
       [ 93, 123,   4,  31]])

numpy的使用

原文:https://www.cnblogs.com/techoc/p/13428182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!