zoukankan      html  css  js  c++  java
  • 机器学习 —— python库 —— 使用array创建

    python库

    Pip

    安装python包的推荐工具

    Numpy

    为python提供快速的多维数组处理能力

    Scipy

    在Numpy基础上添加了众多科学计算工具包

    Matplotlib

    python丰富的绘图库

    数据生成示例

    import numpy as np
    
    a=np.arange(0,60,10).reshape((-1,1))+np.arange(6)
    print(a)
    
    [[ 0  1  2  3  4  5]
     [10 11 12 13 14 15]
     [20 21 22 23 24 25]
     [30 31 32 33 34 35]
     [40 41 42 43 44 45]
     [50 51 52 53 54 55]]
    

    代码解释

    arange(0,60,10)
    

    arange给定一个从0开始到60结束以10作为间距的范围

    [0,10,20,30,40,50]
    
    reshape((-1,1)
    

    自动匹配行数之后转置为列

    [[0,]
    [10,]
    [20,]
    [30,]
    [40,]
    [50]]
    
    arange(6)
    

    没有给起点值,默认为0

    [0,1,2,3,4,5]
    

    相加之后

    [[ 0  1  2  3  4  5]
     [10 11 12 13 14 15]
     [20 21 22 23 24 25]
     [30 31 32 33 34 35]
     [40 41 42 43 44 45]
     [50 51 52 53 54 55]]
    

    代码演示

    1.使用array创建

    通过array函数传递list对象

        L = [1, 2, 3, 4, 5, 6]
        print("L = ", L)
        a = np.array(L)
        print("a = ", a)
        print(type(a))
    

    L是一个列表类型,如果直接打印L的话会显示一个列表,通过np.array函数可以将L列表转换为ndarray形式的行向量。

    L =  [1, 2, 3, 4, 5, 6]
    a =  [1 2 3 4 5 6]
    <class 'numpy.ndarray'>
    

    若传递的是多层嵌套的list,将创建多维数组

        b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
        print(b)
    

    如果将多个列表传入np.array函数,将被转换为二维的矩阵。

    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    

    数组大小可以通过其shape属性获得

        print (a.shape)
        print (b.shape)
    

    shape可以返回一个矩阵的大小。

    (6,)
    (3, 4)
    

    也可以强制修改shape

        b.shape = 4, 3
        print(b)
    

    通过直接对shape赋值可以修改矩阵的大小。

    [[ 1  2  3]
     [ 4  5  6]
     [ 7  8  9]
     [10 11 12]]
    

    注:从(3,4)改为(4,3)并不是对数组进行转置,而只是改变每个轴的大小,数组元素在内存中的位置并没有改变。

        b.shape = 2, -1
        print (b)
        print (b.shape)
    

    当某个轴为-1时,将根据数组元素的个数自动计算此轴的长度。

    [[ 1  2  3  4  5  6]
     [ 7  8  9 10 11 12]]
    (2, 6)
    

    reshape方法

        c = b.reshape((4, -1))
        print ("b = \n", b)
        print ('c = \n', c)
    

    reshape可以创建改变了尺寸的新数组,原数组的shape保持不变。

    b = 
     [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    c = 
     [[ 1  2  3]
     [ 4  5  6]
     [ 7  8  9]
     [10 11 12]]
    
        b[0][1] = 20
        print ("b = \n", b)
        print ("c = \n", c)
    

    数组b和c共享内存,修改任意一个将影响另外一个。

    b = 
     [[ 1 20  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    c = 
     [[ 1 20  3]
     [ 4  5  6]
     [ 7  8  9]
     [10 11 12]]
    

    数组的元素类型可以通过dtype属性获得

        print (a.dtype)
        print (b.dtype)
    
    int32
    int32
    

    可以通过dtype参数在创建时指定元素类型

        d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.float)
        f = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.complex)
        print (d)
        print (f)
    

    分别设置为浮点型和复数型。

    [[ 1.  2.  3.  4.]
     [ 5.  6.  7.  8.]
     [ 9. 10. 11. 12.]]
    [[ 1.+0.j  2.+0.j  3.+0.j  4.+0.j]
     [ 5.+0.j  6.+0.j  7.+0.j  8.+0.j]
     [ 9.+0.j 10.+0.j 11.+0.j 12.+0.j]]
    

    如果更改元素类型,可以使用astype安全的转换

        f = d.astype(np.int)
        print (f)
    
    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    

    但不要强制仅修改元素类型

        d.dtype = np.int
        print (d)
    

    将会以int来解释单精度float类型。

    [[         0 1072693248          0 1073741824          0 1074266112           0 1074790400]
     [         0 1075052544          0 1075314688          0 1075576832           0 1075838976]
     [         0 1075970048          0 1076101120          0 1076232192           0 1076363264]]
    
  • 相关阅读:
    eclipse设置字体大小
    如何利用服务器下发的Cookie实现基于此Cookie的会话保持
    Docker学习笔记_安装和使用Python
    Docker学习笔记_安装和使用mysql
    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/images/json: dial unix /var/run/docker.sock: conne
    Ubuntu18.04创建新的系统用户
    Docker学习笔记_安装和使用nginx
    Docker安装和使用Tomcat
    虚拟机ubuntu18.04设置静态IP
    我学习参考的网址
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338511.html
Copyright © 2011-2022 走看看