
python
涂伟峰
这个作者很懒,什么都没留下…
展开
-
Python对redis的list切片
众所皆知Redis的增删改查指令十分简陋,我们如何在Python中像操作自身的list一样操作redis的list呢?比如切片赋值:a = [1,2,3,4]b = [6,7,8,9]a[:3:2] = b[:3:2]print(a)下面开始准备工作在redis中这么一个list,key为testlist2127.0.0.1:6379> lrange testl...原创 2019-06-11 15:27:28 · 292 阅读 · 0 评论 -
Python技巧和陷阱
Python 拥有众多方便的标准库,这是它强大的地方同时也留下很多陷阱。1. strip从中文教程文档描述来看,该方法应该是去除头尾的字符序列Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。官方注释也差不多是这个意思strip(...) S.strip...原创 2019-05-29 14:30:48 · 343 阅读 · 0 评论 -
骚气的Python之装饰器
装饰器是Python的特性,采用了闭包的思想,使用得当可简化代码,提高可读性。装饰器的特点是只运行一次,就是说不能动态添加装饰器,只能在需要装饰的方法上手动添加装饰器。下面举个简单的例子import datetimedef logtime(f): def setf(*args, **kw): print(datetime.datetime.now()) ...原创 2019-05-23 17:16:18 · 289 阅读 · 0 评论 -
spark 入门(一)
spark 的安装配置:https://blog.youkuaiyun.com/seTaire/article/details/90263765试跑一个简单的word count例子检验一下安装配置是否成功。1. 首先在主机 sh start.sh 启动三个容器,start.sh 在https://blog.youkuaiyun.com/seTaire/article/details/902637652....原创 2019-05-18 14:23:46 · 255 阅读 · 0 评论 -
Python关于try语句需要注意的几点
try语句属于防御性编程的一种,允许我们捕获异常进行处理,提高程序的健壮性。在 finally 块中不管你在 except 中是否抛出异常都会执行,如下:def f(): try: print("try") a = 0 /0 except Exception as e: print("except") rai...原创 2019-05-27 10:31:55 · 1597 阅读 · 0 评论 -
Librosa音频处理(二)
对音频信号的处理可以通过librosa.ifgram 方法获取 stft 短时傅立叶变换的矩阵,对该矩阵进行修改搬移,再进行 istft 逆转换获得处理后的音频信号。y, sr = librosa.load(path)frequencies, D = librosa.ifgram(y, sr=sr)''' 中间对D进行处理就行了'''y = librosa.istft(D...原创 2019-05-17 13:55:49 · 3207 阅读 · 5 评论 -
Librosa音频处理(四)
使用 Librosa 对音色可视化specshow.pyimport librosa import matplotlib.pyplot as pltimport numpy as npimport librosa.displayimport osclass specshowplot: def __init__(self): self.plotlist ...原创 2019-05-21 11:13:19 · 2505 阅读 · 0 评论 -
spark 入门(二)
关于提交集群任务,test.py 是word count 例子,在上篇内容有提到spark-submit --master spark://master:7077 --driver-memory 512M --executor-memory 512M test.py 如果出现报错initial job has not accepted any resources参考:https...原创 2019-05-20 15:22:07 · 230 阅读 · 0 评论 -
Librosa音频处理(五)
使用keras做一个狗猫叫声的分类器。已上传github:https://github.com/tuweifeng/SoundClassifierimport librosaimport kerasimport numpy as npfrom sklearn.preprocessing import LabelBinarizerimport osfrom sklearn.m...原创 2019-06-26 10:05:22 · 2499 阅读 · 0 评论 -
一道小学推理题
偶然看到了一道小学推理题,打算用代码的形式解出来。from collections import defaultdictfrom copy import deepcopyimport itertoolsclass MathGame: def __init__(self, calcinfo, n1, n2, answer, cards="abcdefghijklmn")...原创 2019-06-21 18:04:07 · 510 阅读 · 1 评论 -
BP神经网络 + Python
参考博客:https://blog.youkuaiyun.com/weixin_41090915/article/details/79521161整理了一下代码import numpy as npimport matplotlib.pyplot as pltclass Test: def __init__(self, classes, numberperclass, dimen...原创 2019-06-26 18:03:27 · 425 阅读 · 0 评论 -
Python的加速模块numba
关于numba的介绍有很多,就是一个可以把大量重复代码即时编译为机器码来加快程序运行速度的库。优点是快,方便,但缺陷也很明显,如很多类型不兼容,使用时不太灵活,必须把f方法内包含的所有方法加上装饰器,在数值计算量小时反而会减慢速度等等...import librosafrom numba import njitimport timepath = "/Users/birenjianmo...原创 2019-08-22 17:06:55 · 558 阅读 · 0 评论 -
动态信号周期检测
一段信号的周期可能会动态变化,下面的代码是根据信号幅度极大值的分布来判断是否存在周期,https://blog.youkuaiyun.com/seTaire/article/details/99968948主要是在该方法上做了一点改变。实验结果:import librosapath = "/Users/birenjianmo/Desktop/learn/librosa/input/1你好.wa...原创 2019-08-22 11:56:16 · 1411 阅读 · 0 评论 -
动态规律查找
如何用程序得到一组数字的动态规律,前提已知该规律只有加法。例如:1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 27, 37, 47, 67可直观的发现这几组规律: [1, 2, 3], [11, 12, 13], [21, 22, 23], [31, 32, 33] 和 [27], [37], [47]下面用代码实现:a = [...原创 2019-08-21 16:20:24 · 748 阅读 · 0 评论 -
动态规律查找(二)
优化了之前的代码,使查询更快a = [1,2,4, 11,12,14, 21,22,24, 31,32,34, 41,27,37,47,67, 1,2,4, 11,12,14, 21,22,24, 31,32,34, 41,27,37,47,67]data = []for i in range(1,len(a)): data.append( a[i]-a[i-1] )data...原创 2019-08-24 09:26:52 · 260 阅读 · 0 评论 -
声音匹配
每个人说话的内容虽然不一样,但是频率基本不会变,所以声音匹配原理是根据频率的分布情况。import librosaimport osfrom collections import Counterimport numpy as npfrom functools import reducedirpath = "/Users/birenjianmo/Desktop/learn/libr...原创 2019-08-19 15:34:19 · 1472 阅读 · 5 评论 -
Librosa音频处理(六)
劣质的麦克风在录音时会把电流和嗡嗡的背景声录进去,通过对噪声取样去除频率可以达到降噪的目的。主要步骤:1. 噪声取样2. 统计频率3. 移除频率代码如下:'''采样降噪'''def test2(n, y, sr): indexs = librosa.effects.split(y, top_db=25-n) noicefrequencies = []...原创 2019-07-09 11:36:21 · 5996 阅读 · 2 评论 -
BP神经网络 + Tensorflow
将上一篇文章的代码翻译为Tensorflow。链接:https://blog.youkuaiyun.com/seTaire/article/details/93760032训练结果:import numpy as npimport tensorflow as tfdef randomdata(classes, numberperclass, dimension): x ...原创 2019-06-27 16:27:21 · 573 阅读 · 0 评论 -
骚气的Python之Celery
在我的__init__.py 文件里配置,需要先安装redis,mac上安装: brew install redisfrom celery import Celery#初始化Flask对象app = Flask(__name__)app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'app.config['CEL...原创 2019-04-03 17:34:04 · 306 阅读 · 0 评论 -
pip 的使用
将所有的库和版本输出到xxx.txtpip freeze > xxx.txt安装xxx.txt里的库pip install -r xxx.txt原创 2019-04-12 15:49:33 · 225 阅读 · 0 评论 -
骚气的Python之进程通信
# -*- coding: utf-8 -*-from multiprocessing import Process, Queueimport os, time, random# 写数据进程执行的代码:def write(q): print('Process to write: %s' % os.getpid()) for value in ['A', 'B', 'C'...转载 2018-11-06 12:32:22 · 454 阅读 · 0 评论 -
骚气的Python之捕获输出流
有时候我们的代码里可能要调用控制台命令,比如我想用Python写一个批量编译 .java 文件的脚本,用到如下代码常规用法 os.system import os,tracebacktry: p = os.system("javac Test.java") print pexcept: print "\nexcept:\n" print tracebac...原创 2018-11-03 20:35:47 · 2765 阅读 · 0 评论 -
selenium.common.exceptions.WebDriverException: Message: unable to set
本人使用的版本是python3.7selenium3.14.1macOS10.13.3另外是在谷歌浏览器做的实验下面看代码: 我想要添加一个name为MUSIC_U的cookie,运行后出现错误selenium.common.exceptions.WebDriverException: Message: unable to set # -*- coding: ut...原创 2018-09-21 19:35:12 · 2236 阅读 · 0 评论 -
骚气的Python之k-means算法
1.回顾上次对验证码进行了去噪和灰度化,这次对它进一步地分类处理,这里用颜色区分,显然是分成4个类 2.代码关于算法原理我就不多说,下面看代码#encoding=utf-8from PIL import Imageimport math import copyimport randomclass Pixel(object): def __init__(self,x,y):原创 2017-11-23 16:33:30 · 1122 阅读 · 0 评论 -
骚气的Python之验证码处理
在做爬虫的时候有时需要识别验证码,但是验证码一般都有干扰物,这时需要对验证码进行预处理,效果如下: from PIL import Imageimport itertoolsimg = Image.open('C:/img.jpg').convert('L') #打开图片,convert图像类型有L,RGBA# 转化为黑白图def blackWrite(img): blackXY =原创 2017-11-20 00:14:19 · 5328 阅读 · 1 评论 -
Django联表查询
models.py文件内容class Article(models.Model): title =models.CharField(max_length=50) content = models.CharField(max_length=800) def __unicode__(self): return self.titleclass author(mode原创 2017-11-13 18:41:30 · 1182 阅读 · 0 评论 -
骚气的Python之8皇后
test1:可用于实现遗传算法(还没写)test2:遍历找出所有解(92)下面看代码#encoding=utf-8import numpy as np # 导入numpy前先安装import syssys.setrecursionlimit(10000000) # 设置最大递归次数,默认为1000arrList = []原创 2017-10-23 17:08:51 · 740 阅读 · 0 评论 -
骚气的Python之递归
举一个例子:计算1-10000中”1”的出现次数import numpy as np # 使用前请先安装numpy模块import syssys.setrecursionlimit(100000) # python默认最大递归次数为1000,这里设置为100000def calc(number): if number>=10000:原创 2017-10-23 13:48:58 · 1086 阅读 · 0 评论 -
骚气的Python之正则表达式
正则表达式有什么用?正则表达式的恰当运用可以提高查找效率减少代码量,便于维护简单易学,适用于所有编程语言下面来看一些例子import res = '38x1x234x35x612x3yxxx'patten1 = re.compile("x.*x") #返回一个,中间重复xprint '1\n',patten1.findall(s)patten2 = re.compile("x\w.*?x原创 2017-10-30 21:42:04 · 2749 阅读 · 0 评论 -
骚气的Python之大顶堆算法
大顶堆是一种结合二叉树的排序算法,平均时间复杂度为Ο(nlogn) ,不清楚该算法的可以先看详细的介绍https://blog.youkuaiyun.com/sun_ru/article/details/52004044# -*- coding: utf-8 -*-class HeapTree(object): leftChild = None rightChild = None ...原创 2018-11-09 17:49:50 · 931 阅读 · 0 评论 -
骚气的Python之heapq的使用
官网文档https://docs.python.org/2/library/heapq.htmlheapq.heapify(x) 把 list 堆排序(下文中参数里的 heap 都是由这一步得到的 list,如果未先 heapify 会出现错误)import heapqa = [2,4,1,5,6,3]heapq.heapify(a)print a[1, 4, 2...原创 2018-11-09 23:13:59 · 873 阅读 · 0 评论 -
骚气的Python之Numpy
假定一组数据a = np.array([ [1, 2, 3], [4, 5, 6]])数据位移(第一列向后移动一位) a[:,0] = np.roll(a[:,0], 1) 结果: [[4 2 3] [1 5 6]] 条件修改 a[a%2==0] = 0 结果: [[1 0 3] [0 5 0]] 转为一...原创 2019-03-14 15:01:48 · 517 阅读 · 0 评论 -
Python数组转字符串
a = [1,2,3]d = "".join(map(lambda x:str(x),a))print(d)结果是:123原创 2019-01-17 10:13:22 · 5109 阅读 · 0 评论 -
Python使用mfcc的两种方式
Librosa import librosa filepath = "/Users/birenjianmo/Desktop/learn/librosa/mp3/in.wav"y,sr = librosa.load(filepath)mfcc = librosa.feature.mfcc( y,sr,n_mfcc=13 ) 返回结构为(13,None)的np.Array,None...原创 2019-01-03 17:42:16 · 6862 阅读 · 2 评论 -
Conda使用方法
安装 conda install 包名=版本号 查看 conda list 查看环境名 conda env list 复制环境 conda create -n 环境名 --clone 被复制的环境路径 复制指定python版本的环境 conda create -n 环境名 python=2.7 切换环境 source activate 环境名 退出环境 source deactivat...原创 2018-12-11 18:16:43 · 676 阅读 · 0 评论 -
深度学习MNIST手写数字识别
python3.6tensorflow1.12.0# -*- coding: utf-8 -*-import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)...原创 2018-12-14 18:18:28 · 471 阅读 · 0 评论 -
MNIST手写数字识别
python3.6tensorflow1.12.0 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)W = tf.Variable(tf.zero...原创 2018-12-14 16:03:27 · 264 阅读 · 1 评论 -
Librosa音频处理(一)
Librosa是一个用于音乐和音频分析的python包,如果没学过《数字信号处理》需要先了解一下相关的基础知识,傅立叶变换,梅尔频率倒谱安装:pip install librosa环境:Python3.6我们先做个简单的变声 import librosay,sr = librosa.load("/Users/birenjianmo/Desktop/learn/librosa...原创 2018-12-08 09:26:43 · 16303 阅读 · 3 评论 -
骚气的Python之线性回归
公式1公式2# -*- coding: utf-8 -*-import numpy as npimport pylabdef comput(x,y,time,rate): b = 0.0 m = 0.0 n = float(len(x)) for i in xrange(time): b_gradient = -2/n...原创 2018-11-16 18:13:03 · 720 阅读 · 0 评论 -
骚气的Python之单例模式
单例模式是一种软件设计模式,运行中只允许存在一个实例对象,举个例子class T(object): def __init__(self): print selfa = T()b = T()执行代码,将输出下面信息<__main__.T object at 0x10bdc4cd0><__main__.T object at 0x10b...原创 2018-11-04 21:36:01 · 262 阅读 · 0 评论