用实践带领你进入numpy的世界——(二):numpy基本数组创建函数

                         QQ:3020889729                                                                                 小蔡

本节主要讲解numpy数组创建的方法。
在这里插入图片描述
本文以jupyter notebook格式展开~

引入numpy库

import numpy as np

np.zeros()创建全零数组

numpy.zeros(shape,dtype = float,order =‘C’ )——默认数据类型为np.float64

example_1 = np.zeros((1, ))   # 一维全零数组,且一维的维度大小为1个元素
example_2 = np.zeros((2, 2))   # 二维全零数组, 2*2
example_3 = np.zeros((3, 3, 3))  # 三维全零数组, 3*3*3
print("(zeros)example_1 :\n{}".format(example_1))
print("(zeros)example_2 :\n{}".format(example_2))
print("(zeros)example_3 :\n{}".format(example_3))

结果(打印):
(zeros)example_1 :
[0.]
(zeros)example_2 :
[[0. 0.]
[0. 0.]]
(zeros)example_3 :
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]]

np.empty()创建数值不定的数组(数组元素值取决于内存)

example_1 = np.empty((1, ))   # 一维数值不定的数组,且一维的维度大小为1个元素
example_2 = np.empty((2, 2))   # 二维数值不定的数组, 2*2
example_3 = np.empty((3, 3, 3))  # 三维数值不定的数组, 3*3*3
print("(empty)example_1 :\n{}".format(example_1))
print("(empty)example_2 :\n{}".format(example_2))
print("(empty)example_3 :\n{}".format(example_3))

结果(打印):
(empty)example_1 :
[0.]
(empty)example_2 :
[[0. 0.]
[0. 0.]]
(empty)example_3 :
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]]

np.ones()创建全一数组

numpy.ones(shape,dtype = None,order =‘C’ )

example_1 = np.ones((1, ))  # 一维全一数组,且一维的维度大小为1个元素
example_2 = np.ones((2, 2))  # 二维全一数组, 2*2
example_3 = np.ones((3, 3, 3))  # 三维全一数组, 3*3*3
print("(ones)example_1 :\n{}".format(example_1))
print("(ones)example_2 :\n{}".format(example_2))
print("(ones)example_3 :\n{}".format(example_3))

结果(打印):
(ones)example_1 :
[1.]
(ones)example_2 :
[[1. 1.]
[1. 1.]]
(ones)example_3 :
[[[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.]]]

np.linspace()创建任意长度的一维数组

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

example_1 = np.linspace(0 , 9, 100)  # 一维向量——从0到9,共取100个点,作为元素(包含左右端点)
example_2 = np.linspace(-9, 0, 100)  # 一维向量——从-9到0,共取100个点,作为元素(包含左右端点)
example_3 = np.linspace(-9, 9, 200)  # 一维向量——从-9到9,共取100个点,作为元素(包含左右端点)
print("(linspace)example_1 :\n{}".format(example_1))
print("(linspace)example_2 :\n{}".format(example_2))
print("(linspace)example_3 :\n{}".format(example_3))

结果(打印):
(linspace)example_1 :
[0. 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545
0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.
1.09090909 1.18181818 1.27272727 1.36363636 1.45454545 1.54545455
1.63636364 1.72727273 1.81818182 1.90909091 2. 2.09090909
2.18181818 2.27272727 2.36363636 2.45454545 2.54545455 2.63636364
2.72727273 2.81818182 2.90909091 3. 3.09090909 3.18181818
3.27272727 3.36363636 3.45454545 3.54545455 3.63636364 3.72727273
3.81818182 3.90909091 4. 4.09090909 4.18181818 4.27272727
4.36363636 4.45454545 4.54545455 4.63636364 4.72727273 4.81818182
4.90909091 5. 5.09090909 5.18181818 5.27272727 5.36363636
5.45454545 5.54545455 5.63636364 5.72727273 5.81818182 5.90909091
6. 6.09090909 6.18181818 6.27272727 6.36363636 6.45454545
6.54545455 6.63636364 6.72727273 6.81818182 6.90909091 7.
7.09090909 7.18181818 7.27272727 7.36363636 7.45454545 7.54545455
7.63636364 7.72727273 7.81818182 7.90909091 8. 8.09090909
8.18181818 8.27272727 8.36363636 8.45454545 8.54545455 8.63636364
8.72727273 8.81818182 8.90909091 9. ]
(linspace)example_2 :
[-9. -8.90909091 -8.81818182 -8.72727273 -8.63636364 -8.54545455
-8.45454545 -8.36363636 -8.27272727 -8.18181818 -8.09090909 -8.
-7.90909091 -7.81818182 -7.72727273 -7.63636364 -7.54545455 -7.45454545
-7.36363636 -7.27272727 -7.18181818 -7.09090909 -7. -6.90909091
-6.81818182 -6.72727273 -6.63636364 -6.54545455 -6.45454545 -6.36363636
-6.27272727 -6.18181818 -6.09090909 -6. -5.90909091 -5.81818182
-5.72727273 -5.63636364 -5.54545455 -5.45454545 -5.36363636 -5.27272727
-5.18181818 -5.09090909 -5. -4.90909091 -4.81818182 -4.72727273
-4.63636364 -4.54545455 -4.45454545 -4.36363636 -4.27272727 -4.18181818
-4.09090909 -4. -3.90909091 -3.81818182 -3.72727273 -3.63636364
-3.54545455 -3.45454545 -3.36363636 -3.27272727 -3.18181818 -3.09090909
-3. -2.90909091 -2.81818182 -2.72727273 -2.63636364 -2.54545455
-2.45454545 -2.36363636 -2.27272727 -2.18181818 -2.09090909 -2.
-1.90909091 -1.81818182 -1.72727273 -1.63636364 -1.54545455 -1.45454545
-1.36363636 -1.27272727 -1.18181818 -1.09090909 -1. -0.90909091
-0.81818182 -0.72727273 -0.63636364 -0.54545455 -0.45454545 -0.36363636
-0.27272727 -0.18181818 -0.09090909 0. ]
(linspace)example_3 :
[-9. -8.90954774 -8.81909548 -8.72864322 -8.63819095 -8.54773869
-8.45728643 -8.36683417 -8.27638191 -8.18592965 -8.09547739 -8.00502513
-7.91457286 -7.8241206 -7.73366834 -7.64321608 -7.55276382 -7.46231156
-7.3718593 -7.28140704 -7.19095477 -7.10050251 -7.01005025 -6.91959799
-6.82914573 -6.73869347 -6.64824121 -6.55778894 -6.46733668 -6.37688442
-6.28643216 -6.1959799 -6.10552764 -6.01507538 -5.92462312 -5.83417085
-5.74371859 -5.65326633 -5.56281407 -5.47236181 -5.38190955 -5.29145729
-5.20100503 -5.11055276 -5.0201005 -4.92964824 -4.83919598 -4.74874372
-4.65829146 -4.5678392 -4.47738693 -4.38693467 -4.29648241 -4.20603015
-4.11557789 -4.02512563 -3.93467337 -3.84422111 -3.75376884 -3.66331658
-3.57286432 -3.48241206 -3.3919598 -3.30150754 -3.21105528 -3.12060302
-3.03015075 -2.93969849 -2.84924623 -2.75879397 -2.66834171 -2.57788945
-2.48743719 -2.39698492 -2.30653266 -2.2160804 -2.12562814 -2.03517588
-1.94472362 -1.85427136 -1.7638191 -1.67336683 -1.58291457 -1.49246231
-1.40201005 -1.31155779 -1.22110553 -1.13065327 -1.04020101 -0.94974874
-0.85929648 -0.76884422 -0.67839196 -0.5879397 -0.49748744 -0.40703518
-0.31658291 -0.22613065 -0.13567839 -0.04522613 0.04522613 0.13567839
0.22613065 0.31658291 0.40703518 0.49748744 0.5879397 0.67839196
0.76884422 0.85929648 0.94974874 1.04020101 1.13065327 1.22110553
1.31155779 1.40201005 1.49246231 1.58291457 1.67336683 1.7638191
1.85427136 1.94472362 2.03517588 2.12562814 2.2160804 2.30653266
2.39698492 2.48743719 2.57788945 2.66834171 2.75879397 2.84924623
2.93969849 3.03015075 3.12060302 3.21105528 3.30150754 3.3919598
3.48241206 3.57286432 3.66331658 3.75376884 3.84422111 3.93467337
4.02512563 4.11557789 4.20603015 4.29648241 4.38693467 4.47738693
4.5678392 4.65829146 4.74874372 4.83919598 4.92964824 5.0201005
5.11055276 5.20100503 5.29145729 5.38190955 5.47236181 5.56281407
5.65326633 5.74371859 5.83417085 5.92462312 6.01507538 6.10552764
6.1959799 6.28643216 6.37688442 6.46733668 6.55778894 6.64824121
6.73869347 6.82914573 6.91959799 7.01005025 7.10050251 7.19095477
7.28140704 7.3718593 7.46231156 7.55276382 7.64321608 7.73366834
7.8241206 7.91457286 8.00502513 8.09547739 8.18592965 8.27638191
8.36683417 8.45728643 8.54773869 8.63819095 8.72864322 8.81909548
8.90954774 9. ]

np.arange()与python中的range使用方法相同,返回的是一个数组

(这里需要说明的是,arange中的参数均可以是浮点数(小数))

numpy.arange([start, ]stop, [step, ]dtype=None)

example_1 = np.arange(0 , 9, 1)  # 一维数组——从0到9,步长为1取值作为元素(包含左端点)
example_2 = np.arange(0, 9, 0.5)  # 一维数组——从0到9,步长为0.5取值作为元素(包含左端点)
example_3 = np.arange(0, 4.5, 0.5)  # 一维数组——从0到4.5,步长为0.5取值作为元素(包含左端点)
print("(arange)example_1 :\n{}".format(example_1))
print("(arange)example_2 :\n{}".format(example_2))
print("(arange)example_3 :\n{}".format(example_3))

结果(打印):
(arange)example_1 :
[0 1 2 3 4 5 6 7 8]
(arange)example_2 :
[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5]
(arange)example_3 :
[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. ]

补充arange—— 由arange创建的数组,只是通过step间隔取值,无法确定需要取多少的元素(换句话说就是,如果你需要一定元素个数的数组,你就需要好好设计step才行)。
这是不方便的——这里建议,如果需要取某一范围有限个数的数值作为元素,不妨使用linspace。
因为使用linspace可以预测(确定)在总长中取多少个点作为数组元素。

np.array()创建标准数组(需要传入元素参数)

可指定数据类型

example_1 = np.array([i for i in range(10)], dtype=np.int32)  # 创建数组——数据类型为int32
example_2 = np.array([i for i in range(10)], dtype=np.float32)  # 创建数组——数据类型为float32
example_3 = np.array([i for i in range(10)], dtype=np.complex)  # 创建数组——数据类型为complex
print("(array)example_1 :\n{}".format(example_1))
print("(array)example_2 :\n{}".format(example_2))
print("(array)example_3 :\n{}".format(example_3))

结果(打印):
(array)example_1 :
[0 1 2 3 4 5 6 7 8 9]
(array)example_2 :
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
(array)example_3 :
[0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j 6.+0.j 7.+0.j 8.+0.j 9.+0.j]

改变数组形状——reshape()

example_3 = np.array([i for i in range(10)], dtype=np.complex).reshape(5, 2)
print("(array)example_3 :\n{}".format(example_3))

结果(打印):
(array)example_3 :
[[0.+0.j 1.+0.j]
[2.+0.j 3.+0.j]
[4.+0.j 5.+0.j]
[6.+0.j 7.+0.j]
[8.+0.j 9.+0.j]]

最后做一个总结

一般的,使用函数创建任意形状大小的数组通常是为了某种便捷操作而进行的——比如,有时我们需要一个33的数组来存放特定的顺序,这是我们可以使用zeros((3, 3))创建一个33全零数组,这样我们再把数据依次放到对应索引下存储即可。
总的来说,使用函数创建任意形状的数组是很方便的。

除了以上这些方法外,创建数组还可以依赖随机数来取:
比如:np.random.randint(a, b, (h, w))——参数依次为:从a到b的范围取随机值,组成(h,w)形状的数组。
还有许多不同的数组创建方法,但是我认为,从以上的这些方法入手,你会很快的学习使用新的数组创建函数。

往期回顾

用实践带领你进入numpy的世界——(一):numpy的效率和基本属性
用实践带领你进入numpy的世界——(三):numpy基本运算讲解
用实践带领你进入numpy的世界——(四):numpy基本函数运算(sum,mean……)
用实践带领你进入numpy的世界——(五):numpy广播运算讲解(加法为例)
用实践带领你进入numpy的世界——(六):numpy综合小练习①
numpy知道多少?感知篇——numpy的属性

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NULL not error

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值