目錄
使用ndarray提供的內置函數可以快速,簡單的創建需要的ndarray數組。
一、zeros
import numpy as np
#np.zeros 創建用0填充的ndarray
arr = np.zeros((3, 4))
print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))
運行結果:
arr=[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
arr.shape=(3, 4)
arr.dtype=float64
二、ones
import numpy as np
#np.ones創建用1填充的ndarray
arr = np.ones((3, 4))
print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))
運行結果:
arr=[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
arr.shape=(3, 4)
arr.dtype=float64
三、full
import numpy as np
#np.full 創建用自定義內容(20)填充的ndarray
arr = np.full((3, 4), 20)
print('arr={}'.format(arr))
print('arr.type={}'.format(type(arr)))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))
運行結果:
arr=[[20 20 20 20]
[20 20 20 20]
[20 20 20 20]]
arr.type=<class 'numpy.ndarray'>
arr.shape=(3, 4)
arr.dtype=int64
四、eye
import numpy as np
#np.eye(N) 創建一個 N*N的單位矩陣。單位矩陣是主對角線上全是 1,其他位置全是 0 的方形矩陣。
arr = np.eye(3)
print('arr={}'.format(arr))
print('arr.type={}'.format(type(arr)))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))
運行結果:
arr=[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
arr.type=<class 'numpy.ndarray'>
arr.shape=(3, 3)
arr.dtype=float64
五、diag
import numpy as np
#np.diag(list) 創建一個指定list的對角矩陣
arr = np.diag((10, 20, 30))
print('arr={}'.format(arr))
print('arr.type={}'.format(type(arr)))
print('arr.shape={}'.format(arr.shape))
print('arr.dtype={}'.format(arr.dtype))
運行結果:
arr=[[10 0 0]
[ 0 20 0]
[ 0 0 30]]
arr.type=<class 'numpy.ndarray'>
arr.shape=(3, 3)
arr.dtype=int64
六、arange
#np.arange(N) 將創建一個秩爲 1 的 ndarray,其中包含從 0 到 N - 1 的連續整數。
arr = np.arange(10)
print('arr={}'.format(arr))
print('--------------------------------')
#np.arange(start,stop) 將創建一個秩爲 1 的 ndarray,其中包含位於半開區間 [start, stop) 內並均勻分佈的值。
arr = np.arange(4, 10)
print('arr={}'.format(arr))
print('--------------------------------')
#np.arange(start,stop,step) 將創建一個秩爲 1 的 ndarray
# 其中包含位於半開區間 [start, stop) 內並均勻分佈的值,step 表示兩個相鄰值之間的差。
arr = np.arange(4, 15, 3)
print('arr={}'.format(arr))
運行結果:
arr=[0 1 2 3 4 5 6 7 8 9]
--------------------------------
arr=[4 5 6 7 8 9]
--------------------------------
arr=[ 4 7 10 13]
七、linespace
import numpy as np
#np.linspace(start, stop, N) 函數返回 N 個在閉區間 [start, stop] 內均勻分佈的數字
#endpoint 表示是否包含最後stop
arr = np.linspace(0, 10, 4, endpoint=False)
print('arr={}'.format(arr))
運行結果:
arr=[0. 2.5 5. 7.5]
八、reshape
import numpy as np
#np.reshape(ndarray, new_shape) 函數會將給定 ndarray 轉換爲指定的 new_shape。
#注意:new_shape 應該與給定 ndarray 中的元素數量保持一致
arr = np.arange(0, 10)
arr_new = np.reshape(arr, (2, 5))
print('arr={}'.format(arr))
print('------------------------')
print(arr_new)
運行結果:
arr=[0 1 2 3 4 5 6 7 8 9]
------------------------
arr_new = [[0 1 2 3 4]
[5 6 7 8 9]]
九、random
import numpy as np
#np.random(shape)函數創建具有給定形狀的 ndarray
# 其中包含位於半開區間 [0.0, 1.0) 內的隨機浮點數。
arr = np.random.random((2, 3))
print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('----------------------------------')
#np.random.randint(start, stop, size = shape) 會創建一個具有給定形狀的 ndarray
# 其中包含在半開區間 [start, stop) 內的隨機整數
arr = np.random.randint(4, 9, size=(3, 4))
print('arr={}'.format(arr))
print('arr.shape={}'.format(arr.shape))
print('----------------------------------')
#np.random.normal(mean, standard deviation, size=shape) 會創建一個具有給定形狀的 ndarray
#其中包含從正態高斯分佈(具有給定均值和標準差)中抽樣的隨機數字
# 我們來創建一個 1,000 arr 1,000 ndarray,其中包含從正態分佈(均值爲 0,標準差爲 0.1)中隨機抽樣的浮點數。
arr = np.random.normal(0, 0.1, size=(1000,1000))
print('arr.shape={}'.format(arr.shape))
print('arr type={}'.format(type(arr)))
print('arr.dtype={}'.format(arr.dtype))
print('arr 的平均值={}'.format(arr.mean()))
print('arr 的最大值={}'.format(arr.max()))
print('arr 的最小值={}'.format(arr.min()))
print('arr 中小於0的元素個數爲:{}'.format((arr < 0).sum()))
print('arr 中大於0的元素個數爲:{}'.format((arr > 0).sum()))
運行結果:
arr=[[0.68767487 0.41654744 0.10873277]
[0.93088438 0.91054255 0.70659358]]
arr.shape=(2, 3)
----------------------------------
arr=[[4 6 7 5]
[8 7 4 5]
[6 8 8 4]]
arr.shape=(3, 4)
----------------------------------
arr.shape=(1000, 1000)
arr type=<class 'numpy.ndarray'>
arr.dtype=float64
arr 的平均值=-8.940630904173296e-05
arr 的最大值=0.48415107922103773
arr 的最小值=-0.49444820736728473
arr 中小於0的元素個數爲:500370
arr 中大於0的元素個數爲:499630