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)
評論
查看更多