文章目錄
- 數組的創建
- 創建全0的二維數組a(3,3)
- 全1的二維數組b(3,4)
- 隨機數二維數數組c(2,3)
-
- 數組的屬性
-
- 數組的維度操作
- 將數組c的行變列,返回最后一個元素
- 返回數組c第2到第4個元素
- 返回逆序的數組
-
- 數組的合并
- 數組a與數組c的水平合并(可根據需要進行數組轉置)
- 數組a與數組c垂直合并(可根據需要進行數組轉置)
- 數組c與數組c深度合并
-
- 數組運算
-
數組的創建
創建全0的二維數組a(3,3)
import numpy as np
a = np.zeros((3, 3))
print("全0的二維數組a(3,3):")
print(a)
全1的二維數組b(3,4)
b = np.ones((3, 4))
print("\n全1的二維數組b(3,4):")
print(b)
隨機數二維數數組c(2,3)
c = np.random.rand(2, 3)
print("\n隨機數二維數數組c(2,3):")
print(c)
效果截圖

數組的屬性
查看b數組的維度
print("數組b的維度:", b.shape)
查看b數組元素的個數
print("數組b的元素個數:", b.size)
效果截圖

數組的維度操作
將數組c的行變列,返回最后一個元素
transposed_c = c.T
last_element = transposed_c.flatten()[-1]
print("數組c經行列變換后的最后一個元素:", last_element)
返回數組c第2到第4個元素
elements_2_to_4 = c.flatten()[1:4]
print("\n數組c第2到第4個元素:", elements_2_to_4)
返回逆序的數組
reversed_c = np.flip(c)
print("\n逆序的數組c:")
print(reversed_c)
效果截圖

數組的合并
數組a與數組c的水平合并(可根據需要進行數組轉置)
horizontal_merge = np.hstack((a, c.T))
print("數組a與數組c的水平合并:")
print(horizontal_merge)
數組a與數組c垂直合并(可根據需要進行數組轉置)
vertical_merge = np.vstack((a.T, c))
print("\n數組a與數組c的垂直合并:")
print(vertical_merge)
數組c與數組c深度合并
depth_merge = np.dstack((c, c))
print("\n數組c與數組c的深度合并:")
print(depth_merge)
效果截圖

數組運算
數組c加2
c_plus_2 = c + 2
print("數組c加2后的結果:")
print(c_plus_2)
數組c所有元素的和、平均值、標準差
sum_c = np.sum(c)
mean_c = np.mean(c)
std_c = np.std(c)
print("\n數組c所有元素的和:", sum_c)
print("數組c所有元素的平均值:", mean_c)
print("數組c所有元素的標準差:", std_c)
效果截圖
