Python工具库NumPy

1 什么是NumPy

       python扩展库,主要用于处理高维度数组和矩阵运算。NumPy提供了非常强大的数字运功功能,其主要用于数据科学和机器学习领域。

1.1 Ndarray

       ndarray是用于存放同类型元素的多维数组。

1.1.1 array方法

示列:

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

在这里插入图片描述
       使用array方法,该方法参数有object就是具体的数组,dtype,copy,order,subok,ndmin,其含义如下,官方注解:

array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0)
    Create an array.
    Parameters
    ----------
    object : array_like
        An array, any object exposing the array interface, an object whose
        __array__ method returns an array, or any (nested) sequence.
    dtype : data-type, optional
        The desired data-type for the array.  If not given, then the type will
        be determined as the minimum type required to hold the objects in the
        sequence.
    copy : bool, optional 可以理解成值传递还是引用传递,默认就是复制值,所以是值传递。
        If true (default), then the object is copied.  Otherwise, a copy will
        only be made if __array__ returns a copy, if obj is a nested sequence,
        or if a copy is needed to satisfy any of the other requirements
        (`dtype`, `order`, etc.).
    order : {'K', 'A', 'C', 'F'}, optional
        Specify the memory layout of the array. If object is not an array, the
        newly created array will be in C order (row major) unless 'F' is
        specified, in which case it will be in Fortran order (column major).
        If object is an array the following holds.

        ===== ========= ===================================================
        order  no copy                     copy=True
        ===== ========= ===================================================
        'K'   unchanged F & C order preserved, otherwise most similar order
        'A'   unchanged F order if input is F and not C, otherwise C order
        'C'   C order   C order
        'F'   F order   F order
        ===== ========= ===================================================
        When ``copy=False`` and a copy is made for other reasons, the result is
        the same as if ``copy=True``, with some exceptions for `A`, see the
        Notes section. The default order is 'K'.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).
    ndmin : int, optional
        Specifies the minimum number of dimensions that the resulting
        array should have.  Ones will be pre-pended to the shape as
        needed to meet this requirement.

1.1.2 创建数组

       创建默认值为0的数组,示列:

arr1 = np.zeros([2, 2], dtype=int)
print(arr1)

创建默认值为0的数组
       创建默认值为1的数组,bool型1就是true,示例:

arr_default1 = np.ones([3, 4], dtype=bool, order="F")
print(arr_default1)

在这里插入图片描述

1.1.3 list,tuple转numpy array

       直接将list或者tuple传入asarray方法即可,代码示例:

arr = [[1, 2], [4, 5]]
arr0 = np.asarray(arr)
print(arr0)

在这里插入图片描述
未完-

相关推荐
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值