numpy常用函式

2021-09-25 01:43:31 字數 1936 閱讀 5618

arange是numpy模組中的函式,arange([start,] stop[, step,], dtype=none),根據start與stop指定的範圍以及step設定的步長,生成乙個 ndarray,如果未給出dtype,則資料型別根據輸入引數確定。arange生成乙個等差陣列,step可以為float型別。

a=np.arange(1,5,0.5)

print(a)

[1. 1.5 2. 2.5 3. 3.5 4. 4.5]

a=np.arange(1,5,1)

print(a)

[1 2 3 4]

range為python內建函式,生成

range(stop) -> range object

range(start, stop[, step]) -> range object,兩種呼叫方式,step預設為1,只能取整數。該函式生成可迭代物件,而不是迭代器。

a=range(1,5,1)

print(a)

print(type(a))

range(1, 5)

for i in a:

print(i)01

234

meshgrid函式是numpy的函式,其用兩個座標軸上的點在平面上畫網格。用法:

[x,y]=meshgrid(x,y)

[x,y]=meshgrid(x)與[x,y]=meshgrid(x,x)是等同的

[x,y,z]=meshgrid(x,y,z)生成三維陣列,可用來計算三變數的函式和繪製三維立體圖

[x,y] = meshgrid(x,y) 將向量x和y定義的區域轉換成矩陣x和y,

其中矩陣x的行向量是向量x的簡單複製,而矩陣y的列向量是向量y的簡單複製

array1,array2=np.meshgrid(np.arange(1,5,1),np.arange(2,8,1))

print(array1)

print("*"*10)

print(array2)

[[1 2 3 4]

[1 2 3 4]

[1 2 3 4]

[1 2 3 4]

[1 2 3 4]

[1 2 3 4]]

**********

[[2 2 2 2]

[3 3 3 3]

[4 4 4 4]

[5 5 5 5]

[6 6 6 6]

[7 7 7 7]]

numpy.linspace(start, stop, num=50, endpoint=true, retstep=false, dtype=none),在指定範圍內返回均勻間隔的陣列 ,num代表陣列大小,預設為50,endpoint若為真則返回值中包含stop,反之不包含stop

a=np.linspace(1,10,20)

print(a)

[ 1. 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105

3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632

6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158

9.52631579 10. ]

a=np.linspace(1,10,20,endpoint=false)

print(a)

[1. 1.45 1.9 2.35 2.8 3.25 3.7 4.15 4.6 5.05 5.5 5.95 6.4 6.85

7.3 7.75 8.2 8.65 9.1 9.55]

Numpy常用函式

1 把向量轉化為矩陣 import numpy as np a np.arange 15 構造出乙個從0到14的向量 檢視為 array 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14 改變向量為三行五列的矩陣 a.reshape 3,5 結果為 array 0,1,2,3,4...

numpy常用函式

np.unique 去除重複值 np.c 按行按列合併陣列 np.searchsorted a,b 返回b有序插入在a中的位置索引 np.vectorize 向量化運算函式 np.percentile 取數列第百分分位的數值 np.array.any 和numpy.array.all np.arra...

numpy常用函式

ndim 維度 shape 各維度的尺度 2,5 size 元素的個數 10 dtype 元素的型別 dtype int32 np.arange n 元素從0到n 1的ndarray型別 np.ones shape 生成全1 np.zeros shape ddtype np.int32 生成int3...