numpy基本用法

2022-07-27,

numpy

1.reshape 变换形状

import numpy as np
#建立一维数组
t1=np.arange(12)
#转为3行4列
t2=t1.reshape((3,4))
t2
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

此时有两种变化形式

括号中第二个数字的不同

第一种情况

#reshape
import numpy as np
#建立一维数组
t1=np.arange(12)
t2=t1.reshape((12,1))
t2
array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11]])

第二种情况

#reshape
import numpy as np
#建立一维数组
t1=np.arange(12)
#转为一维数组
t2=t1.reshape((12,))
t2
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

上面是一个一维数组,下面是一个二维数组,当括号里面只有一个数字才代表一维!!!

#reshape
import numpy as np
#建立一维数组
t1=np.arange(12)

t2=t1.reshape((1,12))
t2
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]])

2.扁平处理

有点像三体里面的降维打击(相当于把蚊子一个三维生物拍死变成二维)
这里是把多维变成一维

#reshape
import numpy as np
#建立一维数组
t1=np.arange(12)
t2=t1.reshape((3,4))
#压缩为一维
t3=t2.flatten()
print("t2:\n",t2)
print("t3:\n",t3)

利用flatten函数压缩为一维

t2:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
t3:
 [ 0  1  2  3  4  5  6  7  8  9 10 11]

3.数组的计算

(1) 广播相加

#reshape
import numpy as np
#建立一维数组
t1=np.arange(12)
t2=t1.reshape((3,4))
#数组+数字
t3=t2+2
print("t2:\n",t2)
print("t3:\n",t3)

对矩阵中每个数字广播加2

t2:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
t3:
 [[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]

(2)对应相加

#reshape
import numpy as np
#建立一维数组
t1=np.arange(12)
t2=t1.reshape((3,4))
#数组+数字
t3=t2+2
t4=t2+t3
print("t2:\n",t2)
print("t3:\n",t3)
print("t4:\n",t4)
t2:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
t3:
 [[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
t4:
 [[ 2  4  6  8]
 [10 12 14 16]
 [18 20 22 24]]

(3)其他运算同理,但是这里不能用线性代数中矩阵概念理解

4.读取本地数据

只是有这个方法,但是大部分情况是用pandas库去读取数据,它的功能更强大

np.loadtxt()

5.索引和切片

建立一个(4,6)的数组
本小节均以此数组为例

import numpy as np
t1=np.arange(24).reshape((4,6))
t1

打印t1

array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])

(1) 取某一行

直接加上索引

print(t1[2])
[12 13 14 15 16 17]

(2) 取连续多行

从第一行到最后一行

print(t1[1:])
[[ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

(3) 取不连续行

嵌套一个方括号

print(t1[[0,2,3]])
[[ 0  1  2  3  4  5]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

(4) 取值既跟行有关系也跟列有关系时

这里的 “ :” 可以看作左闭右开区间
取第一行,所有列
print(t1[1,:])
[ 6  7  8  9 10 11]
取从第一行开始的所有列
print(t1[1:,:])
[[ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

取所有行,第一列
print(t1[:,1])
[ 1 7 13 19]
取2,3行的第一列

只需把行那块多加个[ ]添加想取的行

print(t1[[2,3],1])
[13 19]
取所有行,第3列开始的每一列
print(t1[:,3:])
[[ 3  4  5]
 [ 9 10 11]
 [15 16 17]
 [21 22 23]]
第0行到第1行,第1列到第二列

*这里的冒号可以看作 [0-2) *

print(t1[0:2,1:3])
取第0行第1列,第1行第2列,第2行第3列(取不相邻的点)
print(t1[[0,1,2],[1,2,3]])
[1 8 15]
取最后一列
print(t1[:,-1])
[ 5 11 17 23]

6.数值的修改 替换

将数组中小于10的替换为3
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
t1[t1<10]=3
print(t1)
[[ 3  3  3  3  3  3]
 [ 3  3  3  3 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
提取大于15的值
print(t1[t1>15])
[16 17 18 19 20 21 22 23]
将小于10的变成0,大于10的变成20

用到np.where(),类似于三元运算

t1=np.where(t1<10,0,20)
print(t1)
[[ 0  0  0  0  0  0]
 [ 0  0  0  0 20 20]
 [20 20 20 20 20 20]
 [20 20 20 20 20 20]]

7.数组拼接

建立两个数组
import numpy as np
t1=np.arange(24).reshape((4,6))
t2=np.arange(24).reshape((4,6))
print("t1:\n",t1)
print("t2:\n",t2)
import numpy as np
t1=np.arange(24).reshape((4,6))
t2=np.arange(24).reshape((4,6))
print("t1:\n",t1)
print("t2:\n",t2)

t1:
 [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
t2:
 [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

竖直拼接

t3=np.vstack((t1,t2))
t3

array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])

水平拼接

t3=np.hstack((t1,t2))
t3
array([[ 0,  1,  2,  3,  4,  5,  0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11,  6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23, 18, 19, 20, 21, 22, 23]])

8.行列交换

1,2行交换
t1[[1,2],:]=t1[[2,1],:]
array([[ 0,  1,  2,  3,  4,  5],
       [12, 13, 14, 15, 16, 17],
       [ 6,  7,  8,  9, 10, 11],
       [18, 19, 20, 21, 22, 23]])
列交换同理

9.随机数

np.random

本文地址:https://blog.csdn.net/weixin_46109590/article/details/109954946

《numpy基本用法.doc》

下载本文的Word格式文档,以方便收藏与打印。