Numpy 陣列的建立

2021-08-22 07:25:10 字數 1510 閱讀 7963

1、numpy.arange(相當於matlab中的 a = 0:14),建立的是列向量

>>> print np.arange(15)

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

>>> print type(np.arange(15))

>>> print np.arange(15).reshape(3,5)

[[ 0  1  2  3  4]

[ 5  6  7  8  9]

[10 11 12 13 14]]

>>> print type(np.arange(15).reshape(3,5))

2、numpy.linspace

numpy.linspace(start, stop, num=50, endpoint=true, retstep=false, dtype=none)

在指定的間隔內返回均勻間隔的數字。

返回num均勻分布的樣本,在[start, stop]。

這個區間的端點可以任意的被排除在外。

例如,在從1到3中產生9個數:

**如下:

>>> print np.linspace(1,3,9)

[ 1.    1.25  1.5   1.75  2.    2.25  2.5   2.75  3.  ]

np.linspace(2.0, 3.0, num=5)

array([ 2. , 2.25, 2.5 , 2.75, 3. ])

>>> np.linspace(2.0, 3.0, num=5, endpoint=false)

array([ 2. , 2.2, 2.4, 2.6, 2.8])

>>> np.linspace(2.0, 3.0, num=5, retstep=true)

(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

3、使用numpy.zeros,numpy.ones,numpy.eye等方法可以構造特定的矩陣

例如:**如下:

>>> print np.zeros((3,4))

[[ 0.  0.  0.  0.]

[ 0.  0.  0.  0.]

[ 0.  0.  0.  0.]]

>>> print np.ones((3,4))

[[ 1.  1.  1.  1.]

[ 1.  1.  1.  1.]

[ 1.  1.  1.  1.]]

>>> print np.eye(3)

[[ 1.  0.  0.]

[ 0.  1.  0.]

[ 0.  0.  1.]]

參考:

numpy陣列的建立

建立陣列最簡單的方法就是使用array函式。它接收一切序列型的物件 包括其他陣列 然後產生乙個新的含有傳入資料的numpy陣列。array函式建立陣列import numpy as np ndarray1 np.array 1,2,3,4 ndarray2 np.array list abcdefg...

numpy的陣列建立

1 建立乙個長度為10的陣列,陣列的值都為0 print np.zeros 10,dtype int 2 建立乙個3 5的浮點型陣列,陣列的值全部為1 np.ones 3,5 dtype float 3 建立乙個3 5的浮點型陣列,陣列的值全部為8 np.full 3,5 8 4 建立乙個線性序列陣...

NumPy 建立陣列

ndarray 陣列除了可以使用底層 ndarray 構造器來建立外,也可以通過以下幾種方式來建立。numpy.empty 方法用來建立乙個指定形狀 shape 資料型別 dtype 且未初始化的陣列 numpy.empty shape,dtype float,order c 引數說明 引數描述 s...