• numpy——使用array创建(1)


     标准Python的列表(list)中,元素本质是对象。
     如:L = [1, 2, 3],需要3个指针和三个整数对象,对于数值运算比较浪费内存和CPU。
     因此,Numpy提供了ndarray(N-dimensional array object)对象:存储单一数据类型的多维数组。
    tips:np.set_printoptions(linewidth = 200)//设置在控制台显示的数据长度,更美观
    # # # 通过array函数传递list对象
         L = [1, 2, 3, 4, 5, 6]
         print "L = ", L
         a = np.array(L)
         print "a = ", a           # # >>array([1, 2, 3, 4, 5, 6])
         print type(a),type(L)     # # >><class 'numpy.ndarray'> <class 'list'>
    # # # 若传递的是多层嵌套的list,将创建多维数组
         b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
         print b
        
    # # # # 数组大小(即长宽)可以通过其shape属性获得
         print a.shape
         print b.shape     #3,4
       
    # # # 也可以强制修改shape
         b.shape = 4, 3
         print b
        ##>> array([[ 1,  2,  3],
                        # [ 4,  5,  6],
                        # [ 7,  8,  9],
                        # [10, 11, 12]])
        # # # # 注:从(3,4)改为(4,3)并不是对数组进行转置,而只是改变每个轴的大小,数组元素在内存中的位置并没有改变
        # # #
    # # # 当某个轴为-1时,将根据数组元素的个数自动计算此轴的长度
         b.shape = 2, -1
         print b
         print b.shape
          # #>>array([[ 1,  2,  3,  4,  5,  6],
          ##          [ 7,  8,  9, 10, 11, 12]]) 
        
          # # # 使用reshape方法,可以创建改变了尺寸的新数组,原数组的shape保持不变
         c = b.reshape((4, -1))
         print "b = 
    ", b 
         #>> b = array([[ 1,  2,  3,  4,  5,  6],
         #             [ 7,  8,  9, 10, 11, 12]])
         print 'c = 
    ', c
        # #>>c = array([[ 1,  2,  3],
            #              [ 4,  5,  6],
            #              [ 7,  8,  9],
            #              [10, 11, 12]])
        # #
    
    # # # 数组b和c共享内存,修改任意一个将影响另外一个 b[0][1] = 20 print "b = ", b #>>b = array([[ 1, 20, 3, 4, 5, 6], # [ 7, 8, 9, 10, 11, 12]]) print "c = ", c # >> c = array([[ 1, 2, 3], # [ 4, 5, 6], # [ 7, 8, 9], # [10, 11, 12]])
    # # # 数组的元素类型可以通过dtype属性获得 print a.dtype print b.dtype #>> dtype('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 #>>array([[ 1., 2., 3., 4.], # [ 5., 6., 7., 8.], # [ 9., 10., 11., 12.]]) print f #>>array([[ 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 # #

    # # # 但不要强制仅修改元素类型,如下面这句,将会以int来解释单精度float类型 d.dtype = np.int print d     #>>array([[ 0, 1072693248, 0, 1073741824, 0, 1074266112, 0, 1074790400],     # [ 0, 1075052544, 0, 1075314688, 0, 1075576832, 0, 1075838976],     # [ 0, 1075970048, 0, 1076101120, 0, 1076232192, 0, 1076363264]]) #
    
    
  • 相关阅读:
    bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】
    bzoj 1627: [Usaco2007 Dec]穿越泥地【bfs】
    bzoj 1596: [Usaco2008 Jan]电话网络【贪心】
    bzoj 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路【Floyd】
    bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】
    bzoj 1639: [Usaco2007 Mar]Monthly Expense 月度开支【二分】
    bzoj 1708: [Usaco2007 Oct]Money奶牛的硬币
    bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】
    bzoj 1576: [Usaco2009 Jan]安全路经Travel【spfa+树链剖分+线段树】
    bzoj 1592: [Usaco2008 Feb]Making the Grade 路面修整【dp】
  • 原文地址:https://www.cnblogs.com/Theo-sblogs/p/12263547.html
Copyright © 2020-2023  润新知