文章目录
Numpy
1、np.array的计算
import numpy as np
arr01=np.array([[[1,2,3],
[4,5,6],
[7,8,9]],
[[11,22,33],
[44,55,66],
[77,88,99]]])
print(arr01.shape)
print(type(arr01))
mean=[1,2,3]
std=[2,2,2]
arr01-mean
输出:
(2, 3, 3)
<class 'numpy.ndarray'>
array([[[ 0, 0, 0],
[ 3, 3, 3],
[ 6, 6, 6]],
[[10, 20, 30],
[43, 53, 63],
[76, 86, 96]]])
2、np.argmax()
将数组进行从小到大排列,再返回其索引。
参考:链接
对于1维向量:
import numpy as np
a = np.array([3, 1, 2, 4, 6, 1])
b=np.argmax(a)#取出a中元素最大值所对应的索引,此时最大值位6,其对应的位置索引值为4,(索引值默认从0开始)
print(b)#4
对于2维向量,分为行索引和列索引,axis=0为列索引,axis=1为行索引,
import numpy as np
a = np.array([[1, 5, 5, 2],
[9, 6, 2, 8],
[3, 7, 9, 1]])
b=np.argmax(a, axis=0)#对二维矩阵来讲a[0][1]会有两个索引方向,第一个方向为a[0],默认按列方向搜索最大值
#a的第一列为1,9,3,最大值为9,所在位置为1,
#a的第一列为5,6,7,最大值为7,所在位置为2,
#此此类推,因为a有4列,所以得到的b为1行4列,
print(b)#[1 2 2 1]
c=np.argmax(a, axis=1)#现在按照a[0][1]中的a[1]方向,即行方向搜索最大值,
#a的第一行为1,5,5,2,最大值为5(虽然有2个5,但取第一个5所在的位置),索引值为1,
#a的第2行为9,6,2,8,最大值为9,索引值为0,
#因为a有3行,所以得到的c有3个值,即为1行3列
print(c)#[1 0 2]
2.1 np.argsort()
参考:链接
3、np.where
代码:
import numpy as np
a = np.array([2, 4, 6, 8, 10])
index=np.where(a > 5)[0] # 对于一维来说加[0]与不加[0]都一样,但对于2维或更高维就不能加[0]
print(index)
print(type(index))
print(a[index])
print(type(a[np.where(a > 5)]))
输出:
[2 3 4]
<class 'numpy.ndarray'>
[ 6 8 10]
<class 'numpy.ndarray'>
对于2维:
代码:
a = np.arange(27).reshape(3,3,3)
print(a)
index=np.where(a > 5)
print(index)
print("*"*50)
print(a[index])
输出:
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2],
dtype=int64), array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2],
dtype=int64), array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
dtype=int64))
**************************************************
[ 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26]
参考:链接
4、np.vstack和np.hstack
参考:链接
我现在用到了np.vstack,即在竖直方向上堆叠
import numpy as np
arr1=np.array([[1,2,3]])
arr2=np.array([[4,5,6],[7,8,9]])
print (np.vstack((arr1,arr2)))
print("-"*50)
print (np.vstack(arr2))
输出:
[[1 2 3]
[4 5 6]
[7 8 9]]
--------------------------------------------------
[[4 5 6]
[7 8 9]]
5、np.cumsum()
np.cumsum()就是按照某一个维度进行逐元素累加
代码:
arr2=np.array([1,2,3,4,5,6,7,8,9])
np.cumsum(arr2)
输出:
array([ 1, 3, 6, 10, 15, 21, 28, 36, 45], dtype=int32)
代码:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.cumsum(a, axis=0)
c = np.cumsum(a, axis=1)
print(b)
print("-"*50)
print(c)
输出:
[[1 2 3]
[5 7 9]]
--------------------------------------------------
[[ 1 3 6]
[ 4 9 15]]
6、np.logical_and()和np.logical_or()
代码:
import numpy as np
arr01=np.array([np.array(list([1,2,3])), np.array(list([11,22,3]))])
arr01[:,0:2][arr01[:,0:2]<=2]=0
arr01[np.logical_and(arr01[:,1]>2,arr01[:,2]>2)]
输出:
array([[11, 22, 3]])
参考:链接
7、np.random.uniform()
函数原型: numpy.random.uniform(low,high,size)
功能:从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
参数介绍:
low: 采样下界,float类型,默认值为0;
high: 采样上界,float类型,默认值为1;
size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出m*n*k个样本,缺省时输出1个值。
返回值:ndarray类型,其形状和参数size中描述一致。
这里顺便说下ndarray类型,表示一个N维数组对象,其有一个shape(表维度大小)和dtype(说明数组数据类型的对象),使用zeros和ones函数可以创建数据全0或全1的数组,原型:
numpy.ones(shape,dtype=None,order='C'),
其中,shape表数组形状(m*n),dtype表类型,order表是以C还是fortran形式存放数据。
参考:链接
7.1 np.random.choice()
参考:链接
7.2 np.random.seed()
参考:链接1
7.3 np.random.random()
参考:链接1
8、np.expand_dims
代码:
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a)
输出:
[[1 2 3]
[4 5 6]]
a.shape: (2, 3)
代码:
b=np.expand_dims(a, axis=0)
print(b)
print("b.shape:", b.shape)
-------------------------------
[[[1 2 3]
[4 5 6]]]
b.shape: (1, 2, 3)
-------------------------------
b=np.expand_dims(a, axis=1)
print(b)
print("b.shape:", b.shape)
-------------------------------
[[[1 2 3]]
[[4 5 6]]]
b.shape: (2, 1, 3)
-------------------------------
b=np.expand_dims(a, axis=2)
print(b)
print("b.shape:", b.shape)
-------------------------------
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
b.shape: (2, 3, 1)
参考:链接
9、np.random.rand()
返回一个或一组服从0~1均匀分布的数
9.1 np.random.randn()
返回一个或一组服从标准正态分布的随机样本值
参考:链接
10、np.uint8
uint8是专门用于存储各种图像的(包括RGB,灰度图像等),范围是从0–255
参考:链接
11、numpy中的nonzero()函数详解
参考:链接
12、numpy.transpose详解
参考:链接
13、np.percentile()
参考:链接1
Random
1、random.sample
sample(序列a,n)
作用:从序列a中随机抽取n个元素,并将n个元素以list形式返回。
参考:链接
2、Python中random函数用法整理
参考:链接1
其他
1、Map
参考:链接
2、global()[ ]
使用 global()[ ] 返回当前的函数,[ ]里面放置函数名
代码:
def zero_dockers():
print('zero')
def one_dockers():
print('one')
def two_dockers():
print('two')
def hello():
print('hello')
if __name__=='__main__':
for local_function in list(globals()):
if local_function.endswith('dockers'):
print(globals()[local_function])
输出:
<function zero_dockers at 0x0000021D2CAA6D90>
<function one_dockers at 0x0000021D2CAA6730>
<function two_dockers at 0x0000021D2CAA6620>
如果不用global()[ ],则只返回函数名,
代码:
def zero_dockers():
print('zero')
def one_dockers():
print('one')
def two_dockers():
print('two')
def hello():
print('hello')
if __name__=='__main__':
for local_function in list(globals()):
if local_function.endswith('dockers'):
print(local_function)
输出:
zero_dockers
one_dockers
two_dockers
参考:链接
3、列表转字符串
使用 " ".join(list),
代码:
list01=['a', 'b', 'c']
" ".join(list01) # 使用空格隔开
输出:
'a b c'
也可以用其他分隔符隔开
代码:
list01=['a', 'b', 'c']
"\\".join(list01)
输出:
'a\\b\\c'
注意列表中的元素需要是字符串类型,如果不是字符串则需要做转换,
代码:
list01=[1,2,3]
" ".join(str(a) for a in list01)
输出:
'1 2 3'
参考:链接1
4、引用,浅拷贝,深拷贝
参考:链接1
5、python 的标准库模块glob使用教程
参考:链接1
6、ModuleNotFoundError: No module named ‘src’
参考:链接1