Python numpy

numpy創建

Python list創建array

import numpy as np

arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([[1, 2],[3, 4]])

print(arr1.shape) # 一維shape:(5,)
print(arr2.shape) # 二維shape:(2, 2)
print("-------------------------")
print(arr1.ndim) # 一維維數:1
print(arr2.ndim) # 二維維數:2
print("-------------------------")
print(arr1.size) # 一維元素總數:5
print(arr2.size) # 二維元素總數:4
print("-------------------------")
print(arr1.dtype) # 一維元素類型:int32
print(arr2.dtype) # 二維元素類型:int32

arange創建

import numpy as np

arr1 = np.arange(10) 
arr2 = np.arange(2, 10, 2) #開始,結束,步長

print(arr1) # [0 1 2 3 4 5 6 7 8 9]
print(arr2) # [2 4 6 8]

one創建

import numpy as np

arr1 = np.ones(10)
arr2 = np.ones((2, 3))

print(arr1) # [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
print(arr2) #[[1. 1. 1.], [1. 1. 1.]]
print(arr1.shape) # (10,)
print(arr2.shape) # (2, 3)
print(arr1.dtype) # float64
print(arr2.dtype) # float64

zeros創建

import numpy as np

arr1 = np.zeros(10)
arr2 = np.zeros((2, 3))

print(arr1) # [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
print(arr2) #[[0. 0. 0.],[0. 0. 0.]]
print(arr1.shape) # (10,)
print(arr2.shape) # (2, 3)
print(arr1.dtype) # float64
print(arr2.dtype) # float64

empty創建

import numpy as np

# 內容值爲隨機生成
arr1 = np.empty(3)
arr2 = np.empty((2, 3))

print(arr1) # [2.12199579e-314 2.12199579e-314 2.12199579e-314]
print(arr1.shape) # (3,)
print(arr2) # [[7.08972474e-315 7.09002739e-315 7.08924068e-315],[3.88336902e-316 3.34454656e-316 3.34446079e-316]]
print(arr2.shape) # (2, 3)

full創建

import numpy as np

# 創建指定值的array
arr1 = np.full(10, 6)
arr2 = np.full((2, 3), 6)

print(arr1) # [6 6 6 6 6 6 6 6 6 6]
print(arr2) #[[6 6 6],[6 6 6]]
print(arr1.shape) # (10,)
print(arr2.shape) # (2, 3)
print(arr1.dtype) # int32
print(arr2.dtype) # int32

ones_like, zeros_like, empty_like, full_like創建

import numpy as np

temp = np.array([[1, 2, 3], [4, 5, 6]])
# 創建與輸入array形狀,類型一致,元素全爲1的array
ones = np.ones_like(temp)
# 創建與輸入array形狀,類型一致,元素全爲0的array
zeros = np.zeros_like(temp)
# 創建與輸入array形狀,類型一致,元素隨機生成的array
emptys = np.empty_like(temp)
# 創建與輸入array形狀,類型一致,元素爲指定值的array
fulls = np.full_like(temp, 6)

print(temp.shape) # (2, 3)
print(temp) # [[1 2 3], [4 5 6]]
print("-----------------")
print(ones.shape) # (2, 3)
print(ones) # [[1 1 1], [1 1 1]]
print("-----------------")
print(zeros.shape) # (2, 3)
print(zeros) # [[0 0 0], [0 0 0]]
print("-----------------")
print(emptys.shape) # (2, 3)
print(emptys) # [[57802964        0 57802928],[       0 78882440        0]]
print("-----------------")
print(fulls.shape) # (2, 3)
print(fulls) # [[6 6 6],[6 6 6]]

identity創建

import numpy as np

# identity創建單位矩陣
identity = np.identity(4)
# eye創建, k:對角線下標
eye1 = np.eye(4)
eye2 = np.eye(4, k = 1)
eye3 = np.eye(4, k = -1)

print("identity")
print(identity)
print("-----------------")
print("eye1")
print(eye1)
print("-----------------")
print("eye2")
print(eye2)
print("-----------------")
print("eye3")
print(eye3)

輸出:
identity
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
-----------------
eye1
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
-----------------
eye2
[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]]
-----------------
eye3
[[0. 0. 0. 0.]
 [1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]

random隨機數創建

import numpy as np

arr1 = np.random.randn()
arr2 = np.random.randn(2)
arr3 = np.random.randn(2, 3)

print(type(arr1)) # <class 'float'> 產生一個隨機數
print(arr1) # -0.23415417452114165
print(arr2.shape) # (2,) 產生隨機數組
print(arr2) # [-0.45654992  1.14708534]
print(arr3.shape) # (2, 3) 產生隨機數組
print(arr3) # [[ 1.31220501  1.8120359   1.69368757],[-0.69807866 -0.8124401  -1.23147465]]

numpy索引

基礎索引

import numpy as np

arr1 = np.arange(10)
arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(arr1) # [0 1 2 3 4 5 6 7 8 9]
print(arr2) # [[1 2 3],[4 5 6],[7 8 9]]
print("----------------")
# 一維索引
print(arr1[0]) # 0
print(arr1[1: -1]) # [1 2 3 4 5 6 7 8]
print("----------------")
# 多維索引
print(arr2[1, 1]) # 5
print(arr2[-2, 1]) # 5
print(arr2[1]) # [4 5 6]
print(arr2[-2]) # [4 5 6]
print(arr2[1:3, 1:3]) # [[5 6],[8 9]]
print("----------------")
# numpy對切片的修改會修改原數組
arr3 = np.arange(10)
arr4 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr3) # [0 1 2 3 4 5 6 7 8 9]
print(arr4) # [[1 2 3],[4 5 6],[7 8 9]]
arr3[2:4] = -1
arr4[1:3, 1:3] = -1
print(arr3) # [ 0  1 -1 -1  4  5  6  7  8  9]
print(arr4) # [[ 1  2  3],[ 4 -1 -1],[ 7 -1 -1]]

神奇索引

import numpy as np

temp = np.arange(10)

index1 = np.array([1, 3, 5])
arr1 = temp[index1]

index2 = np.array([[1, 3], [2, 4]])
arr2 = temp[index2]

print(temp) # [0 1 2 3 4 5 6 7 8 9]
print(arr1) # [1 3 5]
print(arr2) # [[1 3],[2 4]]
import numpy as np

temp = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(temp) # [[1 2 3],[4 5 6],[7 8 9]]
print(temp[[0, 2],:]) # [[1 2 3],[7 8 9]]  獲取(0, 0),(0,1),(0, 2),(2, 0),(2, 1),(2, 2)位置的值
print(temp[:, [0]]) # [[1],[4],[7]] 獲取(0, 0),(1,0),(2, 0)位置的值
print(temp[[0, 1], [2, 2]]) # [3 6] 獲取(0, 2)和(1, 2)位置的值

布爾索引

import numpy as np

temp = np.arange(0, 20).reshape(4, 5)

arr1 = temp>3
arr2 = temp[arr1]

arr3 = temp[:, 3] > 3
arr4 = temp[arr3]

arr5 = temp[2, :] > 12
arr6 = temp[:,arr5]

print(temp)
print("-----------------")
print(arr1)
print(arr2)
print("-----------------")
print(arr3)
print(arr4)
print("-----------------")
print(arr5)
print(arr6)

輸出:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
-----------------
[[False False False False  True]
 [ True  True  True  True  True]
 [ True  True  True  True  True]
 [ True  True  True  True  True]]
[ 4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
-----------------
[False  True  True  True]
[[ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
-----------------
[False False False  True  True]
[[ 3  4]
 [ 8  9]
 [13 14]
 [18 19]]

axis

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章