![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
ephramliu
NWPU硕士
展开
-
Python自动点击鼠标脚本
【代码】Python自动点击鼠标脚本原创 2022-12-12 14:14:46 · 3084 阅读 · 1 评论 -
Python数据处理Pandas库使用
Python数据处理Pandas库使用原创 2022-11-03 12:41:56 · 265 阅读 · 0 评论 -
matplotlib快速画图
range_ = 1.5compare_line = list(np.arange(-range_,range_,0.01))font_size = 16plt.rcParams['font.family'] = 'SimHei'plt.rcParams['font.size'] = font_sizeplt.rcParams['axes.unicode_minus'] = False# 测试集plt.figure(figsize=(8,6))plt.scatter(pred_te原创 2022-01-11 15:40:35 · 405 阅读 · 0 评论 -
ERROR: Could not find a version that satisfies the requirement absl (from versions: none) ERROR: No
pip install absl安装absl库时遇到问题ERROR: Could not find a version that satisfies the requirement absl (from versions: none)ERROR: No matching distribution found for absl应该改为pip install absl-py原创 2021-11-02 15:16:48 · 2090 阅读 · 2 评论 -
【Python】排序函数 sort、sorted 对复杂列表排序
1 lambda 表达式匿名函数。匿名函数lambda:是指一类无需定义标识符(函数名)的函数或子程序。lambda 函数可以接收任意多个参数 (包括可选参数) 并且返回单个表达式的值。lambda 函数不能包含命令。包含的表达式不能超过一个。a = lambda x,y,z:(x+8)*y-zprint(a(5,6,8))>> 702 sorted() 方法默认升序排列a = [5,7,6,3,4,1,2]b = sorted(a) # 保留原列表a原创 2021-08-19 00:03:19 · 553 阅读 · 0 评论 -
【Python】常用字符串api
1 strip()方法去除字符串中指定的字符s = "123abc "s.strip(" ") # 去除空格s = "123abc"2 replace()方法替换指定字符为其他字符s = "123abc "s.replace(" ","1") # 将空格替换为1s = "123abc111"去除 回车\r 和 换行符\ns.replace('\n', '').replace('\r', '')3 大小写转换str = "www.runoob.com"print(原创 2021-08-18 23:18:26 · 357 阅读 · 0 评论 -
机器学习面试题目整理
1 归一化1.1 定义将数据映射到指定的范围,如:把数据映射到0~1或-1~1的范围之内处理。1.2 常见做法min-max 归一化,也叫 0-1 标准化1.3 作用有量纲表达式变成无量纲表达式,便于不同单位或量级的指标能够进行比较和加权简化计算数据映射到指定的范围内进行处理,更加便捷快速2 标准化2.1 定义将数据变换为均值为0,标准差为1的分布,切记,并非一定是正态的2.2 通常做法z-score 标准化robust 标准化(RobustScaler)MaxAbs原创 2021-08-17 21:44:15 · 533 阅读 · 0 评论 -
【Python】int binary str 互转
1 int() 用法int() 是 python 内置的对象转 int 的方法# str 进行十进制转换int('10',10)>> 10int('2',10)>> 2# str 进行二进制转换int('10',2)>> 2# str 进行其他进制转换int('101',15)>> 226int('101',16)>> 2572 int -> binx,y,z = 10,20,30bin_lst = [bi原创 2021-07-07 17:44:41 · 1119 阅读 · 0 评论 -
pyinstaller 用法
1 安装pip install pyinstaller2 打包pyinstaller main.py参数详解-F 同时打包库文件-w 不创建控制台窗口,也不分配标准输入/输出-D 产生一个目录用于部署 (默认)原创 2021-05-13 15:09:48 · 127 阅读 · 0 评论 -
Python OS使用
import osimport timefile='/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'os.path.getatime(file) #输出最近访问时间1318921018.0os.path.getctime(file) #输出文件创建时间os.path.getmtime(file) #输出最近修改时间time.gmtime(os.path.getmtime(file)) #以struct_time形式输出最近修改时间os原创 2021-05-09 11:52:37 · 75 阅读 · 0 评论 -
python any all
>>> a = [True, True]>>> b = [True, False]>>> c = [False, False]>>> any(a)True>>> any(b)True>>> any(c)False>>> all(a)True>>> all(b)False>>> all(c)False原创 2021-05-07 16:31:39 · 61 阅读 · 0 评论 -
python 求点到线段距离
1 点和线段的参数# pointp = [0,0]# line segment# point-1a = [-1,1]# point-2b = [1,1]2 求取距离import numpy as npdef dis_point_to_seg_line(p, a, b): a, b, p = np.array(a), np.array(b), np.array(p) # trans to np.array d = np.divide(b - a, np.linalg.原创 2021-05-06 20:17:42 · 2510 阅读 · 2 评论 -
python 射线法 判断点是否在 多边形内
def judgeIfInPolygon(polySides, polyX, polyY, point): in_bool = False j = polySides-1 for i in range(polySides): if ((((polyY[i]<point[1])and(polyY[j]>=point[1]))or((polyY[j]<point[1])and(polyY[i]>=point[1]))) \原创 2021-04-28 16:15:43 · 438 阅读 · 0 评论 -
python time datetime 使用
1 导入import datetime, time2 查看当前时间datetime.datetime.now()>> datetime.datetime(2021, 4, 20, 11, 16, 40, 342361) # 2021-4-20 11:16:40.3423613 自定义时间a = datetime.datetime(2020,1,1)>> datetime.datetime(2020, 1, 1, 0, 0) # 2020-1-1 0:0:0.0原创 2021-04-20 13:38:31 · 134 阅读 · 0 评论 -
ImportError: Can‘t find framework /System/Library/Frameworks/OpenGL.framework.ImportError:Error occu
pip install pyglet==1.5.11原创 2021-04-02 15:45:44 · 866 阅读 · 0 评论 -
python 列表生成式 字典生成式
a=["agent_{}".format(i) for i in range(10)]b={"agent_{}".format(i):i for i in range(10)}原创 2021-03-19 13:04:35 · 85 阅读 · 0 评论 -
python 秒数转化为时分秒
def timeCost(self, t_start): t_now = time.time() m, s = divmod(t_now-t_start, 60) h, m = divmod(m, 60) return "%02d:%02d:%02d" % (h, m, s)原创 2021-03-06 15:38:09 · 4295 阅读 · 0 评论 -
numpy 向量旋转任意角度
举例:向量a顺时针旋转-90度,即逆时针旋转90度:import numpy as npfrom numpy.linalg import norma = np.array([0.0, 1.0])alpha = -0.5*np.pix_ = a[1]*np.sin(alpha)+a[0]*np.cos(alpha)y_ = a[1]*np.cos(alpha)-a[0]*np.sin(alpha)print("[ {} , {} ]".format(x_, y_))结果:[ -1.0 ,原创 2021-02-01 22:53:26 · 4896 阅读 · 0 评论 -
numpy 求向量夹角 区间 [-pi, +pi]
求取 numpy 向量夹角可以使用 numpy.arctan2 函数;从而避免了特殊值带来的角度错误;实例:# 初始化向量a = np.array([-1.0, 1.0])b = np.array([0.0, 1.0])# 单位化(可以不用这一步)a /= norm(a)b /= norm(b)# 夹角cos值cos_ = np.dot(a,b)/(norm(a)*norm(b))# 夹角sin值sin_ = np.cross(a,b)/(norm(a)*norm(b))arcta原创 2021-02-01 22:07:39 · 7002 阅读 · 3 评论 -
使用argparse对python脚本时运行时添加参数
import argparsedef parse_args(): parser = argparse.ArgumentParser("paras") now = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) parser.add_argument("--now", type=str, default=now, help="current time") return parser.parse_args()argli原创 2021-01-14 21:17:15 · 521 阅读 · 0 评论 -
成功解决Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/xx/: There was a problem confirming
pip安装xxx库出现问题:比如,xxx是pandas(py37) user@node01:~$ pip install pandasLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simpleWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(原创 2020-12-23 17:51:28 · 10139 阅读 · 4 评论 -
【Pygame】在 Pygame 屏幕中添加文字
font = pygame.font.SysFont("freesansbold.ttf", 30) # 30:font sizetext = font.render("content", True, (0,0,0)) # (0,0,0) color of fontself.window.blit(text,(10,10)) # (10,10) rect left top原创 2020-12-18 20:18:12 · 4730 阅读 · 2 评论 -
【Python】一句话 if else 简洁写法
if else 的简洁写法c = a if a>b else b原创 2020-12-14 17:54:28 · 6412 阅读 · 3 评论 -
Windows下 Anaconda + VScode Python 环境搭建 多图 非常详细
如何在 windows 系统下搭建属于自己的 python 深度学习环境;建议收藏;1 下载 Anaconda 和 Vscode演示电脑是 64 位操作系统;Anaconda 官网下载;链接 https://www.anaconda.com/products/individualVscode 官网下载;链接 https://code.visualstudio.com/2 安装 Anaconda 和 VScode找到我们下载的目录,分别运行;注意勾选以下;在安装 VScode 时;原创 2020-12-10 12:44:00 · 3170 阅读 · 1 评论 -
【Python】彩色图片转为灰度图(4行脚本搞定)
1 安装PIL库pip install pillow2 建立文件夹建立包括彩色图片和准备保存为灰度图的文件夹;我们建立了名为 change_fig 的文件夹;fig1.jpg是我们的彩图原图;fig2.jpg是我们即将生成的灰度图;trans.py是我们的脚本;3 脚本内容from PIL import ImageI = Image.open('./fig1.jpg')L = I.convert('L')L.save("./fig2.jpg") # 灰度图fig2.jpg保存在当原创 2020-12-04 15:45:47 · 3133 阅读 · 3 评论 -
【Python】读取 txt 文件
使用 python 脚本对 txt 文件进行读取:使用 with 方法读取文件时,可以不用在读取后再 close 文件;本例中,txt 内容为 1,1,1;with open('./task.txt') as f: lst = f.read()print(lst)print(type(lst))# 1,1,1# <class 'str'># 默认将 txt 内容作为字符串读取lst = eval(lst)print(lst)print(type(lst))原创 2020-11-30 14:58:00 · 292 阅读 · 0 评论 -
【Python】any() all() 用法
这是两个 python 内置 API;1 any()any() 的输入是一个可迭代对象;any() 用于判断输入这个 可迭代对象 是否全为 真/假;只要不是全为 空/假,就输出 真;全空/假输出假,否则输出真;只有全假/空才输出假;a = [[],"",0,()] # 全假/空print(any(a))>>> Falsea = [1," ",] # 全真print(any(a))>>> Truea = [0,1,0,0,0] # 有真,原创 2020-11-24 15:26:49 · 419 阅读 · 0 评论 -
【Python】随机种子 random/numpy/pytorch/pytorch.cuda
设立随机种子能够确保随机数的初始值在每次实验时相同;即能确保每次实验处于相同的初始状态;import random, torchimport numpy as npseed = 1random.seed(seed)np.random.seed(seed)torch.manual_seed(seed)torch.cuda.manual_seed(seed)其中 seed = 1 表示编号为 1 的随机值,是一组随机值;也可以换成别的整数数字,比如 123,456 之类的;通常不原创 2020-11-23 10:54:32 · 396 阅读 · 0 评论 -
【Linux】后台 nohup 运行 python 程序
nohup python -u Game2d.py > out.log &Game2d.py 是我们想运行的程序;out.log 是输出日志;原创 2020-11-22 20:42:30 · 365 阅读 · 0 评论 -
【Python】嵌套类的定义与使用
在python语言中我们能够方便地在类中定义类,在类中多次实例化类;使用方法如下;我们试图在 World 这个 class 里定义了一个 class Agent;并试图在 World 的 __init__方法中对 Agent 类实例化10次;class World(object): def __init__(self): self.agent_lst = [self.Agent() for i in range(10)] for index,value in原创 2020-11-10 13:21:23 · 5863 阅读 · 0 评论 -
numpy.random 模块中文文档学习笔记
1 numpy.random.randint()整数随机,指定上下界,左闭右开np.random.randint(1, size=10)>>> array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])np.random.randint(2, size=10)>>> array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])np.random.randint(5, size=(2, 4))>>> arra原创 2020-10-20 14:00:48 · 532 阅读 · 1 评论 -
list numpy array tensor转换
1.1 list 转 numpy.arrayndarray = np.array(list)1.2 numpy 转 listlist = ndarray.tolist()2.1 list 转 torch.Tensortensor=torch.Tensor(list)2.2 torch.Tensor 转 list先转numpy,后转listlist = tensor.numpy().tolist()3.1 torch.Tensor 转 numpyndarray = tensor.numpy原创 2020-10-20 11:38:32 · 728 阅读 · 0 评论 -
【Python】列表方法“+”与“extend()”区别
列表是 python 常见的数据结构类型用于在列表后追加一个数据有“append()”方法用于在列表后追加多个数据有“+”和“extend()”两种方法下面介绍“+”和“extend()”这两种方法的异同:1 extend() 方法a = [1,2,3]b = [4,5,6]print("a id :",id(a))print("b id :",id(b))# extend()方法a.extend(b)print("extend: a =",a)print("extend: a i原创 2020-10-16 12:35:29 · 1212 阅读 · 0 评论 -
【Python】打开/查看 pkl csv json txt 文件
import picklef = open('your_file_name.pkl','rb')info = pickle.load(f)print(info)原创 2020-10-15 21:47:51 · 1078 阅读 · 0 评论 -
【Python】弧度转化为角度
# usage:"""第一种:import mathmath.degrees(x)第二种:from math import *degrees(x)"""x=1print("弧度{}的角度为{}".format(x,math.degrees(x)))>>弧度1的角度为57.29577951308232原创 2020-10-15 17:00:34 · 16260 阅读 · 0 评论 -
成功解决 AttributeError: module ‘neat’ has no attribute ‘Config’解决方式
如果你想用neat的强化学习算法,那么你要装的不是neat,而是neat-python执行下列语句#卸载neatpip uninstall neat#安装neat-python,通过豆瓣源pip install neat-python -i https://pypi.doubanio.com/simple原创 2020-10-13 16:15:59 · 2796 阅读 · 1 评论 -
【Anaconda】Anaconda 环境操作(新建/激活/退出/删除/克隆/重命名)
查看当前已创建的环境列表conda info -e新建一个环境新建一个名为xxx的,python版本为3.6的环境conda create -n xxx python=3.6激活环境激活名为xxx的环境conda activate xxx退出环境退出当前的环境conda deactivate删除一个环境删除名为xxx的环境conda remove -n xxx --all克隆环境将环境xxx克隆为yyyconda create -n yyy --clone xxx原创 2020-10-13 09:59:35 · 313 阅读 · 0 评论 -
pytorch 向量转化为one-hot编码
#pytorch 向量转化为one-hot编码import torch#原始向量index = torch.tensor([[1], [2], [0], [3]])onehot = torch.zeros(4, 4)onehot.scatter_(1, index, 1)print(onehot)#结果tensor([[0., 1., 0., 0.], [0., 0., 1., 0.], [1., 0., 0., 0.], [0., 0., 0., 1.]])...原创 2020-10-09 16:44:07 · 1604 阅读 · 0 评论 -
python 对文件进行操作(读取/路径/写入/修改/储存/附加)
1 从文件读取数据1.1 读取整个文件with open("./test_file.txt") as file_object: content = file_object.read() print(content)with 关键字 用于不再访问文件时将其关闭;可以使用open()与close()来访问文件;file_object.read() 用于读取文件内容,以str形式储存在变量content里;打印content可以讲文件的内容打印出来;1.2 文件路径# linuxpath原创 2020-10-01 19:33:05 · 5975 阅读 · 2 评论 -
【Python】matplotlib.pyplot 常用技巧
使用 matplotlib.pyplot 进行绘图非常的方便高效原创 2020-09-26 11:06:06 · 285 阅读 · 0 评论