美文网首页
【python】Numpy学习笔记

【python】Numpy学习笔记

作者: 南谛走心 | 来源:发表于2020-07-08 10:00 被阅读0次

    个人认为比较有用的地方

    1. 数组属性

    # Array properties

    a = np.array([[11, 12, 13, 14, 15],

                  [16, 17, 18, 19, 20],

                  [21, 22, 23, 24, 25],

                  [26, 27, 28 ,29, 30],

                  [31, 32, 33, 34, 35]])

    print(type(a)) # >>><class 'numpy.ndarray'>

    print(a.dtype) # >>>int64

    print(a.size) # >>>25

    print(a.shape) # >>>(5, 5)

    print(a.itemsize) # >>>8

    print(a.ndim) # >>>2

    print(a.nbytes) # >>>200

    2. 基本操作符

    # Basic Operators

    a = np.arange(25)

    a = a.reshape((5, 5))

    b = np.array([10, 62, 1, 14, 2, 56, 79, 2, 1, 45,

                  4, 92, 5, 55, 63, 43, 35, 6, 53, 24,

                  56, 3, 56, 44, 78])

    b = b.reshape((5,5))

    print(a + b)

    print(a - b)

    print(a * b)

    print(a / b)

    print(a ** 2)

    print(a < b)

    print(a > b)

    print(a.dot(b))

    3. 数组特殊运算符

    # dot, sum, min, max, cumsum

    a = np.arange(10)

    print(a.sum()) # >>>45

    print(a.min()) # >>>0

    print(a.max()) # >>>9

    print(a.cumsum()) # >>>[ 0  1  3  6 10 15 21 28 36 45]

    4. 花式索引

    # Fancy indexing

    a = np.arange(0, 100, 10)

    indices = [1, 5, -1]

    b = a[indices]

    print(a) # >>>[ 0 10 20 30 40 50 60 70 80 90]

    print(b) # >>>[10 50 90]

    5. 缺省索引

    # Incomplete Indexing

    a = np.arange(0, 100, 10)

    b = a[:5]

    c = a[a >= 50]

    print(b) # >>>[ 0 10 20 30 40]

    print(c) # >>>[50 60 70 80 90]

    6. Where 函数

    # Where

    a = np.arange(0, 100, 10)

    b = np.where(a < 50)

    c = np.where(a >= 50)[0]

    print(b) # >>>(array([0, 1, 2, 3, 4]),)

    print(c) # >>>[5 6 7 8 9]

    相关文章

      网友评论

          本文标题:【python】Numpy学习笔记

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