【小睿的ML之路】Numpy常用函数

发布时间 2023-09-14 00:42:11作者: 郭小睿
import numpy as np
a = np.arange(3)
print(a)
[0 1 2]
print(np.exp(a)) # 指数运算 e^0  e^1 e^2
[1.         2.71828183 7.3890561 ]
print(np.sqrt(a)) # 计算每个元素的平方根
[0.         1.         1.41421356]
b = 10 * np.random.random((3,4))
print(b)
c = np.floor(b) # 向下取整
print(c)
[[0.89198914 9.40225391 3.75857859 8.45085184]
 [2.58700305 9.79247821 5.17718314 4.84501262]
 [5.60284687 3.46467384 1.20591779 6.01311384]]
[[0. 9. 3. 8.]
 [2. 9. 5. 4.]
 [5. 3. 1. 6.]]
print(c.ravel()) # 将多维数组中的所有元素按照某种顺序排列成一个一维数组,返回这个一维数组的视图
[0. 9. 3. 8. 2. 9. 5. 4. 5. 3. 1. 6.]
c.shape = (6,2) # 设置纬度
print(c)

print(c.T) # 使用 .T 进行矩阵转置
[[0. 9.]
 [3. 8.]
 [2. 9.]
 [5. 4.]
 [5. 3.]
 [1. 6.]]
[[0. 3. 2. 5. 5. 1.]
 [9. 8. 9. 4. 3. 6.]]
d = np.floor(10*np.random.random((2,2)))
print(d)
e = np.floor(10*np.random.random((2,2)))
print(e)

print(np.vstack((d,e))) # 使用 np.vstack() 垂直堆叠这两个矩阵
print(np.hstack((d,e))) # 使用 np.hstack() 水平堆叠这两个矩阵
[[5. 5.]
 [2. 2.]]
[[9. 3.]
 [6. 0.]]
[[5. 5.]
 [2. 2.]
 [9. 3.]
 [6. 0.]]
[[5. 5. 9. 3.]
 [2. 2. 6. 0.]]
f = np.floor(10*np.random.random((2,12)))
print(f)
print('-'*10)

print(np.hsplit(f,3)) # 水平平均切分3份

print(np.hsplit(f,(3,4))) # 在指定的分割点处切分
[[3. 9. 2. 1. 8. 0. 8. 1. 0. 3. 0. 0.]
 [3. 8. 8. 9. 9. 0. 7. 0. 8. 0. 2. 2.]]
----------
[array([[3., 9., 2., 1.],
       [3., 8., 8., 9.]]), array([[8., 0., 8., 1.],
       [9., 0., 7., 0.]]), array([[0., 3., 0., 0.],
       [8., 0., 2., 2.]])]
[array([[3., 9., 2.],
       [3., 8., 8.]]), array([[1.],
       [9.]]), array([[8., 0., 8., 1., 0., 3., 0., 0.],
       [9., 0., 7., 0., 8., 0., 2., 2.]])]
g = np.floor(10*np.random.random((8,2)))
print(g)
print('-'*20)
print(np.hsplit(g,2))
print('-'*20)
print(np.vsplit(g,2)) # 垂直平均切分3份
[[7. 1.]
 [8. 1.]
 [3. 2.]
 [5. 0.]
 [4. 0.]
 [1. 7.]
 [7. 4.]
 [2. 2.]]
--------------------
[array([[7.],
       [8.],
       [3.],
       [5.],
       [4.],
       [1.],
       [7.],
       [2.]]), array([[1.],
       [1.],
       [2.],
       [0.],
       [0.],
       [7.],
       [4.],
       [2.]])]
--------------------
[array([[7., 1.],
       [8., 1.],
       [3., 2.],
       [5., 0.]]), array([[4., 0.],
       [1., 7.],
       [7., 4.],
       [2., 2.]])]
h = np.arange(12)
i = h # 深拷贝,shape等属性一致
print(i is h)
i.shape = 3,4

print(i.shape)
print(h.shape)

print(id(i))
print(id(h))

print(i)
print(h)
True
(3, 4)
(3, 4)
2303479872464
2303479872464
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
j = h.view() # 浅拷贝,这种很少用,虽然shape等属性不一致了,但是数据还是共用的
print(j)

print(j is h)
j.shape = 2,6

print(j.shape)
print(h.shape)

j[0,2 ]= 123 # 但是,a的值也会变化

print(j)
print(id(j))

print(h)
print(id(h))
[[  0   1 123   3]
 [  4   5   6   7]
 [  8   9  10  11]]
False
(2, 6)
(3, 4)
[[  0   1 123   3   4   5]
 [  6   7   8   9  10  11]]
2303484963760
[[  0   1 123   3]
 [  4   5   6   7]
 [  8   9  10  11]]
2303479872464
k = h.copy() # 完成复制后,修改变量中的内容,不会影响引用的变量(经常用)
print(k)
print(k is h)

k[0,1]=9999
print(k)
print('-'*10)
print(h)
[[  0   1 123   3]
 [  4   5   6   7]
 [  8   9  10  11]]
False
[[   0 9999  123    3]
 [   4    5    6    7]
 [   8    9   10   11]]
----------
[[  0   1 123   3]
 [  4   5   6   7]
 [  8   9  10  11]]
data = np.sin(np.arange(20)).reshape(5,4)
print(data)
[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
ind = data.argmax(axis=0) #使用 argmax() 函数找到每列中最大元素的索引
print(ind)

data_max = data[ind,range(data.shape[1])] #  data.shape[1]获取二维数组的列数
print(data_max)
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]
x = np.arange(0,40,10)
print(x)

# 使用 np.tile() 函数在行方向上重复 x 3 次,在列方向上重复 5 次
y = np.tile(x,(3,5))
print(y)
[ 0 10 20 30]
[[ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30  0 10 20 30]]
z = np.array([[4,3,5],[1,2,1]])
print(z)
print('-'*10)

t = np.sort(z,axis=1)
print(t)
print('-'*10)

u = np.array([4,3,1,2])
print(u)

print(np.sort(u))

v = np.argsort(u) # 使用 np.argsort() 获取 u 中元素的排序索引
print(v)

print(u[v]) # 使用排序索引 v 来获取排序后的数组 u
[[4 3 5]
 [1 2 1]]
----------
[[3 4 5]
 [1 1 2]]
----------
[4 3 1 2]
[1 2 3 4]
[2 3 1 0]
[1 2 3 4]