1. 程式人生 > >python---Numpy模塊中數組運算的常用代碼示例

python---Numpy模塊中數組運算的常用代碼示例

元素訪問 數組大小 and 判斷 array 調整 轉換 att tostring

import numpy as np

# Numpy數組操作
print(========訪問列表元素, 切片,賦值===========)
arr = np.array([2., 6., 5., 5.])
print(arr[:3])
print(arr[3])
arr[0] = 5.
print(arr)
print(========數組唯一性元素===========)
print(np.unique(arr))
print(========數組排序,排序索引===========)
print(np.sort(arr))
print(np.argsort(arr))
print(========將數組亂序重排===========) np.random.shuffle(arr) print(arr) print(========數組相等性比較===========) print(np.array_equal(arr, np.array([1., 3., 2.]))) print(========二維數組(矩陣)的元素訪問===========) matrix = np.array([[4., 5., 6.], [2, 3, 6]], float) print(matrix) print(matrix[0, 0]) print(matrix[0, 2])
print(========對數組的各維進行切片操作===========) print(matrix[1:2,2:3]) print(matrix[1, :]) print(matrix[:, 2]) print(matrix[-1:, -2:]) print(========將多維數組拉平為一維數組===========) print(matrix.flatten()) print(========獲取數組大小信息===========) print(matrix.shape) print(========獲取數組元素的類型===========) print
(matrix.dtype) print(========數組的數據類型轉換===========) int_arr = matrix.astype(np.int32) print(int_arr) print(int_arr.dtype) print(========獲取數組第一維的長度===========) print(len(matrix)) print(========判斷數組是否包含元素===========) print(2 in matrix) print(0 in matrix) print(========調整數組維度===========) arr = np.array(range(8), float) print(arr) re_arr = arr.reshape((4, 2)) print(re_arr) print(========矩陣的轉置運算===========) print(re_arr.transpose()) print(========使用數組的T屬性實現轉置===========) matrix = np.arange(15).reshape(3, 5) print(matrix) print(matrix.T) print(========使用newaxis調整元素位置,增加維度===========) arr = np.array([14, 32, 13], float) print(arr) print(arr[:, np.newaxis]) print(arr[:, np.newaxis].shape) print(arr[np.newaxis, :]) print(arr[np.newaxis, :].shape) print(========數組的連接===========) arr1 = np.array([10, 22], float) arr2 = np.array([31, 43, 54, 61], float) arr3 = np.array([71, 82, 29], float) print(np.concatenate((arr1, arr2, arr3))) print(========數組連接時,指定具體的條軸===========) arr1 = np.array([[11, 12], [32, 42]], float) arr2 = np.array([[54, 26], [27, 28]], float) print(np.concatenate((arr1, arr2))) print(np.concatenate((arr1, arr2), axis=0)) print(np.concatenate((arr1, arr2), axis=1)) print(========二進制字符串和數組之間的轉換,fromstring已升級為frombuffer===========) arr = np.array([10, 20, 30], float) str = arr.tostring() print(str) print(np.frombuffer(str))

PS C:\test> & C:/Python37/python.exe c:/test/ml.py
========訪問列表元素, 切片,賦值===========
[2. 6. 5.]
5.0
[5. 6. 5. 5.]
========數組唯一性元素===========
[5. 6.]
========數組排序,排序索引===========
[5. 5. 5. 6.]
[0 2 3 1]
========將數組亂序重排===========
[5. 5. 6. 5.]
========數組相等性比較===========
False
========二維數組(矩陣)的元素訪問===========
[[4. 5. 6.]
 [2. 3. 6.]]
4.0
6.0
========對數組的各維進行切片操作===========
[[6.]]
[2. 3. 6.]
[6. 6.]
[[3. 6.]]
========將多維數組拉平為一維數組===========
[4. 5. 6. 2. 3. 6.]
========獲取數組大小信息===========
(2, 3)
========獲取數組元素的類型===========
float64
========數組的數據類型轉換===========
[[4 5 6]
 [2 3 6]]
int32
========獲取數組第一維的長度===========
2
========判斷數組是否包含元素===========
True
False
========調整數組維度===========
[0. 1. 2. 3. 4. 5. 6. 7.]
[[0. 1.]
 [2. 3.]
 [4. 5.]
 [6. 7.]]
========矩陣的轉置運算===========
[[0. 2. 4. 6.]
 [1. 3. 5. 7.]]
========使用數組的T屬性實現轉置===========
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
[[ 0  5 10]
 [ 1  6 11]
 [ 2  7 12]
 [ 3  8 13]
 [ 4  9 14]]
========使用newaxis調整元素位置,增加維度===========
[14. 32. 13.]
[[14.]
 [32.]
 [13.]]
(3, 1)
[[14. 32. 13.]]
(1, 3)
========數組的連接===========
[10. 22. 31. 43. 54. 61. 71. 82. 29.]
========數組連接時,指定具體的條軸===========
[[11. 12.]
 [32. 42.]
 [54. 26.]
 [27. 28.]]
[[11. 12.]
 [32. 42.]
 [54. 26.]
 [27. 28.]]
[[11. 12. 54. 26.]
 [32. 42. 27. 28.]]
========二進制字符串和數組之間的轉換,fromstring已升級為frombuffer===========
b\x00\x00\x00\x00\x00\[email protected]\x00\x00\x00\x00\x00\[email protected]\x00\x00\x00\x00\x00\x00>@
[10. 20. 30.]

python---Numpy模塊中數組運算的常用代碼示例