参考链接: NumPy官网
参考链接: NumPy: the absolute basics for beginners
参考链接: Quickstart tutorial
参考链接: Broadcasting广播
参考链接: NumPy 中文教程
参考链接: Python数据分析与展示
使用array函数,从Python中的列表、元组等类型创建ndarray数组,当np.array()不指定dtype时,NumPy将根据数据情况关联一个dtype类型:
以下分别使用列表和元组以及列表元组的混合类型创建ndarray:
>>>
>>>
>>> x = np.array([0,1,2,3,4], dtype=np.float32)
>>> x
array([0., 1., 2., 3., 4.], dtype=float32)
>>>
>>> x = np.array([0,1,2,3])
>>> x
array([0, 1, 2, 3])
>>> print(x)
[0 1 2 3]
>>> x = np.array((0,1,2,3))
>>> x
array([0, 1, 2, 3])
>>> print(x)
[0 1 2 3]
>>> x = np.array([[0,1],[2,3],(4,5)])
>>> x
array([[0, 1],
[2, 3],
[4, 5]])
>>> print(x)
[[0 1]
[2 3]
[4 5]]
>>>
>>>
使用NumPy中函数创建ndarray数组:
函数 | 说明 |
---|---|
np.arange(n) | 类似range()函数,返回ndarray类型,元素从0到n‐1 |
np.ones(shape) | 根据shape生成一个全1数组,shape是元组类型 |
np.zeros(shape) | 根据shape生成一个全0数组,shape是元组类型 |
np.full(shape,val) | 根据shape生成一个数组,每个元素值都是val |
np.eye(n) | 创建一个正方的n*n单位矩阵,对角线为1,其余为0 |
np.ones_like(a) | 根据数组a的形状生成一个全1数组 |
np.zeros_like(a) | 根据数组a的形状生成一个全0数组 |
np.full_like(a,val) | 根据数组a的形状生成一个数组,每个元素值都是val |
np.linspace() | 根据起止数据等间距地填充数据,形成数组 |
np.concatenate() | 将两个或多个数组合并成一个新的数组 |
实验1:
Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x = arrange(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'arrange' is not defined
>>>
>>> x = np.arrange(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python\Python37\lib\site-packages\numpy\__init__.py", line 220, in __getattr__
"{!r}".format(__name__, attr))
AttributeError: module 'numpy' has no attribute 'arrange'
>>>
>>>
>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> print(x)
[0 1 2 3 4 5 6 7 8 9]
>>>
>>> x = np.ones((3,4))
>>> x
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> x = np.ones([3,4])
>>> x = np.ones([3,5])
>>> x
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
>>>
>>> x = np.zeros((2,6))
>>> x
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
>>>
>>> x = np.full((2,3),20200910)
>>> x
array([[20200910, 20200910, 20200910],
[20200910, 20200910, 20200910]])
>>>
>>> x = np.full((2,3),20200910.00)
>>> x
array([[20200910., 20200910., 20200910.],
[20200910., 20200910., 20200910.]])
>>> x = np.eye(3)
>>> x
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>>
>>> x = np.ones(2,3,4,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ones() takes from 1 to 3 positional arguments but 4 were given
>>> x = np.ones((2,3,4,5))
>>> x
array([[[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]],
[[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]]])
>>> x.shape
(2, 3, 4, 5)
>>>
>>>
实验2:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> x = np.full((3,4),20200910)
>>> x.shape
(3, 4)
>>> x
array([[20200910, 20200910, 20200910, 20200910],
[20200910, 20200910, 20200910, 20200910],
[20200910, 20200910, 20200910, 20200910]])
>>> y = np.ones_like(x)
>>> y
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
>>> y = np.zeros_like(x)
>>> y
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
>>> y = np.full_like(x,910)
>>> y
array([[910, 910, 910, 910],
[910, 910, 910, 910],
[910, 910, 910, 910]])
>>>
>>>
实验3:
>>>
>>> x = np.linspace(0,10,5)
>>> x
array([ 0. , 2.5, 5. , 7.5, 10. ])
>>> y = np.linspace(10,20,5)
>>> y
array([10. , 12.5, 15. , 17.5, 20. ])
>>> z = np.concatenate((x,y))
>>> z
array([ 0. , 2.5, 5. , 7.5, 10. , 10. , 12.5, 15. , 17.5, 20. ])
>>>
>>>