zoukankan      html  css  js  c++  java
  • Python3玩转儿 机器学习(5)

    numpy 的使用

    numpy.array基础

    import numpy
    
    numpy.__version__   #查询当前numpy的版本
    
    '1.14.0'
    
    import numpy as np
    
    np.__version__
    
    '1.14.0'
    

    Python List 特点

    L = [i for i in range(10)]
    L
    
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    L[5]
    
    5
    
    L[5] = 20
    L
    
    [0, 1, 2, 3, 4, 20, 6, 7, 8, 9]
    
    L[5] = 'hello'
    L
    
    [0, 1, 2, 3, 4, 'hello', 6, 7, 8, 9]
    

    Python List 中的List是对元素类型没有进行限制的。也就是说什么类型都是可以赋值进去的。这样使得Python中List是非常灵活的,但是也导致了List的效率是比较低的。因为对于每个元素都必须去查找对应的元素类型。Python中array模块是对元素类型有限制的。

    Python array模块的特点

    import array
    
    arr = array.array('i',[i for i in range(10)])
    arr
    
    array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    arr[5]
    
    5
    
    arr[5] = 100
    arr
    
    array('i', [0, 1, 2, 3, 4, 100, 6, 7, 8, 9])
    
    arr[5]= 'hello'  #限定类型
    
    ---------------------------------------------------------------------------
    
    TypeError                                 Traceback (most recent call last)
    
    <ipython-input-19-53429961e592> in <module>()
    ----> 1 arr[5]= 'hello'
    
    
    TypeError: an integer is required (got type str)
    

    类型限定,但是效率比List更高,但是只是把数据当成数组来看,并没有将数据当作矩阵来看,所以不适合在大数据和人工智能上使用。

    Python中 numpy.array的使用

    nparr = np.array([i for i in range(10)])
    nparr
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    nparr[5]
    
    5
    
    nparr[5] = 100
    nparr
    
    array([  0,   1,   2,   3,   4, 100,   6,   7,   8,   9])
    

    numpy会对类型进行限制

    nparr[5] = 'sdfsd'    #类型限制
    nparr
    
    ---------------------------------------------------------------------------
    
    ValueError                                Traceback (most recent call last)
    
    <ipython-input-25-011f26deb6f3> in <module>()
    ----> 1 nparr[5] = 'sdfsd'
          2 nparr
    
    
    ValueError: invalid literal for int() with base 10: 'sdfsd'
    
    查看类型
    nparr.dtype   #数据类型
    
    dtype('int32')
    
    nparr[5] = 5.0
    nparr
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    进行了一次隐式的类型转换

    其他创建 numpy.array的方法

    创建由10个整数0组成的 int 矩阵
    np.zeros(10,dtype=int)
    
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    
    创建一个3行5列全部由0组成的 float 矩阵
    np.zeros((3,5),dtype=float)
    
    array([[0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.]])
    
    创建一个3行5列全为1的 float 矩阵
    np.ones((3,5),dtype=float)
    
    array([[1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]])
    
    创建一个指定值的矩阵
    np.full(shape=(3,5),fill_value=666.0)
    
    array([[666., 666., 666., 666., 666.],
           [666., 666., 666., 666., 666.],
           [666., 666., 666., 666., 666.]])
    
    numpy.arange方法
    [i for i in range(0,20,2)]   #0至20 步长为2的数组
    
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    
    0至20 步长为2的数组
    np.arange(0,20,2)
    
    array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
    
    numpy.linspace方法
    np.linspace(0,20,10)
    
    array([ 0.        ,  2.22222222,  4.44444444,  6.66666667,  8.88888889,
           11.11111111, 13.33333333, 15.55555556, 17.77777778, 20.        ])
    
    np.linspace(0,20,11)
    
    array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18., 20.])
    

    0至20 等长步长的一共11个元素,包含0也包含20

    numpy.random 方法
    np.random.randint(0,10)
    
    7
    
    np.random.randint(4,8,size=(3,5))
    
    array([[6, 4, 4, 4, 4],
           [6, 4, 5, 6, 7],
           [4, 7, 5, 7, 7]])
    

    生成一个随机值在4至8之间的 3行5列的矩阵

    np.random.seed(666)
    np.random.randint(4,8,size=(3,5))
    
    array([[4, 6, 5, 6, 6],
           [6, 5, 6, 4, 5],
           [7, 6, 7, 4, 7]])
    

    设置一个随机数种子

    np.random.random()
    
    0.2811684913927954
    

    生成一个0--1之间的随机数

    np.random.random((3,5))
    
    array([[0.46284169, 0.23340091, 0.76706421, 0.81995656, 0.39747625],
           [0.31644109, 0.15551206, 0.73460987, 0.73159555, 0.8578588 ],
           [0.76741234, 0.95323137, 0.29097383, 0.84778197, 0.3497619 ]])
    

    生成一个3行5列的随机数矩阵

    np.random.normal()
    
    -0.21326813235544162
    

    生成一个符合均值为0,方差为1分布的随机数

    np.random.normal(10,100)
    
    54.07669166918434
    

    生成一个符合均值为10,方差为100分布的随机数

    np.random.normal(0,1,size = (3,5))
    
    array([[ 0.69339587,  0.03820097, -0.18592982, -0.35371521, -1.95332994],
           [-0.34376486, -1.47693162, -0.70022971,  0.77605168,  1.18063598],
           [ 0.06102404,  1.07856138, -0.79783572,  1.1701326 ,  0.1121217 ]])
    

    生成一个符合均值为0,方差为1分布的 3行5列的随机数

    当对方法不清楚的时候可以使用 方法?的格式查询使用方法

    np.random.normal?
    

    help()使用

    help(np.random)
    
  • 相关阅读:
    LeetCode 227. Basic Calculator II
    LeetCode 224. Basic Calculator
    LeetCode 103. Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 169. Majority Element
    LeetCode 145. Binary Tree Postorder Traversal
    LeetCode 94. Binary Tree Inorder Traversal
    LeetCode 144. Binary Tree Preorder Traversal
  • 原文地址:https://www.cnblogs.com/taoke2016/p/8986328.html
Copyright © 2011-2022 走看看