美文网首页
numpy基本使用

numpy基本使用

作者: 小吉头 | 来源:发表于2020-12-15 16:30 被阅读0次

    数组的创建

    import numpy as np
    
    t1 = np.array([1,2,3])
    print(t1,type(t1))
    >>>[1 2 3] <class 'numpy.ndarray'>
    
    t2 = np.array(range(10))
    print(t2,type(t2))
    >>>[0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>
    
    t3 = np.arange(10)#跟range用法一样,np.arrage(1,10,2)
    print(t3,type(t3))
    >>>[0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>
    

    numpy中常见的数据类型:



    查看数据类型:

    t1 = np.array(range(1,6))
    print(t1)
    print(t1.dtype)
    >>>[1 2 3 4 5]
    >>>int32
    

    指定数据类型:

    t1 = np.array(range(1,6),dtype=float) #方式二:dtype="int8"   #方式三:dtype="i1"
    print(t1)
    print(t1.dtype)
    >>>[1. 2. 3. 4. 5.]
    >>>float64
    
    #布尔类型
    t1 = np.array([1,0,1,1],dtype=bool)
    print(t1)
    print(t1.dtype)
    >>>[ True False  True  True]
    >>>bool
    

    修改数据类型:

    t1 = np.array([1,0,1,1],dtype=bool)
    print(t1)
    print(t1.dtype)
    >>>[ True False  True  True]
    >>>bool
    
    t2 = t1.astype("int8")
    print(t2)
    print(t2.dtype)
    >>>[1 0 1 1]
    >>>int8
    

    修改浮点型保留小数位数:

    t1 = np.array([random.random() for i in range(3)])
    print(t1)
    print(t1.dtype)
    >>>[0.63296597 0.04263672 0.92288572]
    >>>float64
    
    t2 = np.round(t1,2)
    print(t2)
    >>>[0.63 0.04 0.92]
    

    数组的形状

    #一维数组
    t1 = np.arange(5)
    print(t1)
    print(t1.shape)
    >>>[0 1 2 3 4]
    >>>(5,)
    
    t2 = np.array([[1,2,3],[4,5,6]])
    print(t2)
    print(t2.shape)
    >>>[[1 2 3]
     [4 5 6]]
    >>>(2, 3)
    

    修改数组形状,reshape()方法不会改变数组本身,会返回一个新的数组。reshape((12,1))或者reshape(12,1)都行,官方文档使用的是元祖

    #一维数组
    t1 = np.arange(12)
    print(t1)
    print(t1.shape)
    >>>[ 0  1  2  3  4  5  6  7  8  9 10 11]
    >>>(12,)
    
    #t1修改成二维数组,12行1列
    t2 = t1.reshape(12,1)
    print(t2)
    >>>
    [[ 0]
     [ 1]
     [ 2]
     [ 3]
     [ 4]
     [ 5]
     [ 6]
     [ 7]
     [ 8]
     [ 9]
     [10]
     [11]]
    
    #t1修改成二维数组,1行12列
    t3 = t1.reshape(1,12)
    print(t3)
    >>>[[ 0  1  2  3  4  5  6  7  8  9 10 11]]
    
    #t1修改成二维数组3行4列
    t4 = t1.reshape((3,4))
    print(t4)
    >>>[[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    
    #t1修改成二维数组3行5列报错
    t5 = t1.reshape((3,5))
    print(t5)
    >>>Traceback (most recent call last):
      File "test.py", line 12, in <module>
        t3 = t1.reshape((3,5))
    ValueError: cannot reshape array of size 12 into shape (3,5)
    
    #t4修改成一维数组
    t6 = t4.reshape((t4.shape[0]*t4.shape[1],))
    print(t6)
    >>>[ 0  1  2  3  4  5  6  7  8  9 10 11]
    
    #t4修改成一位数组,简便方法
    t7 = t4.flatten()
    print(t7)
    >>>[ 0  1  2  3  4  5  6  7  8  9 10 11]
    

    数组的计算

    t1 = np.arange(12).reshape(3,4)
    print(t1)
    >>>
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    
    #每个元素都+10
    t2 = t1+10
    print(t2)
    >>>
    [[10 11 12 13]
     [14 15 16 17]
     [18 19 20 21]]
    
    #每个元素都除以0,0理解成非常小的数
    t3 = t1/0
    print(t3)
    >>>
    [[nan inf inf inf]
     [inf inf inf inf]
     [inf inf inf inf]]
    
    #相同形状的数组相加,即对应位置的元素相加
    t4 = np.arange(12,24).reshape(3,4)
    print(t4)
    >>>
    [[12 13 14 15]
     [16 17 18 19]
     [20 21 22 23]]
    t5 = t1 + t4
    print(t5)
    >>>
    [[12 14 16 18]
     [20 22 24 26]
     [28 30 32 34]]
    

    nan表示不是一个数字not a number
    inf表示无穷大infinity

    #形状不完全相同计算,要么列形状相同,要么行形状相同
    t1 = np.arange(12).reshape(3,4)
    print(t1)
    >>>
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    #含有4个元素的一维数组
    t2 = np.array([1,2,3,4]) #t2 = np.array([1,2,3,4]).reshape(1,4) 和一维数组效果一样
    print(t2)
    >>>
    [1 2 3 4]
    
    #t1每行和t2的列数量相同,列对应元素 做减运算
    print(t1-t2)
    >>>
    [[-1 -1 -1 -1]
     [ 3  3  3  3]
     [ 7  7  7  7]]
    
    #3行1列
    t3 = np.array([1,2,3]).reshape(3,1)
    print(t3)
    >>>
    [[1]
     [2]
     [3]]
    #t1每列和t3行数量相同, 行对应元素 做减运算
    print(t1-t3)
    >>>
    [[-1  0  1  2]
     [ 2  3  4  5]
     [ 5  6  7  8]]
    
    #形状完全不同计算报错,行列形状都不同
    t4 = np.array([1,2,3,4,5])
    print(t1-t4)
    >>>
    ValueError: operands could not be broadcast together with shapes (3,4) (5,) 
    

    相关文章

      网友评论

          本文标题:numpy基本使用

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