numpy——科學計算庫的講解(五)
QQ:3020889729 小蔡
本節主要講解:numpy廣播運算講解(加法爲例)。
引入numpy庫
import numpy as np
創建初始數據矩陣
1.首先,創建隨機元素的矩陣
# 3*3矩陣
data_one = np.random.randint(1, 10, (3, 3))
# 4*4矩陣
data_two = np.random.randint(1, 10, (4, 4))
2.查看矩陣數據
print("data_one(3*3)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(data_one.size, data_one.ndim, data_one, data_one.dtype))
print("") # 輸出空行,美觀
print("data_two(4*4)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(data_two.size, data_two.ndim, data_two, data_two.dtype))
結果展示:
data_one(3*3)
矩陣大小:size = 9
矩陣軸數:ndim = 2
矩陣數據:
[[9 2 4]
[2 2 8]
[8 3 5]] , dtype = int32
data_two(4*4)
矩陣大小:size = 16
矩陣軸數:ndim = 2
矩陣數據:
[[4 7 7 9]
[1 3 9 8]
[8 4 6 8]
[4 2 2 4]] , dtype = int32
創建用於行廣播的矩陣數據
一、創建不同大小的廣播-行矩陣,來進行廣播運算:
1.——1 * 1
2.——1 * 3
3.——1 * 4
4.——1 * 5
# 1*1的行矩陣
one_rows_1 = np.random.randint(1, 10, (1, 1))
# 1*3的行矩陣
one_rows_2 = np.random.randint(1, 10, (1, 3))
# 1*4的行矩陣
one_rows_3 = np.random.randint(1, 10, (1, 4))
# 1*5的行矩陣
one_rows_4 = np.random.randint(1, 10, (1, 5))
二、查看矩陣數據
print("one_rows_1(1*1)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_rows_1.size, one_rows_1.ndim, one_rows_1, one_rows_1.dtype))
print("") # 輸出空行,美觀
print("one_rows_2(1*3)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_rows_2.size, one_rows_2.ndim, one_rows_2, one_rows_2.dtype))
print("") # 輸出空行,美觀
print("one_rows_3(1*4)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_rows_3.size, one_rows_3.ndim, one_rows_3, one_rows_3.dtype))
print("") # 輸出空行,美觀
print("one_rows_4(1*5)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_rows_4.size, one_rows_4.ndim, one_rows_4, one_rows_4.dtype))
結果展示:
one_rows_1(1*1)
矩陣大小:size = 1
矩陣軸數:ndim = 2
矩陣數據:
[[2]] , dtype = int32
one_rows_2(1*3)
矩陣大小:size = 3
矩陣軸數:ndim = 2
矩陣數據:
[[1 8 3]] , dtype = int32
one_rows_3(1*4)
矩陣大小:size = 4
矩陣軸數:ndim = 2
矩陣數據:
[[3 1 4 9]] , dtype = int32
one_rows_4(1*5)
矩陣大小:size = 5
矩陣軸數:ndim = 2
矩陣數據:
[[2 3 5 9 8]] , dtype = int32
創建用於列廣播的矩陣數據
一、創建不同大小的廣播-列矩陣,來進行廣播運算:
1.——1 * 1
2.——3 * 1
3.——4 * 1
4.——5 * 1
# 1*1的行矩陣
one_cols_1 = np.random.randint(1, 10, (1, 1))
# 1*3的行矩陣
one_cols_2 = np.random.randint(1, 10, (3, 1))
# 1*4的行矩陣
one_cols_3 = np.random.randint(1, 10, (4, 1))
# 1*5的行矩陣
one_cols_4 = np.random.randint(1, 10, (5, 1))
# 1*1的行矩陣
one_cols_1 = np.random.randint(1, 10, (1, 1))
# 1*3的行矩陣
one_cols_2 = np.random.randint(1, 10, (3, 1))
# 1*4的行矩陣
one_cols_3 = np.random.randint(1, 10, (4, 1))
# 1*5的行矩陣
one_cols_4 = np.random.randint(1, 10, (5, 1))
二、數據查看
print("one_cols_1(1*1)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_cols_1.size, one_cols_1.ndim, one_cols_1, one_cols_1.dtype))
print("") # 輸出空行,美觀
print("one_cols_2(3*1)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_cols_2.size, one_cols_2.ndim, one_cols_2, one_cols_2.dtype))
print("") # 輸出空行,美觀
print("one_cols_3(4*1)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_cols_3.size, one_cols_3.ndim, one_cols_3, one_cols_3.dtype))
print("") # 輸出空行,美觀
print("one_cols_4(5*1)\n矩陣大小:size = {0}\n矩陣軸數:ndim = {1}\n矩陣數據:\n{2} , dtype = {3}".format(one_cols_4.size, one_cols_4.ndim, one_cols_4, one_cols_4.dtype))
結果展示:
one_cols_1(1*1)
矩陣大小:size = 1
矩陣軸數:ndim = 2
矩陣數據:
[[7]] , dtype = int32
one_cols_2(3*1)
矩陣大小:size = 3
矩陣軸數:ndim = 2
矩陣數據:
[[8]
[8]
[4]] , dtype = int32
one_cols_3(4*1)
矩陣大小:size = 4
矩陣軸數:ndim = 2
矩陣數據:
[[7]
[7]
[8]
[4]] , dtype = int32
one_cols_4(5*1)
矩陣大小:size = 5
矩陣軸數:ndim = 2
矩陣數據:
[[6]
[5]
[2]
[5]
[2]] , dtype = int32
加法廣播
行廣播運算
1.使用3*3的data_one矩陣進行廣播運算
①
# 與1*1的矩陣進行廣播運算
print("data_one:\n{0}".format(data_one))
print("") # 輸出空行,美觀
print("one_rows_1:\n{0}".format(one_rows_1))
print("") # 輸出空行,美觀
print("data_one + one_rows_1:\n{0}".format(data_one + one_rows_1))
結果展示:
data_one:
[[9 2 4]
[2 2 8]
[8 3 5]]
one_rows_1:
[[2]]
data_one + one_rows_1:
[[11 4 6]
[ 4 4 10]
[10 5 7]]
②
# 與1*3的矩陣進行廣播運算
print("data_one:\n{0}".format(data_one))
print("") # 輸出空行,美觀
print("one_rows_2:\n{0}".format(one_rows_2))
print("") # 輸出空行,美觀
print("data_one + one_rows_2:\n{0}".format(data_one + one_rows_2))
結果展示:
data_one:
[[9 2 4]
[2 2 8]
[8 3 5]]
one_rows_2:
[[1 8 3]]
data_one + one_rows_2:
[[10 10 7]
[ 3 10 11]
[ 9 11 8]]
③
# 與1*4的矩陣進行廣播運算——報錯
# print("data_one:\n{0}".format(data_one))
# print("") # 輸出空行,美觀
# print("one_rows_3:\n{0}".format(one_rows_3))
# print("") # 輸出空行,美觀
# print("data_one + one_rows_3:\n{0}".format(data_one + one_rows_3))
④
# 與1*5的矩陣進行廣播運算——報錯
# print("data_one:\n{0}".format(data_one))
# print("") # 輸出空行,美觀
# print("one_rows_4:\n{0}".format(one_rows_4))
# print("") # 輸出空行,美觀
# print("data_one + one_rows_4:\n{0}".format(data_one + one_rows_4))
行廣播加法運算總結
列廣播運算
使用4*4的data_two矩陣進行廣播運算
①
# 與1*1的矩陣進行廣播運算
print("data_two:\n{0}".format(data_two))
print("") # 輸出空行,美觀
print("one_cols_1:\n{0}".format(one_cols_1))
print("") # 輸出空行,美觀
print("data_two + one_cols_1:\n{0}".format(data_two + one_cols_1))
②
# 與1*1的矩陣進行廣播運算
print("data_two:\n{0}".format(data_two))
print("") # 輸出空行,美觀
print("one_cols_1:\n{0}".format(one_cols_1))
print("") # 輸出空行,美觀
print("data_two + one_cols_1:\n{0}".format(data_two + one_cols_1))
結果展示:
data_two:
[[4 7 7 9]
[1 3 9 8]
[8 4 6 8]
[4 2 2 4]]
one_cols_1:
[[7]]
data_two + one_cols_1:
[[11 14 14 16]
[ 8 10 16 15]
[15 11 13 15]
[11 9 9 11]]
③
# 與3*1的矩陣進行廣播運算——報錯
# print("data_two:\n{0}".format(data_two))
# print("") # 輸出空行,美觀
# print("one_cols_2:\n{0}".format(one_cols_2))
# print("") # 輸出空行,美觀
# print("data_two + one_cols_2:\n{0}".format(data_two + one_cols_2))
④
# 與4*1的矩陣進行廣播運算
print("data_two:\n{0}".format(data_two))
print("") # 輸出空行,美觀
print("one_cols_3:\n{0}".format(one_cols_3))
print("") # 輸出空行,美觀
print("data_two + one_cols_3:\n{0}".format(data_two + one_cols_3))
結果展示:
data_two:
[[4 7 7 9]
[1 3 9 8]
[8 4 6 8]
[4 2 2 4]]
one_cols_3:
[[7]
[7]
[8]
[4]]
data_two + one_cols_3:
[[11 14 14 16]
[ 8 10 16 15]
[16 12 14 16]
[ 8 6 6 8]]
⑥
# 與5*1的矩陣進行廣播運算——報錯
# print("data_two:\n{0}".format(data_two))
# print("") # 輸出空行,美觀
# print("one_cols_4:\n{0}".format(one_cols_4))
# print("") # 輸出空行,美觀
# print("data_two + one_cols_4:\n{0}".format(data_two + one_cols_4))