用實踐帶領你進入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)形狀的數組。
還有許多不同的數組創建方法,但是我認爲,從以上的這些方法入手,你會很快的學習使用新的數組創建函數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章