numpy数组创建
numpy 提供的最重要的数据结构是ndarray,它是 python 中list的扩展。
创建
-
array
array(p_object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)# 创建一维数组
a = np.array([0, 1, 2, 3, 4])
b = np.array((0, 1, 2, 3, 4))
print(a, type(a))
# [0 1 2 3 4] <class ‘numpy.ndarray’>
print(b, type(b))
# [0 1 2 3 4] <class ‘numpy.ndarray’>
# 创建二维数组
c = np.array([[11, 12],
[21, 22]])
print(c, type(c))
# [[11 12]
# [21 22]] <class ‘numpy.ndarray’> -
asarray()
def asarray(a, dtype=None, order=None): return array(a, dtype, copy=False, order=order)
- 由定义可以看出区别:array()和asarray()主要区别就是当数据源是ndarray 时,array()仍然会 copy 出一个副本,占用新的内存,但不改变 dtype 时 asarray()不会。(注:更改为较大的dtype时,其大小必须是array的最后一个axis的总大小(以字节为单位)的除数 )
x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
print(x, x.dtype)
# [[1 1 1]
# [1 1 1]
# [1 1 1]] int32
x.dtype = np.float
# ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.
-
fromfunction()
fromfunction(function, shape, **kwargs)
给函数绘图的时候可能会用到fromfunction(),该函数可从函数中创建数组def f(x, y):
return 10 * x + y
x = np.fromfunction(f, (5, 4), dtype=int)
print(x)
# [[ 0 1 2 3]
# [10 11 12 13]
# [20 21 22 23]
# [30 31 32 33]
# [40 41 42 43]]
x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
print(x)
# [[ True False False]
# [False True False]
# [False False True]]
x = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
print(x)
# [[0 1 2]
# [1 2 3]
# [2 3 4]] -
零数组
zeros()函数:返回给定形状和类型的零数组。
zeros_like()函数:返回与给定数组形状和类型相同的零数组。def zeros(shape, dtype=None, order='C'): def zeros_like(a, dtype=None, order='K', subok=True, shape=None):
示例:
x = np.zeros(5)
print(x) # [0. 0. 0. 0. 0.]
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.zeros_like(x)
print(y)
# [[0 0 0]
# [0 0 0]] -
1数组
ones()函数:返回给定形状和类型的1数组。
ones_like()函数:返回与给定数组形状和类型相同的1数组
用法与zeros()、zeros_like()相同def ones(shape, dtype=None, order='C'): def ones_like(a, dtype=None, order='K', subok=True, shape=None):
-
空数组
empty()函数:返回一个空数组,数组元素为随机数。
empty_like函数:返回与给定数组具有相同形状和类型的新数组
用法与zeros()、zeros_like()相同def empty(shape, dtype=None, order='C'): def empty_like(prototype, dtype=None, order='K', subok=True, shape=None):
-
单位数组
eye()函数:返回一个对角线上为1,其它地方为零的单位数组。
identity()函数:返回一个方的单位数组。def eye(N, M=None, k=0, dtype=float, order='C'): def identity(n, dtype=None):
示例:
x = np.eye(4)
print(x)
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]]
x = np.eye(2, 3)
print(x)
# [[1. 0. 0.]
# [0. 1. 0.]]
x = np.identity(4)
print(x)
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]] -
对角数组
diag()函数:提取对角线或构造对角数组。def diag(v, k=0):
示例
x = np.arange(9).reshape((3, 3))
print(x)# [[0 1 2]# [3 4 5]# [6 7 8]]
print(np.diag(x)) # [0 4 8]
print(np.diag(x, k=1)) # [1 5]
print(np.diag(x, k=-1)) # [3 7]
v = [1, 3, 5, 7]
x = np.diag(v)
print(x)
# [[1 0 0 0]
# [0 3 0 0]
# [0 0 5 0]
# [0 0 0 7]] -
常数数组
full()函数:返回一个常数数组。
full_like()函数:返回与给定数组具有相同形状和类型的常数数组。def full(shape, fill_value, dtype=None, order='C'): def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):
示例
x = np.full((2,), 7)
print(x)
# [7 7]
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.full_like(x, 7)
print(y)
# [[7 7 7]
# [7 7 7]] -
利用数值范围来创建ndarray
arange()函数:返回给定间隔内的均匀间隔的值。
linspace()函数:返回指定间隔内的等间隔数字。
logspace()函数:返回数以对数刻度均匀分布。
numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。def arange([start,] stop[, step,], dtype=None): def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): def rand(d0, d1, ..., dn):
示例:
x = np.arange(5)
print(x) # [0 1 2 3 4]
x = np.arange(3, 7, 2)
print(x) # [3 5]
x = np.linspace(start=0, stop=2, num=9)
print(x)
# [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]
x = np.logspace(0, 1, 5)
print(np.around(x, 2))
# [ 1. 1.78 3.16 5.62 10. ]
#np.around 返回四舍五入后的值,可指定精度。
# around(a, decimals=0, out=None)
# a 输入数组
# decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置
x = np.linspace(start=0, stop=1, num=5)
x = [10 ** i for i in x]
print(np.around(x, 2))
# [ 1. 1.78 3.16 5.62 10. ]
x = np.random.random(5)
print(x)
# [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291]
x = np.random.random([2, 3])
print(x)
# [[0.41151858 0.93785153 0.57031309]
# [0.13482333 0.20583516 0.45429181]] -
结构数组的创建
结构数组,首先需要定义结构,然后利用np.array()来创建数组,其参数dtype为定义的结构。- 利用字典来定义结构
示例:personType = np.dtype({
‘names’: [‘name’, ‘age’, ‘weight’],
‘formats’: [‘U30’, ‘i8’, ‘f8’]})
a = np.array([(‘Liming’, 24, 63.9), (‘Mike’, 15, 67.), (‘Jan’, 34, 45.8)],
dtype=personType)
print(a, type(a))
# [(‘Liming’, 24, 63.9) (‘Mike’, 15, 67. ) (‘Jan’, 34, 45.8)]
# <class ‘numpy.ndarray’> - 利用包含多个元组的列表来定义结构
示例:personType = np.dtype([(‘name’, ‘U30’), (‘age’, ‘i8’), (‘weight’, ‘f8’)])
a = np.array([(‘Liming’, 24, 63.9), (‘Mike’, 15, 67.), (‘Jan’, 34, 45.8)],
dtype=personType)
print(a, type(a))
# [(‘Liming’, 24, 63.9) (‘Mike’, 15, 67. ) (‘Jan’, 34, 45.8)]
# <class ‘numpy.ndarray’>
# 结构数组的取值方式和一般数组差不多,可以通过下标取得元素:
print(a[0])
# (‘Liming’, 24, 63.9)
print(a[-2:])
# [(‘Mike’, 15, 67. ) (‘Jan’, 34, 45.8)]
# 我们可以使用字段名作为下标获取对应的值
print(a[‘name’])
# [‘Liming’ ‘Mike’ ‘Jan’]
print(a[‘age’])
# [24 15 34]
print(a[‘weight’])
# [63.9 67. 45.8]
- 利用字典来定义结构
属性
在使用 numpy 时,你会想知道数组的某些信息。很幸运,在这个包里边包含了很多便捷的方法,可以给你想要的信息。
numpy.ndarray.ndim用于返回数组的维数(轴的个数)也称为秩,一维数组的秩为 1,二维数组的秩为 2,以此类推。
numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
numpy.ndarray.size数组中所有元素的总量,相当于数组的shape中所有元素的乘积,例如矩阵的元素总量为行与列的乘积。
numpy.ndarray.dtype ndarray 对象的元素类型。
numpy.ndarray.itemsize以字节的形式返回数组中每一个元素的大小。
```
class ndarray(object):
shape = property(lambda self: object(), lambda self, v: None, lambda self: None)
dtype = property(lambda self: object(), lambda self, v: None, lambda self: None)
size = property(lambda self: object(), lambda self, v: None, lambda self: None)
ndim = property(lambda self: object(), lambda self, v: None, lambda self: None)
itemsize = property(lambda self: object(), lambda self, v: None, lambda self: None)
```
示例:
a = np.array([1, 2, 3, 4, 5])
print(a.shape) # (5,)
print(a.dtype) # int32
print(a.size) # 5
print(a.ndim) # 1
print(a.itemsize) # 4
b = np.array([[1, 2, 3], [4, 5, 6.0]])
print(b.shape) # (2, 3)
print(b.dtype) # float64
print(b.size) # 6
print(b.ndim) # 2
print(b.itemsize) # 8
在ndarray中所有元素必须是同一类型,否则会自动向下转换,int->float->str
示例:
a = np.array([1, 2, 3, 4, 5])
print(a) # [1 2 3 4 5]
b = np.array([1, 2, 3, 4, ‘5’])
print(b) # [‘1’ ‘2’ ‘3’ ‘4’ ‘5’]
c = np.array([1, 2, 3, 4, 5.0])
print© # [1. 2. 3. 4. 5.]