zoukankan      html  css  js  c++  java
  • python机器学期的常用库

    Numpy:
      1、np.array(): 生成一个数组
      2、a.shape: 判断段所生成数组的属性,(维度)还可以用shape对原数组的显示形式进行改变,
      3、c = a.reshape:会生成一个新的数组,但是和原数组是共享同一个内存空间的
      4、a.dtype : 获取数组的属性
      5、a.astype(np.int): 对数组的类型进行安全转换
      6、np.arange(1, 10, 0.5):类似Python的range
      7、np.linspace(1, 20, 21,endpoint=true): 指定起始值,终止值,和个数,系统会自动平均分配, endpoint默认包括终止值,可以创建等差数列
      8、d = np.logspace(1, 2, 10, endpoint=True,base=10):可以创建等比数列,默认base=10, 1表示10的1次方,2表示10的二次方,在10

    1

    和10

    2

    等比分成十个数
      9、a = np.fromstring(s, dtype=np.int8):可以从字节序列创建数组s = 'abcd'生成的a= [97 98 99 100]
      10、a = np.random.rand(10):从0~10之间随机取10个



    1.使用array创建

    通过array函数传递list对象
      L = [1, 2, 3, 4, 5, 6]
      print "L = ", L
      a = np.array(L)
      print "a = ", a
      print type(a)     # 此时a为 numpy的ndarray类型

     若传递的是多层嵌套的list,将创建多维数组
      b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
      print b  # 此时b为二维数组
    数组大小可以通过其shape属性获得
      print a.shape  # (6L,)

      print b.shape  #(3L, 4L)

    也可以强制修改shape
      b.shape = 4, 3
      print b
      # # # 注:从(3,4)改为(4,3)并不是对数组进行转置,而只是改变每个轴的大小,数组元素在内存中的位置并没有改变

     当某个轴为-1时,将根据数组元素的个数自动计算此轴的长度
      b.shape = 2, -1
      print b
      print b.shape
    b.shape = 3, 4
    使用reshape方法,可以创建改变了尺寸的新数组,原数组的shape保持不变,但是数组b和c共享内存,修改任意一个将影响另外一个,只能制定一个维度是-1
      c = b.reshape((4, -1))
      print "b = ", b
      print 'c = ', c

      b[0][1] = 20
      print "b = ", b
      print "c = ", c

      注:c,和 b指向的是同意内存空间的数组,因此修改b会影响c
    数组的元素类型可以通过dtype属性获得
      print a.dtype
      print b.dtype
    可以通过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
    如果更改元素类型,可以使用astype安全的转换
      f = d.astype(np.int)
      print f
    不要强制仅修改元素类型,如下面这句,将会以int来解释单精度float类型
      d.dtype = np.int
      print d

    2.使用函数创建
      如果生成一定规则的数据,可以使用NumPy提供的专门函数
      arange函数类似于python的range函数:指定起始值、终止值和步长来创建数组
      和Python的range类似,arange同样不包括终值;但arange可以生成浮点类型,而range只能是整数类型
        a = np.arange(1, 10, 0.5)
        print a
      函数通过指定起始值、终止值和元素个数来创建数组,缺省包括终止值
        b = np.linspace(1, 10, 10)
        print 'b = ', b
      可以通过endpoint关键字指定是否包括终值
        c = np.linspace(1, 10, 10, endpoint=False)
        print 'c = ', c
      和linspace类似,logspace可以创建等比数列
        下面函数创建起始值为10^1,终止值为10^2,有20个数的等比数列
        d = np.logspace(1, 2, 10, endpoint=True)
        print d
        下面创建起始值为2^0,终止值为2^10(包括),有10个数的等比数列
        f = np.logspace(0, 10, 11, endpoint=True, base=2)
        print f
      使用 frombuffer, fromstring, fromfile等函数可以从字节序列创建数组
        s = 'abcd'
        g = np.fromstring(s, dtype=np.int8)
        print g
    3、存取
      a = np.arange(10)
      和python的列表取法一样
      注意:b = a[2:5]
         b[0] = 200 ,此时a也会跟着更改
      
      
  • 相关阅读:
    Bayan 2015 Contest Warm Up D. CGCDSSQ 暴力
    Codeforces Round #361 (Div. 2) D. Friends and Subsequences RMQ+二分
    Educational Codeforces Round 21 D. Array Division 前缀和
    Educational Codeforces Round 23 E. Choosing The Commander Trie
    Educational Codeforces Round 23 D. Imbalanced Array 单调栈
    Codeforces Round #421 (Div. 1) B. Mister B and PR Shifts 模拟
    Educational Codeforces Round 24 E. Card Game Again 二分+线段树
    Educational Codeforces Round 25 E. Minimal Labels 优先队列
    Codeforces Round #426 (Div. 1) B. The Bakery DP+线段树
    Codeforces Round #407 (Div. 1) C. The Great Mixing 背包DP+Bitset
  • 原文地址:https://www.cnblogs.com/bianjing/p/9665531.html
Copyright © 2011-2022 走看看