Python NumPy的使用
生活随笔
收集整理的這篇文章主要介紹了
Python NumPy的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NumPy
- 學習目標:
- NumPy的使用
- 學習內容:
- 使用步驟
- Numpy 數組操作
- 1、 數組的拼接
- 2、 數組的分割
- 3、 數組的行列交換
- NumPy 統計函數
- 1、 求和
- 2、 求和函數
- 3、 均值函數
- 4、 中值函數
- 5、 最大值與最小值函數
- 6、 極值函數
- 7、 標準差函數
- 小結
- 對數組中的 nan 填充均值
- 總結
學習目標:
NumPy的使用
環境:
- Anaconda 2.0.4
- Python 3.7.10
- numpy 1.20.2
NumPy安裝:
pip install numPyPython 引入 numpy 庫
import numpy as np學習內容:
Numpy 數組操作
- 1、 數組的拼接
- 2、 數組的分割
- 3、 數組的行列交換
NumPy 統計函數
- 1、求和
- 2、均值
- 3、中值
- 4、最大值
- 5、最小值
- 6、極值
- 7、標準差
使用步驟
Numpy 數組操作
1、 數組的拼接
| hstack | 水平堆疊序列中的數組(列方向) |
| vstack | 豎直堆疊序列中的數組(行方向) |
代碼如下(示例):
import numpy as npt1 = np.arange(12).reshape((2, 6)) # 創建一個 2行6列的數組 print(t1)t2 = np.arange(12, 24).reshape((2, 6)) print(t2) [[ 0 1 2 3 4 5][ 6 7 8 9 10 11]][[12 13 14 15 16 17][18 19 20 21 22 23]] t3 = np.vstack((t1, t2)) # 豎直拼接 print(t3) [[ 0 1 2 3 4 5][ 6 7 8 9 10 11][12 13 14 15 16 17][18 19 20 21 22 23]] t4 = np.hstack((t1, t2)) # 水平拼接 print(t4) [[ 0 1 2 3 4 5 12 13 14 15 16 17][ 6 7 8 9 10 11 18 19 20 21 22 23]]2、 數組的分割
| split | 將一個數組分割為多個子數組 |
| hsplit | 將一個數組水平分割為多個子數組(按列) |
| vsplit | 將一個數組垂直分割為多個子數組(按行) |
numpy.split(ary, indices_or_sections, axis)
- axis:設置沿著哪個方向進行切分,默認為 0,橫向切分,即水平方向。為 1 時,縱向切分,即豎直方向。
代碼如下(示例):
import numpy as npt1 = np.arange(12).reshape((2, 6)) t2 = np.arange(12, 24).reshape((2, 6)) t3 = np.vstack((t1, t2)) # 將上面2個數組進行豎直拼接 print(t3) t4 = np.hstack((t1, t2)) # 將t1 t2兩個數組進行水平拼接 print(t4) print(np.split(t3, 2, axis=0)) # 使用np.split()函數將上面的數組進行橫向分割 [array([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],[18, 19, 20, 21, 22, 23]])] print(np.split(t3, 2, axis=1)) # 使用np.split()函數將上面的數組進行縱向分割 [array([[ 0, 1, 2],[ 6, 7, 8],[12, 13, 14],[18, 19, 20]]), array([[ 3, 4, 5],[ 9, 10, 11],[15, 16, 17],[21, 22, 23]])]注:axis 為 0 時在水平方向分割,axis 為 1 時在垂直方向分割。
print(np.hsplit(t4, 2)) # 用 np.hsplit()函數水平分割分割成2個數組 [array([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],[18, 19, 20, 21, 22, 23]])] print(np.vsplit(t3, 2)) # 用 np.vsplit()函數 垂直分隔成2個數組 [array([[ 0, 1, 2, 3, 4, 5],[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],[18, 19, 20, 21, 22, 23]])] print(np.vsplit(t3, 4)) # 用 np.vsplit()函數 垂直分隔成4個數組 [array([[0, 1, 2, 3, 4, 5]]), array([[ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17]]), array([[18, 19, 20, 21, 22, 23]])]3、 數組的行列交換
代碼如下(示例):
t = np.arange(12, 24).reshape(3, 4) print(t) [[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]] print("-------------行交換-------------") t[[1, 2], :] = t[[2, 1], :] # 行交換 第2行與第三行進行交換 print(t) [[ 0 1 2 3][ 8 9 10 11][ 4 5 6 7]] print("-------------列交換-------------") t = np.arange(0, 12).reshape(3, 4) t[:, [0, 2]] = t[:, [2, 0]] # 列交換 第1列與第3列進行交換,每次交換都會改變原數組 print(t) [[ 2 1 0 3][ 6 5 4 7][10 9 8 11]]NumPy 統計函數
| sum | 求和 |
| mean | 返回數組中元素的算術平均值 |
| np.median | 中值 |
| max | 最大值 |
| min | 最小值 |
| ptp | 極值 |
| std | 標準差 |
注:標準差是一組數據平均值分散程度的一種度量。一個較大的標準差,代表大部分數值和其平均值之間差異較大;一個較小的標準差,代表這些數值較接近平均值反映出數據的波動穩定情況,越大表示波動越大,越不穩定。
1、 求和
代碼如下(示例):
t1 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, np.nan, 15], [16, 17, 18, 19, 20]]) t2 = np.arange(12).reshape((4, 3)) # 創建 4行 3列的數組 print(t1) print(t2) [[ 0. 2. 3. 4. 5.][ 0. 7. 8. 9. 10.][ 0. 12. 13. nan 15.][ 0. 17. 18. 19. 20.]][[ 0 1 2][ 3 4 5][ 6 7 8][ 9 10 11]] print(np.sum(t2)) # 計算t2數組所有的和 66 print(np.sum(t2, axis=0)) # 計算t2數組每一行相加的結果 [18 22 26] print(np.sum(t2, axis=1)) # 計算t2數組每一列相加的結果 [ 3 12 21 30] print(np.sum(t1, axis=0)) # 計算t1數組每一行相加的結果 [ 0. 38. 42. nan 50.] print(np.sum(t1, axis=1)) # 計算t1數組每一列相加的結果 [14. 34. nan 74.]注意:nan和任何值計算都為nan
2、 求和函數
# numpy中常用統計函數 t2 = np.arange(12).reshape((4, 3)) print(t2.sum(axis=0)) # 求和 [18 22 26]3、 均值函數
print(t2.mean(axis=0)) # 均值 [4.5 5.5 6.5]4、 中值函數
print(np.median(t2)) # 中值 print(np.median(t2, axis=0)) # 中值 5.5 [4.5 5.5 6.5]5、 最大值與最小值函數
print(t2.max(axis=0)) # 最大值 print(t2.min(axis=0)) # 最小值 [ 9 10 11] [0 1 2]6、 極值函數
print(np.ptp(t2)) # 極值, 即最大值和最小值之差 print(np.ptp(t2, axis=0)) 11 [9 9 9]7、 標準差函數
print(t2.std(axis=0)) # 標準差 [3.35410197 3.35410197 3.35410197]小結
對數組中的 nan 填充均值
# coding=utf-8 import numpy as np# 為每一列的nan填充均值 def fill_column_ndarray(t1):for i in range(t1.shape[1]): # 遍歷每一列temp_col = t1[:, i] # 當前的一列nan_num = np.count_nonzero(temp_col != temp_col)if nan_num != 0: # 不為0,說明當前這一列中有nantemp_not_nan_col = temp_col[temp_col == temp_col] # 當前一列不為nan的array# 選中當前為nan的位置,把值賦給為不為nan的均值temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()return t1# 為每一行的nan填充均值 def fill_row_ndarray(t2):for i in range(t1.shape[0]): # 遍歷每一行temp_row = t1[i, :] # 當前的一行nan_num = np.count_nonzero(temp_row != temp_row)if nan_num != 0: # 不為0,說明當前這一列中有nantemp_not_nan_row = temp_row[temp_row == temp_row] # 當前一行不為nan的array# 選中當前為nan的位置,把值賦給為不為nan的均值temp_row[np.isnan(temp_row)] = temp_not_nan_row.mean()return t1if __name__ == '__main__':t1 = np.arange(12).reshape((3, 4)).astype("float")t1[1, 0:] = np.nan # 給數組第二行賦值print("原數組:")print(t1)t1 = fill_column_ndarray(t1)print("豎向填充均值:")print(t1)print("*" * 25)t2 = np.arange(12).reshape((3, 4)).astype("float")t2[:, 2] = np.nan # 給數組的第三列賦值print("原數組:")print(t2)t2 = fill_row_ndarray(t2)print("橫向填充均值:")print(t2) 原數組: [[ 0. 1. 2. 3.][nan nan nan nan][ 8. 9. 10. 11.]] 豎向填充均值: [[ 0. 1. 2. 3.][ 4. 5. 6. 7.][ 8. 9. 10. 11.]] ************************* 原數組: [[ 0. 1. nan 3.][ 4. 5. nan 7.][ 8. 9. nan 11.]] 橫向填充均值: [[ 0. 1. 2. 3.][ 4. 5. 6. 7.][ 8. 9. 10. 11.]]總結
1.Numpy 中包含了一些函數用于處理數組,大概可分為以下幾類:
- 修改數組形狀
- 翻轉數組
- 修改數組維度
- 連接數組
- 分割數組
- 數組元素的添加與刪除
2.nan和任何值計算都為nan。
3.axis 為 0 時在水平方向,axis 為 1 時在垂直方向。
總結
以上是生活随笔為你收集整理的Python NumPy的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NoneBot2插件——进群欢迎
- 下一篇: 安装Terminator和快捷键使用