一、簡介
?NumPy(Numerical Python) 是用于科學(xué)計(jì)算及數(shù)據(jù)處理的Python擴(kuò)展程序庫,支持大量的維度數(shù)組與矩陣運(yùn)算,此外也針對數(shù)組運(yùn)算提供大量的數(shù)學(xué)函數(shù)庫。
二、數(shù)據(jù)結(jié)構(gòu)
?numpy基本數(shù)據(jù)結(jié)構(gòu)類型為Ndarray對象,其為可存放同類型元素的多維數(shù)組。
- 創(chuàng)建ndarray語法如下
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
示例
使用前導(dǎo)入numpy包
import numpy as np
- 傳入多維列表
a = np.array([[0, 1], [2, 3]])
print (a)
# 輸出:[[0 1]
# [2 3]]
- 指定維度
a = np.array([1, 2, 3], ndmin = 2)
print (a)
#輸出: [[1 2 3]]
-包含的基本屬性
| 屬性 | 說明 |
|---|---|
| ndarray.ndim | 秩,即軸的數(shù)量或維度的數(shù)量 |
| ndarray.shape | 數(shù)組的維度,對于矩陣,n 行 m 列 |
| ndarray.size | 數(shù)組元素的總個數(shù),相當(dāng)于 .shape 中 n*m 的值 |
| ndarray.dtype | ndarray 對象的元素類型 |
| ndarray.itemsize | ndarray 對象中每個元素的大小,以字節(jié)為單位 |
| ndarray.flags | ndarray 對象的內(nèi)存信息 |
| ndarray.real | ndarray元素的實(shí)部 |
| ndarray.imag | ndarray 元素的虛部 |
| ndarray.data | 包含實(shí)際數(shù)組元素的緩沖區(qū),由于一般通過數(shù)組的索引獲取元素,所以通常不需要使用這個屬性。 |
三、花式創(chuàng)建narray的9種方法
- 創(chuàng)建指定shape的空narray
numpy.empty(shape, dtype = float, order = 'C') # order可取"C"(行優(yōu)先)和"F"(列優(yōu)先)。
- 示例
x = np.empty([3,2], dtype = int)
print (x)
輸出: [[ 6917529027641081856 5764616291768666155] [ 6917529027641081859 -5764598754299804209] [ 4497473538 844429428932120]]
2. 創(chuàng)建全0的narray
numpy.zeros(shape, dtype = float, order = 'C')
- 示例
x = np.zeros((5,), dtype = np.int)
print(x)
輸出: [0 0 0 0 0]
3. 創(chuàng)建全1的narray
numpy.ones(shape, dtype = float, order = 'C')
- 示例
x = np.ones(5)
print(x)
輸出: [1. 1. 1. 1.]
- 從range創(chuàng)建narray
numpy.arange(start = 0, stop, step = 1, dtype =None)
- 示例
x = np.arange(5)
print (x)
輸出: [0 1 2 3 4]
5.創(chuàng)建等差數(shù)列填充的narray
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# endpoint 是否包含最后一個數(shù)
retstep 是否顯示步長
- 示例1
a = np.linspace(1,10,10)
print(a)
輸出: [1 2 3 4 5 6 7 8 9 10]
- 示例2
a = np.linspace(1,10,10, retstep=True)
print(a)
輸出是一個元組: (array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
6. 創(chuàng)建等比數(shù)列填充的narray
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
base 參數(shù)意思是取對數(shù)的時候 log 的下標(biāo)。
| 參數(shù) | 描述 |
|---|---|
| start | 序列的起始值為:base ** start |
| stop | 序列的終止值為:base ** stop。如果endpoint為true,該值包含于數(shù)列中 |
| num | 要生成的等步長的樣本數(shù)量,默認(rèn)為50 |
| endpoint | 該值為 true 時,數(shù)列中中包含stop值,反之不包含,默認(rèn)是True。 |
| base | 對數(shù) log 的底數(shù)。 |
| dtype | ndarray 的數(shù)據(jù)類型 |
- 示例1
# 默認(rèn)底數(shù)是 10
a = np.logspace(1.0, 2.0, num = 10)
print (a)
輸出: [ 10. 12.91549665 16.68100537 21.5443469 27.82559402 35.93813664 46.41588834 59.94842503 77.42636827 100. ]
- 示例2
a = np.logspace(0,9,10,base=2)
print (a)
輸出: [ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]
7. 從已有數(shù)組創(chuàng)建narray
numpy.asarray(a, dtype = None, order = None)
| 參數(shù) | 描述 |
|---|---|
| a | 任意形式的輸入?yún)?shù),可以是,列表, 列表的元組, 元組, 元組的元組, 元組的列表,多維數(shù)組 |
| dtype | 數(shù)據(jù)類型,可選 |
| order | 可選,有"C"和"F"兩個選項(xiàng),分別代表,行優(yōu)先和列優(yōu)先,在計(jì)算機(jī)內(nèi)存中的存儲元素的順序。 |
- 示例
x = [1,2,3]
a = np.asarray(x)
print (a)
輸出: [1 2 3]
8. 從buffer流創(chuàng)建narray
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
| 參數(shù) | 描述 |
|---|---|
| buffer | 可以是任意對象,會以流的形式讀入。 |
| dtype | 返回?cái)?shù)組的數(shù)據(jù)類型,可選 |
| count | 讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù)。 |
| offset | 讀取的起始位置,默認(rèn)為0。 |
- 示例
s = b'Hello World'
a = np.frombuffer(s, dtype = 'S1')
print (a)
輸出: [b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']
9. 從迭代對象創(chuàng)建narray
numpy.fromiter(iterable, dtype, count=-1)
| 參數(shù) | 描述 |
|---|---|
| iterable | 可迭代對象 |
| dtype | 返回?cái)?shù)組的數(shù)據(jù)類型 |
| count | 讀取的數(shù)據(jù)數(shù)量,默認(rèn)為-1,讀取所有數(shù)據(jù) |
- 示例
# 使用 range 函數(shù)創(chuàng)建列表對象
list=range(5)
it=iter(list)
# 使用迭代器創(chuàng)建 ndarray
x=np.fromiter(it, dtype=float)
print(x)
輸出: [0. 1. 2. 3. 4.]
注:整理自菜鳥教程
posted on
浙公網(wǎng)安備 33010602011771號