NumPy库学习笔记(2) 数组的创建和初始化

参考链接: 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. ])
>>> 
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值