美文网首页
【Python | NumPy】基础

【Python | NumPy】基础

作者: 盐果儿 | 来源:发表于2022-08-12 05:20 被阅读0次

    1. 创建一个NumPy数组

    x = np.array([list])

    2. 访问数组属性

    x.ndim # the number of dimensions of the array

    x.size # the total number of elements of the array

    x.shape # returns a tuple of integers that indicate the number of elements stored along each dimension of the array.

    x.dtype # type of the ndarray

    3. 基本数组操作

    # 为NumPy数组添加元素4

    x = np.append(x, 4)

    # 删除NumPy数组中第一个元素

    x = np.delete(x, 0)

    # 将NumPy数组中元素排序

    x = np.sort(x)

    # 创建一个数组(注意"arange"只有一个"r")

    x = np.arange(2, 10, 3)

    4. Changing the shape

    # 将数组变为3行,2列的二维数组

    x = np.reshape(3, 2)

    # 将数组变为一维数组

    x = np.reshape(7)

    5. Indexing and slicing

    NumPy arrays can be indexed and sliced the same way that Python lists are.

    import numpy as np

    x = np.arange(1, 10)

    print(x)

    print(x[0:2])

    print(x[5:])

    print(x[:2])

    print(x[-3:])

    #打印特定条件下的数组

    print(x[x<4])

    print(x[(x > 5) & (x%2 == 0)])

    6. 数组计算

    # The sum of all elements

    x.sum()

    # Get the smallest / largest element

    x.min()

    x.max()

    # Multiply all elements by 2

    y = x * 2

    7. Statistics

    print(np.mean(x))

    print(np.median(x))

    print(np.var(x))

    print(np.std(x))

    相关文章

      网友评论

          本文标题:【Python | NumPy】基础

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