概述
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)
创建默认值为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)
未完-
最后
以上就是老迟到大象为你收集整理的Python工具库NumPy1 什么是NumPy的全部内容,希望文章能够帮你解决Python工具库NumPy1 什么是NumPy所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复