
python
zhangpeterx
这个作者很懒,什么都没留下…
展开
-
从洗牌算法谈起--Python的random.shuffle函数实现原理
此文首发于我的个人博客:从洗牌算法谈起–random.shuffle实现原理 — zhang0peter的个人博客昨天看知乎的时候看到了洗牌算法(Knuth shuffle, 最初版本叫Fisher–Yates shuffle/ Sattolo’s algorithm):世界上有哪些代码量很少,但很牛逼很经典的算法或项目案例? - 知乎具体的问题是:如何打乱一个数组,确保数组乱的很随机。伪...原创 2020-01-16 10:13:24 · 4412 阅读 · 0 评论 -
Python-logging报错解决:UnicodeEncodeError: 'gbk' codec can't encode character '\u' in position: illegal
我在Python3 中使用Logging模块把日志打到终端输出时会报错。代码如下:import logginglogging.basicConfig(level=logging.INFO, filename='log.log', format='%(asctime)s - %(name)s - %(levelna...原创 2020-01-07 15:12:21 · 4057 阅读 · 1 评论 -
Python 爬虫使用pyppeteer 网页进行向下翻页操作
pyppeteer是Python接口的puppeteer,用来操作chrome浏览器,比传统的selenium 更好用。我想把网页拖到底部,scroll page down,在网上找各种方法。第一种如下:Is there a way to scroll to end of page in pyppeteer · Issue #205 · miyakogi/pyppeteerawait pa...原创 2020-01-06 13:15:01 · 4784 阅读 · 0 评论 -
anaconda -spyder报错解决-UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 611: illegal
下午在写Python程序时突然遇到报错:This command failed to be executed because an error occurred while trying to get the file code from Spyder's editor. The error was:An exception has occurred, use %tb to see the...原创 2020-01-05 15:45:25 · 8558 阅读 · 3 评论 -
anaconda spyder使用协程报错解决:RuntimeError: This event loop is already running
早上在anaconda的spyder中写协程代码时遇到了报错。代码如下:import asyncioasync def coroutine(): print("hey") await asyncio.sleep(1)asyncio.get_event_loop().run_until_complete(coroutine())报错如下:Python 3.7.4 (...原创 2020-01-04 13:51:41 · 12396 阅读 · 1 评论 -
LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法
LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法LeetCode题解专栏:LeetCode题解我做的所有的LeetCode的题目都放在这个专栏里,大部分题目C++和Python的解法都有。题目地址:Maximum XOR of Two Numbers in an Array - LeetCodeGive...原创 2019-10-31 08:39:19 · 265 阅读 · 0 评论 -
mxnet报错解决:AttributeError: module 'mxnet.context' has no attribute 'num_gpus'
早上在跑mxnet的mnist的示例代码时报错了,代码如下:import mxnet as mxctx = mx.gpu(0) if mx.context.num_gpus() > 0 else mx.cpu(0)报错如下:---------------------------------------------------------------------------Attr...原创 2019-06-05 10:44:15 · 3131 阅读 · 2 评论 -
keras和tensorflow使用 fit_generator 批次训练
fit_generator 是 keras 提供的用来进行批次训练的函数,使用方法如下:model.fit_generator(generator, steps_per_epoch=None, epochs=1, verbose=1, callbacks=None, validation_data=None, validation_steps=None, ...原创 2019-06-05 11:57:13 · 5828 阅读 · 0 评论 -
Python:matplotlib绘图时指定图像大小,放大图像
matplotlib绘图时是默认的大小,有时候默认的大小会感觉图片里的内容都被压缩了,解决方法如下。先是原始代码:from matplotlib import pyplot as pltplt.figure(figsize=(1,1))x = [1,2,3]plt.plot(x, x)plt.show()关键的代码是plt.figure(figsize=(1,1)),生成的图片...原创 2019-06-01 23:08:05 · 59073 阅读 · 4 评论 -
keras和tensorflow 报错解决:UserWarning: Method on_batch_end() is slow compared to the batch update Check
早上在使用keras时报错如下:C:\Users\peter\Anaconda3\lib\site-packages\keras\callbacks.py:122: UserWarning: Method on_batch_end() is slow compared to the batch update (0.115551). Check your callbacks.这是一个警告,可以...原创 2019-06-07 13:17:10 · 7864 阅读 · 1 评论 -
Windows 10 Anaconda Python 3.7 安装 MXNet GPU版
MXNet CPU版本可以直接通过pip进行安装:pip install mxnetMXNet GPU版本不推荐通过pip进行安装,推荐使用conda进行安装:conda install -c anaconda mxnet-gpu运行结果如下:(base) PS>conda install -c anaconda mxnet-gpuCollecting package met...原创 2019-06-02 09:54:14 · 6097 阅读 · 3 评论 -
keras和tensorflow使用 keras.callbacks.TensorBoard 可视化数据
TensorBoard 是一个非常好用的可视化工具1.数据写入在keras中,使用方法如下:import kerasTensorBoardcallback=keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=False, embeddings_fr...原创 2019-06-04 08:58:13 · 11519 阅读 · 1 评论 -
keras和tensorflow使用 keras.callbacks.EarlyStopping 提前结束训练
一般来说机器学习的训练次数会设置到很大,如果模型的表现没有进一步提升,那么训练可以停止了,继续训练很可能会导致过拟合keras.callbacks.EarlyStopping就是用来提前结束训练的。在keras中,使用方法如下:import kerasearly_stopping=keras.callbacks.EarlyStopping(monitor='val_loss', min_...原创 2019-06-05 10:01:17 · 11313 阅读 · 5 评论 -
我看过的Python方面的好文章
本文不定期更新,最后更新于2019-5-18GitHub上实时更新,地址:good-articles-by-sort/Python.mdPython用Python进行奇异值分解(SVD)实战指南Python 中的元编程python异步编程之asyncio(百万并发)Python 字节码介绍日常 Python 编程优雅之道Python 开发者不得不知的魔术方法(Magic Met...原创 2019-05-18 10:56:09 · 245 阅读 · 0 评论 -
问题解决:树莓派上Python用pip进行安装耗尽内存,卡死
最近在树莓派上用Python3 的pip3进行安装时,经常会耗尽内存,卡死在那里不动了。具体的效果如下:-> # pip3 install grequests Collecting grequestsRequirement already satisfied: requests in /usr/lib/python3/dist-packages (from grequests)C...原创 2019-05-25 16:58:26 · 3867 阅读 · 0 评论 -
Python 报错解决:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
早上升级完pip,后报错如下:-> # pip install --upgrade pwntools/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) ma...原创 2019-05-12 12:14:58 · 2477 阅读 · 0 评论 -
图片去噪:python,线性回归
未完待续原创 2019-05-19 22:01:58 · 4820 阅读 · 11 评论 -
Python scapy库监听网卡,抓取HTTP包
今天查阅了诸多资料,最后找到了一个最好用的,如下:from scapy.all import *stars = lambda n: "*" * ndef GET_print(packet): return "\n".join(( stars(10) + "GET PACKET" + stars(10), "\n".join(packet.sprin...原创 2019-05-07 20:55:01 · 6910 阅读 · 1 评论 -
ubuntu 16.04 python3 使用ryu
ubuntu 16.04 python3 使用ryu先安装:pip3 install ryu从GitHub安装:git clone https://github.com/osrg/ryu.gitcd ryupip3 install .查看版本号:-> # ryu --versionryu 4.31运行:ryu-manager ryu/ryu/app/simple_...原创 2019-04-05 21:44:28 · 2689 阅读 · 0 评论 -
keras/tensorflow 模型保存后重新加载准确率为0 model.save and load giving different result
我在用别人的代码跑程序的时候遇到了这个问题:keras 模型保存后重新加载准确率为0GitHub上有个issue:model.save and load giving different result · Issue #4875 · keras-team/keras这个issue有150个回复了,大部分人都是遇到了这个问题。我用tensorflow重写了一遍,没有解决问题。然后我怀疑不是...原创 2019-04-17 12:05:11 · 2220 阅读 · 4 评论 -
报错解决:InvalidArgumentError: Received a label value of 101 which is outside the valid range of [0, 101
报错解决:InvalidArgumentError: Received a label value of 101 which is outside the valid range of [0, 101).早上在使用tensorflow时,产生了如下报错:Traceback (most recent call last): File "<ipython-input-1-b98f58f...原创 2019-04-14 10:10:39 · 8659 阅读 · 10 评论 -
在Python中使用LLVM接口:llvmpy和llvmlite
文章目录1.过时的llvmpy2.llvmlite1.过时的llvmpyllvmpy是llvm C ++库的Python包装器,允许简单访问编译器工具。但是这个库已经不再更新了,只支持LLVM 3.3,不支持更新的版本。2.llvmlite项目的GitHub地址:numba/llvmlite: A lightweight LLVM python binding for writing J...原创 2019-06-19 22:54:56 · 11109 阅读 · 1 评论 -
安装llvmlite报错解决:RuntimeError: llvm-config failed executing, please point LLVM_CONFIG to the path for
晚上在安装llvmlite时报错:zhang@debian:~$ pip3 install llvmlite==0.16.0Collecting llvmlite==0.16.0 Using cached https://files.pythonhosted.org/packages/23/c7/56d7c18564783f33206b3cb6ce861a8d681e8be1a124c56...原创 2019-06-19 23:08:51 · 23937 阅读 · 10 评论 -
Google colab: 修改系统时间 change system time
Google colab对于机器学习来说非常好用,有免费的GPU可以用。注意:一个Google colab的示例最长可以持续12小时,窗口关闭后只会继续跑90分钟。但是因为服务器并不在国内,所以系统时间并不是东八区的时间,但我希望修改时间到北京时间。我先尝试如下命令:!date -R!apt-get install ntpdate!ntpdate ntp.sjtu.edu.cn!d...原创 2019-06-04 14:31:13 · 3538 阅读 · 1 评论 -
Windows使用MSVC,命令行编译,链接64位dll,Python调用
前一篇博客:Windows下使用Visual Studio自带的MSVC,命令行编译C/C++程序SampleDLL.cpp代码如下:// SampleDLL.cpp#define EXPORTING_DLL#include "sampleDLL.h"BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,...原创 2019-09-07 11:11:42 · 2413 阅读 · 0 评论 -
Python 3/前端 画图工具:Matplotlib,canvajs,pyecharts
之前我一直是用Matplotlib画图,写了挺多博客:Python:matplotlib绘图时指定图像大小,放大图像matplotlib绘制平滑的曲线Matplotlib使用日期作为横坐标matplotlib 设置坐标轴单位Matplotlib画图示例如下:import matplotlib.pyplot as pltfrom pylab import mplmpl.rcPa...原创 2019-08-22 20:13:40 · 997 阅读 · 0 评论 -
Python 3 判断2个字典相同
Python自带的数据结构dict非常好用,之前不知道怎么比较2个字典是否相同,做法是一个一个key比较过去。。。现在想到可以直接用==进行判断!!!a = dict(one=1, two=2, three=3)b = {'one': 1, 'two': 2, 'three': 3}c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))d = ...原创 2019-08-05 15:11:21 · 18041 阅读 · 1 评论 -
matplotlib 设置坐标轴单位
早上想把用matplotlib画的x轴的坐标单位标上去,但是找了半天没找到方法,谁有好的方法请告诉我。找到了调整x轴坐标刻度的方法,也就是修改坐标轴的密度:plt.xticks([1,2,3,4])参考:Python matplotlib - setting x-axis scale - Stack Overflow然后我亲自提了一个问题:python - how to add matp...原创 2019-07-22 19:11:43 · 28298 阅读 · 5 评论 -
报错解决:TypeError: Object type class 'str' cannot be passed to C code
下午在使用Crypto的时候报错如下:Traceback (most recent call last): File "/usr/lib64/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/usr/lib64/python3.6/...原创 2019-07-17 19:54:18 · 27590 阅读 · 2 评论 -
Python:生成两个日期间的随机日期
先提一下我的这篇文章:Python 超快生成大量随机数的方法然后就是代码实现了:import randomimport datetimedef random_date(start=begin, end=end): """Generate a random datetime between `start` and `end`""" return start + dateti...原创 2019-07-19 15:03:33 · 1245 阅读 · 0 评论 -
Python3 的多线程使用:_thread,threading,multiprocessing
在用Python的多线程,之前一直都是用到的时候去搜,现在记录一下。不用多线程,可以通过bash脚本实现原创 2019-07-17 10:00:26 · 628 阅读 · 0 评论 -
Python 超快生成大量随机数的方法
文章目录1.random.randint2./dev/urandom->os.urandom3.fastrand ×4.numpy5. AES CTR综合测试和总结今天花费了很多时间在想办法提高Python的随机数生成的速度,因为我需要生成clickhouse的测试数据。我每生成1亿行数据,每行包括2个随机的uint32,1个uint16,1个uint8和一个随机时间,花费的时间大约在6...原创 2019-07-19 09:49:33 · 11421 阅读 · 0 评论 -
centos/Debian/Ubuntu下编译安装pypy
PyPy的速度是比Python快的,根据官方说法,速度时CPython的2倍-10倍。但PyPy也是有缺点的,那就是PyPy是由RPython实现的,RPython是Python的子集。PyPy快的主要原因是使用了JIT。但在包管理器中的pypy还停留在Python2的版本,因此我们需要从官网下载:PyPy - Download and install可以直接下载二进制文件,解压,运行:...原创 2019-07-18 14:17:51 · 977 阅读 · 0 评论 -
centos/Debian/Ubuntu上安装PyCryptodome/Crypto
注意:PyCryptodome是Crypto的升级版,不要安装旧的Crypto了。如果能联网,直接装:pip3 install PyCryptodome GitHub仓库地址:Legrandin/pycryptodome: A self-contained cryptographic library for Python如果你的机器不能联网,编译安装:wget https://code...原创 2019-07-18 12:00:24 · 2253 阅读 · 3 评论 -
cityhash 算法的使用
今天在看ClickHouse源码时,注意到ClickHouse使用了cityhash128作为自己的HASH算法:The first 16 bytes are the checksum from all other bytes of the block. Now only CityHash128 is used.cityhash 算法是谷歌提出的哈希算法,之前从来没有听说过。GitHub仓...原创 2019-07-11 10:31:12 · 10537 阅读 · 3 评论 -
Linux下使用终端调试Python程序:pudb
根据官网的介绍:PythonDebuggingTools - Python WikiPython自带调试程序pdb,pdb有点像gdb,强大,但不够好用。根据别人的推荐:Suggestions for Python debugging tools? - Stack Overflow我选择了pudb。pudb是一个命令行的GUI调试程序,需要先安装:pip install pudb运行...原创 2019-07-14 15:19:50 · 2397 阅读 · 1 评论 -
计算项目中的代码行数:Count the Lines of Code (LOC)
很多时候打开一个大的项目工程时,我们会想知道这个项目有多少行代码。Visual Studio 自带这个功能,在分析->窗口->代码度量值结果,但是这个功能比较耗时,因为同时分析了代码的耦合程度,继承深度等内容。powershell中可以实现这个功能,参考自:How do you count the lines of code in a Visual Studio solution?...原创 2019-07-13 23:46:59 · 1721 阅读 · 0 评论 -
ClickHouse 系列教程五:多种连接方法
文章目录clickhouse-clientHTTP 接口JDBCPython接口clickhouse-client你可以通过clickhouse-client命令行进行连接,使用方法如下:clickhouse-client --multiline --passwordHTTP 接口你可以通过HTTP直接连上ClickHouse:root@ubuntu:~# curl 'http:/...原创 2019-07-08 14:56:19 · 35753 阅读 · 5 评论 -
报错解决:ResourceExhaustedError: OOM when allocating tensor with shape
报错解决:ResourceExhaustedError: OOM when allocating tensor with shape早上在使用tensorflow时遇到如下报错:Traceback (most recent call last): File "C:\Users\peter\Anaconda3\lib\site-packages\spyder_kernels\custom...原创 2019-04-14 08:50:16 · 24347 阅读 · 9 评论 -
报错解决:alueError: When using data tensors as input to a model, you should specify the `steps_per_epoch
报错解决:valueError: When using data tensors as input to a model, you should specify the steps_per_epoch argument.晚上在使用tensorflow时报错如下: File "C:\Users\peter\Anaconda3\lib\site-packages\spyder_kernel...原创 2019-04-13 23:14:40 · 13075 阅读 · 2 评论