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

在这里插入图片描述

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