在numpy中,主要使用np.array
函数来创建数组,这个函数要完全应用起来还是比较复杂的,今天主要介绍其中经常使用到的三个参数p_object
、dtype
、ndmin
。后续会把剩余的三个参数也会进行说明。
1.函数定义
def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0): # real signature unknown; restored from __doc__
"""
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.
Returns
-------
out : ndarray
An array object satisfying the specified requirements.
Notes
-----
When order is 'A' and `object` is an array in neither 'C' nor 'F' order,
and a copy is forced by a change in dtype, then the order of the result is
not necessarily 'C' as expected. This is likely a bug.
主要参数说明:
-
p_object
用户传递创建数组的数据,一般都是python中的list格式。可以是一维、二维、多维都可以支持。 -
dtype
代表array元素的实际数据类型,基础数据类型,类似:int32
、float64
。如果不指定数据类型,numpy会自动判断出能够包含所有元素的最小空间范围的数据类型。 -
ndim
空间维度,可以手动指定空间维度。主要用在某些高维度情况,本身数据低维度,numpy会自动填充对应的维度。
为什么numpy重新定义一套数据类型?
我们可以看到numpy中的ndarray和python自带的数据类型list是形式上比较类似的,但是在数据类型上面存在很多的差异。python是一切皆对象,占用空间多,多维list的时候,嵌套引用,效率比较差。numpy为了提高效率,把常规语言中的基础数据类型引入进来,这样占用的空间更少,效率更高。
2.常见案例
2.1 只使用p_object
的情况
import numpy as np
# 使用numpy创建一维数组
a = np.array([1, 2, 3])
print(a)
print(type(a))
print(a.dtype)
print('--' * 20)
# 使用numpy创建二位数组
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(b)
print(type(b))
print(b.dtype)
print('--' * 20)
# 使用numpy创建三维数组
c = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
print(c)
print(type(c))
print(c.dtype)
print('--' * 20)
运行效果:
[1 2 3]
<class 'numpy.ndarray'>
int32
----------------------------------------
[[1 2 3]
[4 5 6]
[7 8 9]]
<class 'numpy.ndarray'>
int32
----------------------------------------
[[[1 2 3]
[4 5 6]
[7 8 9]]]
<class 'numpy.ndarray'>
int32
----------------------------------------
程序说明:采用最基本常规的方式创建,多维list都可以识别,都转化成对应的ndarray。
2.2 使用dtype
的情况
import numpy as np
# 不指定dtype的情况
n = np.array([1, 2, 3])
print(n)
print(n.dtype)
print('--' * 20)
# 指定dtype超出当前数据类型范围的情况
f = np.array([1, 2, 3], dtype=float)
print(f)
print(f.dtype)
print('--' * 20)
# 指定dtype小于当前数据类型范围的情况
j = np.array([1.0, 2.0, 3.0], dtype=int)
print(j)
print(j.dtype)
print('--' * 20)
运行效果:
[1 2 3]
int32
----------------------------------------
[1. 2. 3.]
float64
----------------------------------------
[1 2 3]
int32
----------------------------------------
程序说明:
-
第一组没有指定
dtype
的时候,numpy自动判断能够包含[1 2 3]
的数据类型是int
,所以设置dtype=int32 -
第二组和第三组指定的dtype和实际数据是不匹配的,最后的结果都是和指定dtype一致,说明:当指定dtype的时候,不符合类型范围的数据会进行数据类型转换
2.3 使用ndim
的情况
import numpy as np
# 不指定dim的情况
n = np.array([[1, 2, 3], [4, 5, 6]])
print(n)
print("变量n,维度为:{0}".format(n.ndim))
print('--' * 20)
# 指定的ndim超出参数的维度
f = np.array([[1, 2, 3], [4, 5, 6]], ndmin=3)
print(f)
print("变量f,维度为:{0}".format(n.ndim))
print('--' * 20)
# 指定的ndim小于参数的维度
j = np.array([[1, 2, 3], [4, 5, 6]], ndmin=1)
print(j)
print("变量j,维度为:{0}".format(n.ndim))
print('--' * 20)
运行效果:
[[1 2 3]
[4 5 6]]
变量n,维度为:2
----------------------------------------
[[[1 2 3]
[4 5 6]]]
变量f,维度为:3
----------------------------------------
[[1 2 3]
[4 5 6]]
变量j,维度为:2
----------------------------------------
程序说明:
- 第一组不指定ndim参数的时候,numpy自动识别维度
- 第二组当指定的维度大于数据本身的维度,numpy会自动给传递的参数进行增维操作,达到指定的维度。
- 第三组指定的维度小于数据本身的维度,这个时候没有没有办法给参数降维度,因为不确定去掉那一组数据,于是就不处理这种情况。
总结:ndim
参数只在指定维度大于数据实际维度的情况才生效,其余情况不剩下。
备注:
更多精彩博客,请访问:灭bug网-消灭bug
对应视频教程,请访问:python400
完整markdown笔记,请访问: python400_learn_github