NumPy是Python语言的一个扩展包。支持多维数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。NumPy提供了与Matlab相似的功能与 *** 作方式,因为两者皆为直译语言。
NumPy通常与SciPy(ScienTIfic Python)和Matplotlib(绘图库)一起使用,这种组合广泛用于替代Matlab,是一个流行的技术平台。
NumPy中定义的最重要的对象是称为ndarray的N维数组类型。它描述相同类型的元素集合,可以使用基于零的索引访问集合中元素。基本的ndarray是使用NumPy中的数组函数创建的: numpy.array。
NumPy支持比Python更多种类的数值类型。NumPy数值是dtype(数据类型)对象的实例,每个对象具有唯一的特征。
以下对ndarray的介绍来自于
An ndarray is a (usually fixed-size) mulTIdimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N posiTIve integers that specify the sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.
As with other container objects in Python, the contents of an ndarray can be accessed and modified by indexing or slicing the array, and via the methods and attributes of the ndarray.
Different ndarrays can share the same data, so that changes made in one ndarray may be visible in another. That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implemenTIng the buffer or array interfaces.
以下是NumPy简单使用例子
import numpy as np
from matplotlib import pyplot as plt
# 一维
a = np.array([1, 2, 3]); print(a) # [1 2 3]
# 等间隔数字的数组
b = np.arange(10); print(b) # [0 1 2 3 4 5 6 7 8 9]
# 二维
c = np.array([[1, 2], [3, 4]]); print(c) # [[1 2]
# [3 4]]
# ndmin指定返回数组的最小维数
d = np.array([1, 2, 3, 4, 5]); print(d) # [1 2 3 4 5]
e = np.array([1, 2, 3, 4, 5], ndmin=2); print(e) # [[1 2 3 4 5]]
# dtype:数组的所需数据类型
f = np.array([1, 2, 3], dtype=complex); print(f) # [1.+0.j 2.+0.j 3.+0.j]
# 使用数组标量类型
dt = np.dtype(np.int32); print(dt) # int32
# int8,int16,int32,int64可替换为等价的字符串'i1', 'i2', 'i4', 'i8'
dt = np.dtype('i8'); print(dt) # int64
# 调整数组shape
a = np.array([[1, 2, 3], [4, 5, 6]]); print(a); # [[1 2 3]
# [4 5 6]]
a.shape = (3, 2); print(a) # [[1 2]
# [3 4]
# [5 6]]
a = np.array([[1, 2, 3], [4, 5, 6]]); b = a.reshape(3, 2); print(b) # [[1 2]
# [3 4]
# [5 6]]
# ndim:返回数组的维数
a = np.arange(24); print(a.ndim) # 1
# numpy.reshape: 在不改变数据的条件下修改形状
b = a.reshape(2, 4, 3); print(b.ndim) # 3
# itemsize:返回数组中每个元素的字节单位长度
a = np.array([1, 2, 3, 4], dtype=np.int8); print(a.itemsize) # 1
a = np.array([1, 2, 3, 4], dtype=np.float32); print(a.itemsize) # 4
# 空数组
x = np.empty([3, 2], dtype='i1'); print(x) # 数组x的元素为随机值,因为它们未初始化
# 含有5个0的数组,若不指定类型,则默认为float
x = np.zeros(5, dtype=np.int); print(x) # [0 0 0 0 0]
# 含有6个1的二维数组,若不指定类型,则默认为float
x = np.ones([2, 3], dtype=int); print(x) # [[1 1 1]
# [1 1 1]]
# 将列表转换为ndarray
x = [1, 2, 3]
a = np.asarray(x, dtype=float); print(a) # [1. 2. 3.]
# 将元组转换为ndarray
x = (1, 2, 3)
a = np.asarray(x, dtype=complex); print(a) # [1.+0.j 2.+0.j 3.+0.j]
# 使用内置的range()函数创建列表对象
x = range(5); print(x) # range(0, 5)
# 从列表中获得迭代器
it = iter(x); print(it) #
# 使用迭代器创建ndarray, fromiter函数从任何可迭代对象构建一个ndarray对象,返回一个新的一维数组
y = np.fromiter(it, dtype=float); print(y) # [0. 1. 2. 3. 4.]
# arange函数返回ndarray对象,包含给定范围内的等间隔值
# numpy.arange(start, stop, step, dtype), start起始值,默认为0;stop终止值,不包含; step间隔,默认为1
x = np.arange(4, dtype=float); print(x) # [0. 1. 2. 3.]
x = np.arange(10, 20, 2); print(x) # [10 12 14 16 18]
# numpy.linspace,此函数类似于arange,在此函数中,指定了范围之间的均匀间隔数量,而不是步长
# numpy.linspace(start, stop, num, endpoint, retstep, dtype)
# start,起始值;stop,终止值,如果endpoint为true,该值包含于序列中;num,要生成的等间隔样例数量,默认为50;
# endpoint,序列中是否包含stop值,默认为true;retstep,如果为true,返回样例,以及连续数字之间的步长
x = np.linspace(10, 20, 5); print(x) # [10. 12.5 15. 17.5 20.]
x = np.linspace(10, 20, 5, endpoint=False); print(x) # [10. 12. 14. 16. 18.]
x = np.linspace(1,2,5, retstep=True); print(x) # (array([ 1. , 1.25, 1.5 , 1.75, 2. ]), 0.25)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)