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)
print(arr2.shape)
print("-------------------------")
print(arr1.ndim)
print(arr2.ndim)
print("-------------------------")
print(arr1.size)
print(arr2.size)
print("-------------------------")
print(arr1.dtype)
print(arr2.dtype)
arange创建
import numpy as np
arr1 = np.arange(10)
arr2 = np.arange(2, 10, 2)
print(arr1)
print(arr2)
one创建
import numpy as np
arr1 = np.ones(10)
arr2 = np.ones((2, 3))
print(arr1)
print(arr2)
print(arr1.shape)
print(arr2.shape)
print(arr1.dtype)
print(arr2.dtype)
zeros创建
import numpy as np
arr1 = np.zeros(10)
arr2 = np.zeros((2, 3))
print(arr1)
print(arr2)
print(arr1.shape)
print(arr2.shape)
print(arr1.dtype)
print(arr2.dtype)
empty创建
import numpy as np
arr1 = np.empty(3)
arr2 = np.empty((2, 3))
print(arr1)
print(arr1.shape)
print(arr2)
print(arr2.shape)
full创建
import numpy as np
arr1 = np.full(10, 6)
arr2 = np.full((2, 3), 6)
print(arr1)
print(arr2)
print(arr1.shape)
print(arr2.shape)
print(arr1.dtype)
print(arr2.dtype)
ones_like, zeros_like, empty_like, full_like创建
import numpy as np
temp = np.array([[1, 2, 3], [4, 5, 6]])
ones = np.ones_like(temp)
zeros = np.zeros_like(temp)
emptys = np.empty_like(temp)
fulls = np.full_like(temp, 6)
print(temp.shape)
print(temp)
print("-----------------")
print(ones.shape)
print(ones)
print("-----------------")
print(zeros.shape)
print(zeros)
print("-----------------")
print(emptys.shape)
print(emptys)
print("-----------------")
print(fulls.shape)
print(fulls)
identity创建
import numpy as np
identity = np.identity(4)
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