0
点赞
收藏
分享

微信扫一扫

Py记4(NumPy计算库

吴wuwu 2022-02-26 阅读 67

1、安装NumPy库:
pip install numpy #在Anaconda中,已经安装了

2、数组属性:
ndim 数组的维数
shape 数组的形状
size 数组元素的总个数
dtype 数组中元素的数据类型
itemsize 数组中每个元素的字节数

3、创建数组:array([列表])或者array((元组))
也可以指定数据类型:array([列表]/(元组), dtype=数据类型) 数据类型下面将讲到

3.1、多维数组:
array([[…这里一开始有多少个“[",第一个[划分第一个维度的不同元素
#二维数组及其切片

   [[12, 13, 14, 15],        #c[1]
[16, 17, 18, 19],
[20, 21, 22, 23]]])

3.2、数据类型:
数组中数据类型必须一样
int8、uint8、int16、uint16、int32、uint32、int64、uint64
float16、 float32、 float64、 float128
complex64、complex128、complex256
bool、object、string_、unicode_

3.3、创建特殊的数组:
np.arange(起始数字=0,结束数字,步长=1,dtype) 数字序列数组,前闭后开不包括结束数字
np.ones(shape, dtype) 全1数组,dtype可省
np.zeros(shape, dtype) 全0数组
np.eye(shape, dtype) 单位矩阵
np.linspace(start,stop,num=50,dtype) 等差数列,(始值,末值,元素个数,类型),注意包括末值
np.logspace(start,stop,num=50,base=1,dtype) 等比数列

例如:

3.4、asarray():将列表或元组转化为数组对象
对比array与asrray:当数据原来就是ndarray对象(n维数组)时,array()会复制新副本(占内存),而asarray()则不复制副本,而是直接引用原数组(共享内存)
arraylist1=[1,1]
arr1=np.array(list1)
arr2=np.asarray(list1)
list1[0]=2 #则输出arr1和arr2时,均为[1,1]
#对比:
arr3=np.ones((1,2))
arr4=np.array(arr3)
arr5=np.asarray(arr3)
arr3[0]=2 #则输出arr4为[1,1],输出arr5为[2,1],arr5跟着arr3变化

3.5改变数组形状:reshape(shape)、resize(shape)
创建新数组reshape(shape)、改变原数组resize(shape)

4、加、减、乘、除
以加法为例:(乘法也是对应位相乘,不是矩阵乘法)

5、数组元素间的运算:
np.sum() #计算所有元素的和 np.sum(B),返回值9,还可实现按行列求和
np.prod() #计算所有元素的乘积
np.diff() #计算数组的相邻元素之间的差
np.sqrt() #计算各元素的平方根 np.sqrt(A**)
np.exp() #计算各元素的指数值
np.abs() #取各元素的绝对值

   [[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]
])

6、数组合并:
6.1、堆叠运算:np.stack((数组1,数组2,…),axis)
#一维数组堆叠

   [[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]
])

np.stack((m,n),axis=1) #shape:(3,2,3)
Out[72]:
array([[[ 0, 1, 2],
[ 9, 10, 11]],

   [[ 3,  4,  5],
[12, 13, 14]]
,

[[ 6, 7, 8],
[15, 16, 17]]
])
   [[ 3, 12],
[ 4, 13],
[ 5, 14]]
,

[[ 6, 15],
[ 7, 16],
[ 8, 17]]
])

6.2、np.concatenate(数组,axis=0)
a=np.array([[1,2],[3,4]])
b=np.array([[5,6]]) #注意a、b维度相同
np.concatenate((a,b),axis=0)
Out[42]:
array([[1, 2],
[3, 4],
[5, 6]])
np.concatenate((a,b.T),axis=1)
Out[43]:
array([[1, 2, 5],
[3, 4, 6]])

6.3增加一个维度:np.expand_dims(数组,axis=0)
见tf.expand_dims()
7、获取numpy array 数组中指定元素的索引位置
a = np.array([1,2,3,4,5,6,6,7,6])
a==6
b = np.where(a == 6)
print(‘b={} shape(b)={}’ .format(b,np.shape(b)))
b=(array([5, 6, 8], dtype=int64),) shape(b)=(1, 3)
b[0][2]
Out[92]: 8

7、矩阵与矩阵运算 矩阵:matrix(字符串/列表/元组/数组),其中,matrix可以简写为mat

8、矩阵属性:
.ndim 矩阵的维数
.shape 矩阵的形状
.size 矩阵的元素个数
.dtype 元素的数据类型

9、数组实现矩阵运算,
返回的是数组类型:

10、matrix矩阵运算:

矩阵运算比较:用矩阵运算运算符号简单,二维数组能表示高维数组、灵活、速度快
11、随机数模块:numpy.random
np.random.rand(d0,d1,…,dn) 元素在[0,1)区间均匀分布的数组 浮点数(返回值,下同)其中d0 d1…是维度数
np.random.uniform(low,high,size) 元素在[low,high)区间均匀分布的数组 浮点数
numpy.random.randint(low,high,size) 元素在[low,high)区间均匀分布的数组 整数
np.random.randn(d0,d1,…,dn) 产生标准正态分布的数组 浮点数 其中d0 d1…是维度数
np.random.normal(low,scale,size) 产生正态分布的数组 浮点数

例如:
#创建2*3的随机数组,[0,1]内均匀分布

12、随机种子:用seed()设置
(仅设置一次有效),一般默认是根据系统时间生成,随机种子一样则随机数一样。不同系统随机数生成算法不一样

13、打乱顺序函数:shuffle(序列) 、get_state、set_state
可Python的列表和NumPy数组等等

get_state():记录下数组被打乱的操作,
set_state():接收get_state()返回的值,并进行同样的操作
import numpy as np
a = np.arange(0,10,1)
b = np.arange(10,20,1)
print(a,b)
#result:[0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19]
state = np.random.get_state()
np.random.shuffle(a)
print(a)
#result:[6 4 5 3 7 2 0 1 8 9]
np.random.set_state(state)
np.random.shuffle(b)
print(b)
#result:[16 14 15 13 17 12 10 11 18 19]

14、max(axis)
In [9]: arr1 = np.array([[1, 5, 3], [4, 2, 6]])

In [10]: arr1.max(axis=0)
Out[10]: array([4, 5, 6])

In [11]: arr1.max(axis=1)
Out[11]: array([5, 6])

In [12]: arr1.max()
Out[12]: 6

15、导入txt等的文件:
np.getfromtext(文件名,delimiter=‘分割字符’) 返回值是一个numpy数组,将文件读取出来,以分割字符划分数组的元素
16、交换维度:swapaxes
numpy.swapaxes(a,axis1,axis2)
In [115]: x=np.array([[1,2,3]])
In [116]: np.swapaxes(x,0,1)
Out[116]:
array([[1],
[2],
[3]])

举报

相关推荐

0 条评论