
Python
周迪新
好记性不如烂笔头
展开
-
python导出requirements.txt时,不要==,改成>=
python pip原创 2023-03-07 13:33:34 · 283 阅读 · 0 评论 -
python PIL image数据存进 json【base64编码】
如题原创 2023-02-14 15:33:03 · 482 阅读 · 0 评论 -
python通过SMIELS查询CAS号
SMILES CAS原创 2022-10-19 18:43:38 · 2844 阅读 · 1 评论 -
matplotlib 柱状图加标签
matplotlib bar原创 2022-09-28 17:55:17 · 888 阅读 · 0 评论 -
PyTorch Error: Expected more than 1 value per channel when training
python import parent directory原创 2022-08-24 17:02:21 · 418 阅读 · 0 评论 -
python 导入父目录的脚本
python import parent directory原创 2022-08-23 14:13:09 · 849 阅读 · 0 评论 -
PyTorch nn.GRU 使用详解
我们看官方文档一些参数介绍,以及如下一个简单例子:看完之后,还是一脸懵逼: 输入什么鬼? 输出又什么鬼?(这里我先把官网中 h0 去掉了,便于大家先理解更重要的概念)import torchfrom torch import nnrnn = nn.GRU(10, 20, 2)input = torch.randn(5, 3, 10)output, hn = rnn(input)运行之后,各变量的shape如下:要使用GRU这个layer,就要明白,要先定义GRU,然后才是调用这个定义原创 2022-03-16 19:23:07 · 41822 阅读 · 6 评论 -
python 变量赋值变成元组
检查一下你的是不是多了个小逗号,如(看到1右边的该死小逗号的没,费了我老大劲才发现的)x = 1,原创 2021-10-05 17:53:31 · 1555 阅读 · 6 评论 -
Python Loess (Lowess) smooth 曲线平滑
掉包import mathimport numpy as npimport statsmodels.api as smlowess = sm.nonparametric.lowessimport pylab as pln = 100x = np.linspace(0, 2 * math.pi, n)y = np.sin(x) + 0.3 * np.random.randn(n)# yest = lowess(y, x)yest = lowess(y, x, frac=1./3.)[原创 2021-09-15 10:02:02 · 14758 阅读 · 1 评论 -
python 递归拷贝整个文件夹
笔者是在 Ubuntu 系统下进行的演示,下面有些命令是linux的命令。如果不熟悉的朋友,跳过即可。不影响学习。import osimport shutildef copytree(src, dst, symlinks=False, ignore=None): for item in tqdm(os.listdir(src)): s = os.path.join(src, item) d = os.path.join(dst, item) if原创 2021-08-24 15:55:19 · 810 阅读 · 0 评论 -
Python 获取剪切板
from tkinter import Tk # Python 3clip = Tk().clipboard_get()print(clip)按空格分开clip = clip.split()原创 2021-08-24 14:31:32 · 691 阅读 · 1 评论 -
PyTorch 靠谱的模型可视化教程
Netron安装pip install netron使用~$ python3>>> import netron>>> netron.start('/y/home/zdx/model.pth')Serving '/y/home/zdx/model.pth' at http://localhost:8080('localhost', 8080)>>> 打开浏览器,把上面的 http://localhost:8080,用浏览器打开左上原创 2021-07-29 19:27:17 · 1869 阅读 · 0 评论 -
Python Profiler 列举
看运行时间无需安装,自带的cProfilepython 函数import cProfileimport recProfile.run('re.compile("foo|bar")', 'restats')python 文件python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)看运行内存memory_profiler安装 pip install -U memory_profiler原创 2021-07-28 16:45:42 · 371 阅读 · 0 评论 -
python 字典转对象
class Struct: def __init__(self, **entries): self.__dict__.update(entries)args = {'a': 1, 'b': 2}s = Struct(**args)>>> s.a1>>> s.b2原创 2021-07-20 20:57:06 · 1176 阅读 · 0 评论 -
ubuntu 18.04 conda 环境中编译 pytorch
1 新建一个conda环境conda create -n new_env python=3.8.102 激活后,依次安装conda install -c conda-forge cudatoolkit-dev=10.1conda install -c conda-forge cudnn=8.0.5.39=hc0a50b0_1conda install -c conda-forge ncclconda install -n astunparse numpy ninja pyyaml mkl mk原创 2021-06-22 16:14:36 · 780 阅读 · 4 评论 -
python自动处理下载的英文字幕
首先,用这个Downsub网站下载YouTube视频的字幕下载好的视频,用下面的python脚本过一下,即可把断开的句子合并了。input_file = '/home/zdx/Downloads/[English] Insane Inventions That Killed Their Inventors [DownSub.com].txt'with open(input_file, 'r') as f: data = f.read().splitlines() data = ' '原创 2021-06-15 08:31:23 · 181 阅读 · 1 评论 -
python 读取文件时,自动去除\n
with open(input_file, 'r') as f: data = f.read().splitlines()原创 2021-06-15 08:16:14 · 978 阅读 · 2 评论 -
pandas 多进程处理数据框
我假设看到这里的朋友都已经掌握了 apply。df['new'] = df['old'].apply(核心函数:def parallelize_dataframe(df, func, **kwargs): CPUs = multiprocessing.cpu_count() num_partitions = CPUs # number of partitions to split dataframe num_cores = CPUs # number of cores o原创 2021-05-17 14:00:36 · 2639 阅读 · 1 评论 -
python 中统计超大文件行数
num_lines = sum(1 for line in open(input_file))原创 2021-05-12 10:35:33 · 457 阅读 · 0 评论 -
使用远程服务器Spyder
ssh -X 服务器IP然后在服务器的终端输入spyder即可。稍等片刻,Spyder就会出现在你的桌面中原创 2021-04-27 19:31:21 · 734 阅读 · 0 评论 -
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAFloatT
使用torchsummary 中的 summary函数时,模型中有Embedding层的时候,会报错。原因是Embedding 需要整型的Tensor。这么使用即可:from torchsummary import summarysummary(model, (1, 100, 8), dtypes=[torch.LongTensor], device='cpu')pip install 的torchsummary 不行,没有dtypes这个参数。所以你下载这里提供的 torchsummary.p原创 2021-04-27 15:50:08 · 939 阅读 · 1 评论 -
python 绘图中设置颜色对比强烈的组合
文章目录1 核心函数2 举一个完整的例子3 运行后得到:4 颜色表1 核心函数给定一个颜色,自动计算对比度最强的颜色def get_complementary(color): # strip the # from the beginning color = color[1:] # convert the string into hex color = int(color, 16) # invert the three bytes # as good as原创 2021-04-20 10:44:00 · 2710 阅读 · 0 评论 -
VsCode crtl + 鼠标右键 python代码无法跳转
ctrl + shift + x 打开插件搜索框安装 pylance 插件原创 2021-03-22 09:35:10 · 1272 阅读 · 0 评论 -
GitLab CI 中激活Anaconda环境
在使用conda前,加上: - source ~/anaconda3/etc/profile.d/conda.sh - conda activate my-env原创 2021-03-16 11:46:09 · 375 阅读 · 0 评论 -
spyder ctrl + 鼠标左键点击函数 无法跳转
遇到这个问题很简单哦。问题简述点击import的是系统库的函数的时候可以跳转,但是点import是自己的脚本的库的函数的时候,没法跳转。解决方案Spyder IDE的菜单栏上找Tools -> PYTHONPATH manager把自己脚本所在的路径添加进去就好啦。...原创 2021-02-23 10:50:46 · 2722 阅读 · 0 评论 -
python 批量自动搜索、自动抓取需要的信息简单教程【selenium】
文章目录安装selenium以及Chrome需要学习XPath技术和正则表达式技术,xpath能够让程序快速定位,正则表达式打辅助,用来进一步提取需要的信息。Ctrl + shift + c 打开网页源代码,用鼠标右键选择你想定位的地方。右键复制xpath一个简单的示例本文主要使用工具为selenium,并以Chrome浏览器为例。注意:使用不同的浏览器,需要配置不同的浏览器驱动。安装selenium以及Chrome教程链接需要学习XPath技术和正则表达式技术,xpath能够让程序快速定位,正原创 2021-02-22 14:05:01 · 12525 阅读 · 0 评论 -
配置 deepchem 环境 [需要anaconda]
文章目录1. 安装Anaconda (装过的就别装了)2. 配置GPU环境 (配过的就别配了)3. 配置deepchem环境1. 安装Anaconda (装过的就别装了)安装Anaconda教程2. 配置GPU环境 (配过的就别配了)安装pytorch gpu和tensorflow gpu版3. 配置deepchem环境在 conda base 环境中,创建 rdkit 环境,并取名为 deepchem(base) zdx@n104:~$ conda create -c rdkit -n de原创 2021-01-29 16:02:50 · 1917 阅读 · 0 评论 -
‘python3\r’: No such file or directory
windows 创建的代码脚本。在Linux运行,可能会出问题。把文件转成unix文件即可。sudo apt install dos2unix用dos2unix命令即可。dos2unix hello.pyunix文件转windowsunix2dos原创 2021-01-22 09:36:41 · 1374 阅读 · 0 评论 -
windows 终端查看python位置
终端进入python>>> import os>>> import sys>>> os.path.dirname(sys.executable)'C:\\Python25原创 2021-01-19 15:49:55 · 1944 阅读 · 0 评论 -
python 中__init__ 与 __call__ 的区别
可以看到,在类实例化的时候,会调用__init__实例化了之后,再调用实例化的对象的时候,这个时候调用的是__call__class test: def __init__(self, a, b): print(a + b) def __call__(self, a, b): print(a * b)>>> a = test(1, 2)>>> 3>>> a(1, 2)&g原创 2021-01-14 20:43:48 · 233 阅读 · 0 评论 -
《Python数据分析》第二版.第三章.[学习笔记][Jupyter notebook]
第3章 Python的数据结构、函数和文件1.元组是一个固定长度,不可改变的Python序列对象tup = 4, 5, 6nested_tup = (4, 5, 6), (7, 8) #复杂元组2.tuple 可以将任意序列或迭代器转换成元组In : tup = tuple('string')In : tupOut: ('s', 't', 'r', 'i', 'n', '...原创 2019-02-20 21:59:13 · 511 阅读 · 0 评论 -
《Python数据分析》第二版.第二章.[学习笔记][Jupyter notebook]
第2章 Python语法基础,IPython和Jupyter Notebooks2.2 IPython 基础1. 变量前后使⽤问号?,可以显示对象的信息2. ??会显示函数的源码3. %run 运行.py 文件4. %load 将脚本导⼊到 ⼀个代码格中5. Ctrl-C 中断运⾏的代码6. %paste 可以直接运⾏剪贴板中的代码7. %cpaste可以粘贴任意多...原创 2019-02-19 20:44:04 · 425 阅读 · 0 评论 -
Jupyter notebook 快捷键
Jupyter Notebook 有两种键盘输入模式。编辑模式,允许你往单元中键入代码或文本;这时的单元框线是绿色的命令模式,键盘输入运行程序命令;这时的单元框线是灰色。命令模式 (按键 Esc 开启) Enter : 转入编辑模式 Shift-Enter : 运行本单元,选中下个单元 Ctrl-Enter : 运行本单元 Alt-Enter : 运行本单元,在其下插入新单元 Y : 单元转入代码状态 M :单元转入markdown状态 R原创 2020-11-27 12:26:13 · 108 阅读 · 0 评论 -
如何在Spyder中使用远程服务器的python来调试代码
让调试代码变得so easyhost和client都安装下:conda install spyder-kernels在服务上运行:python -m spyder_kernels.console — matplotlib=’inline’ — ip=127.0.0.1 -f=./remotemachine.json回到你的电脑,把这个remotemachine.json文件,移到你自己的电脑上。不要做任何更改。打开spyder,跳出来的下面这个,直接点cancel.把刚才从服务器复原创 2020-11-25 15:10:06 · 3998 阅读 · 8 评论 -
ubuntu 下安装和配置selenium
Install chrome.sudo apt-get updatesudo apt-get install -y unzip xvfb libxi6 libgconf-2-4sudo apt-get install default-jdk sudo apt-get install gdebiwget https://dl.google.com/linux/direct/google-c...原创 2019-12-17 11:26:26 · 1572 阅读 · 0 评论 -
机器学习炼丹术之——调参Tuning the hyper-parameters of an estimator
https://scikit-learn.org/stable/modules/grid_search.html#原创 2020-10-12 19:57:24 · 245 阅读 · 0 评论 -
sklearn炼丹术之——交叉验证Cross-validation: evaluating estimator performance
https://scikit-learn.org/stable/modules/cross_validation.html#原创 2020-10-12 20:00:11 · 169 阅读 · 0 评论 -
sklearn炼丹术之——Linear Models汇总
以前总是稀里糊涂的各种调用,第三章也学过来了,再看这些,应该有更深入的理解。也可以挑战阅读阅读他们的源码。(可以看到,源码写的挺复杂,一股浓浓的工业风)https://scikit-learn.org/stable/modules/linear_model.html...原创 2020-10-08 14:05:06 · 267 阅读 · 0 评论 -
python可交互绘图 mplcursors库
来源原创 2020-10-20 15:35:24 · 1175 阅读 · 0 评论 -
可交互绘图——鼠标移到点的上方会显示该点的标签[jupyter notebook]
import matplotlib.pyplot as pltimport numpy as npimport mplcursorsnp.random.seed(42)%matplotlib auto # 终端运行脚本不用加plt.scatter(*np.random.random((2, 26)))crs = mplcursors.cursor(hover=True)crs.connect("add", lambda sel: sel.annotation.set_text( '原创 2020-10-20 15:32:01 · 2221 阅读 · 0 评论