NumPy是Python語言的一個擴展包。支持多維數(shù)組與矩陣運算,此外也針對數(shù)組運算提供大量的數(shù)學(xué)函數(shù)庫。NumPy提供了與Matlab相似的功能與操作方式,因為兩者皆為直譯語言。
NumPy通常與SciPy(Scientific Python)和Matplotlib(繪圖庫)一起使用,這種組合廣泛用于替代Matlab,是一個流行的技術(shù)平臺。
NumPy中定義的最重要的對象是稱為ndarray的N維數(shù)組類型。它描述相同類型的元素集合,可以使用基于零的索引訪問集合中元素?;镜膎darray是使用NumPy中的數(shù)組函數(shù)創(chuàng)建的: numpy.array。
NumPy支持比Python更多種類的數(shù)值類型。NumPy數(shù)值是dtype(數(shù)據(jù)類型)對象的實例,每個對象具有唯一的特征。
以下對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]
# 等間隔數(shù)字的數(shù)組
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指定返回數(shù)組的最小維數(shù)
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:數(shù)組的所需數(shù)據(jù)類型
f = np.array([1, 2, 3], dtype=complex); print(f) # [1.+0.j 2.+0.j 3.+0.j]
# 使用數(shù)組標(biāo)量類型
dt = np.dtype(np.int32); print(dt) # int32
# int8,int16,int32,int64可替換為等價的字符串'i1', 'i2', 'i4', 'i8'
dt = np.dtype('i8'); print(dt) # int64
# 調(diào)整數(shù)組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:返回數(shù)組的維數(shù)
a = np.arange(24); print(a.ndim) # 1
# numpy.reshape: 在不改變數(shù)據(jù)的條件下修改形狀
b = a.reshape(2, 4, 3); print(b.ndim) # 3
# itemsize:返回數(shù)組中每個元素的字節(jié)單位長度
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
# 空數(shù)組
x = np.empty([3, 2], dtype='i1'); print(x) # 數(shù)組x的元素為隨機值,因為它們未初始化
# 含有5個0的數(shù)組,若不指定類型,則默認(rèn)為float
x = np.zeros(5, dtype=np.int); print(x) # [0 0 0 0 0]
# 含有6個1的二維數(shù)組,若不指定類型,則默認(rèn)為float
x = np.ones([2, 3], dtype=int); print(x) # [[1 1 1]
# [1 1 1]]
# 將列表轉(zhuǎn)換為ndarray
x = [1, 2, 3]
a = np.asarray(x, dtype=float); print(a) # [1. 2. 3.]
# 將元組轉(zhuǎn)換為ndarray
x = (1, 2, 3)
a = np.asarray(x, dtype=complex); print(a) # [1.+0.j 2.+0.j 3.+0.j]
# 使用內(nèi)置的range()函數(shù)創(chuàng)建列表對象
x = range(5); print(x) # range(0, 5)
# 從列表中獲得迭代器
it = iter(x); print(it) #
# 使用迭代器創(chuàng)建ndarray, fromiter函數(shù)從任何可迭代對象構(gòu)建一個ndarray對象,返回一個新的一維數(shù)組
y = np.fromiter(it, dtype=float); print(y) # [0. 1. 2. 3. 4.]
# arange函數(shù)返回ndarray對象,包含給定范圍內(nèi)的等間隔值
# numpy.arange(start, stop, step, dtype), start起始值,默認(rèn)為0;stop終止值,不包含; step間隔,默認(rèn)為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,此函數(shù)類似于arange,在此函數(shù)中,指定了范圍之間的均勻間隔數(shù)量,而不是步長
# numpy.linspace(start, stop, num, endpoint, retstep, dtype)
# start,起始值;stop,終止值,如果endpoint為true,該值包含于序列中;num,要生成的等間隔樣例數(shù)量,默認(rèn)為50;
# endpoint,序列中是否包含stop值,默認(rèn)為true;retstep,如果為true,返回樣例,以及連續(xù)數(shù)字之間的步長
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)
評論
查看更多