Numpy 数组的创建

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))
<type 'numpy.ndarray'>
>>> 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))
<type 'numpy.ndarray'>

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.]]

参考:https://www.jb51.net/article/49397.htm?winzoom=1

猜你喜欢

转载自blog.csdn.net/m0_37712157/article/details/81435392