美文网首页
Python3: 数组

Python3: 数组

作者: LET149 | 来源:发表于2023-10-11 09:06 被阅读0次

    1. 生成随机整数数组

    numpy.random.randint(number, high=, size=())
    number: 生成数组所用随机整数的下限,包含
    high=: 生成数组所有随机整数的上限,不包含
    size=: 数组的维度,依次为 , 等;数组可以有 一维二维三维

    >>> import numpy as np
    >>> 
    >>> 
    >>> kk = np.random.randint(3,10,size=(5,6))
    >>> kk
    array([[6, 7, 6, 4, 3, 4],
           [3, 4, 9, 9, 8, 8],
           [6, 6, 8, 7, 9, 3],
           [5, 8, 9, 3, 7, 3],
           [3, 9, 6, 7, 5, 7]])
    >>> 
    >>> type(kk)
    <class 'numpy.ndarray'>
    >>> 
    >>> kk = np.random.randint(3,10,size=(2,3,4))
    >>> kk
    array([[[6, 6, 9, 4],
            [3, 6, 4, 6],
            [4, 4, 3, 6]],
    
           [[6, 7, 4, 5],
            [6, 8, 4, 3],
            [4, 8, 3, 4]]])
    

    2. 查看数组维度

    numpy.shape(array_name) : 调用numpy中的shape()函数来查看数组对象的维度属性
    array_name.shape : 调用numpy数组对象的维度属性

    >>> import numpy as np
    >>> 
    >>> 
    >>> kk = np.random.randint(3,10,size=(5,6))
    >>> np.shape(kk)
    (5, 6)
    >>> kk.shape
    (5, 6)
    >>> 
    >>> kk = np.random.randint(3,10,size=(2,3,4))
    >>> np.shape(kk)
    (2, 3, 4)
    >>> kk.shape
    (2, 3, 4)
    

    相关文章

      网友评论

          本文标题:Python3: 数组

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