uv配置虚拟环境 功能介绍
更改uv镜像源,修改pyproject.toml文件
1 2 3 [[tool.uv.index]] default = true url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
Python管理 python安装
uv python install 3.12: 安装 Python 版本
uv python list: 查看可用 Python 版本
uv python uninstall: 卸载 Python 版本
项目开发 项目开发
uv init: 创建新 Python 项目
uv venv: 创建.venv虚拟环境,uv venv --python 3.11.6指定版本
uv add: 为项目添加依赖
uv add -r requirements.txt:从 requirements.txt 安装
uv remove: 从项目移除依赖
uv lock --upgrade-package requests: 升级包
uv sync: 同步项目依赖到环境
uv run: 在项目环境中运行命令
uv tree: 查看项目依赖树
Python前置知识 推导式 1 2 3 4 5 6 7 8 9 [(color, item) for color in ["red" , "blue" ] for item in ["pen" , "book" ]] [[(color, item) for color in ["red" ,"blue" ]] for item in ["pen" , "book" ]]
给定3 个二维整数列表L1、L2、L3,它们都是30×20 的列表,即每个列表中包含30 个内层列表,并且每一个内层列表中包含20 个整数。请利用列表推导式,构造一个大小相同的新列表Lnew,其满足任意一个位置的值是L1、L2、L3 相应位置的值的最小值。
1 2 3 4 5 6 7 8 import randomrandom.seed(0 ) n = 10000 L1 = [[random.randint(-n,n) for i in range (20 )] for j in range (30 )] L2 = [[random.randint(-n,n) for i in range (20 )] for j in range (30 )] L3 = [[random.randint(-n,n) for i in range (20 )] for j in range (30 )] Lnew = [[min ([L1[j][i], L2[j][i], L3[j][i]]) for i in range (20 )] for j in range (30 )]
Numpy教程 特殊矩阵 1 2 3 4 5 6 7 8 np.diag([1 ,2 ,3 ,4 ]) ''' array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]) '''
变形
T / transpose / swapaxes
1 2 3 4 5 carbon = np.random.rand(360 , 180 , 3 ) carbon.transpose(1 ,0 ,2 ).shape carbon.T.shape carbon.swapaxes(1 ,2 ).shape
reshape
1 2 3 4 5 6 7 8 9 10 11 12 13 matrix = np.arange(8 ) matrix.reshape(2 ,-1 ,order="C" ) """ array([[0, 1, 2, 3], [4, 5, 6, 7]]) """ matrix.reshape(2 ,-1 ,order="F" ) """ array([[0, 2, 4, 6], [1, 3, 5, 7]]) """
合并 / 拆分变形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 pop_man = np.random.rand(20 , 6 , 3 ) pop_women = np.random.rand(20 , 6 , 3 ) np.stack([pop_man,pop_women],axis=1 ).shape np.concatenate([pop_man, pop_women], axis=1 ).shape np.split(pop_man,indices_or_sections=3 ,axis=1 )[0 ].shape [i.shape for i in np.split(pop_man,indices_or_sections=[1 ,4 ],axis=1 )] a = np.arange(3 ) np.repeat(a,2 ) np.repeat(a,[1 ,2 ,3 ]) a = np.array([[1 ,2 ],[3 ,4 ]]) np.repeat(a, repeats=[1 ,2 ], axis=1 ) np.repeat(a, repeats=[1 ,2 ], axis=0 )
请使用repeat()函数构造两个10×10 的数组,第一个数组要求第i 行的元素值都为i,第二个数组要求第i 列的元素值都为i
1 2 3 arr = np.arange(1 ,11 ) np.repeat(arr.reshape(10 ,1 ),repeats=10 ,axis=1 ) np.repeat(arr.reshape(1 ,10 ),repeats=10 ,axis=0 )
切片 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 target = np.arange(24 ).reshape(4 , 2 , 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]]]) """ target[[0 , 1 ], [0 , 1 ], [0 , 1 ]] target[..., 0 ::2 ]
常用函数 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 np.var np.std np.argmax np.argmin a = np.array([0 , -5 , 0 , 1 , 3 , -1 ]) np.nonzero(a) np.percentile(arr,50 ) np.percentile(arr,20 ) np.quantile(arr,q = 0.2 ) np.greater([1 , 2 , 3 , 4 ], 2 ) np.less([1 , 2 , 3 , 4 ], 2 ) np.equal([1 , 2 , 3 , 4 ], 2 ) np.logical_and np.logical_or np.logical_not np.any np.all arr = np.arange(1 ,11 ) np.where(arr % 2 == 0 ,arr,np.nan) arr = np.arange(1 ,11 ) np.select([arr % 2 ==0 ,True ],[1 ,2 ]) np.sort np.unique
二维扩展
1 2 3 4 5 6 7 8 9 10 11 12 my_matrix = np.random.randint(20 , 40 , 24 ).reshape(2 , 3 , 4 ) ''' array([[[21, 25, 37, 31], [39, 32, 36, 34], [37, 27, 31, 24]], [[37, 38, 22, 33], [30, 33, 26, 33], [20, 22, 21, 35]]]) ''' my_matrix.sum (axis=(1 , 2 ))
给定一个维度为m×n 的整数数组,请返回一个元素为0 或1 的同维度数组,当且仅当某位置在原数组中的对应元素是原数组同行元素中的最大值时,该位置的元素取1。
1 2 3 matrix = np.random.randint(1 ,10 ,(5 ,5 )) np.where(matrix == matrix.max (axis=1 ).reshape(5 ,1 ),1 ,0 )