
每天一点python
jinmingz
ASR SRE DeepLearning
展开
-
安装scipy的时候要可以安装 pillow
直接安装scipy, 然后在保存image的时候会提示找到方法.需要事先装一个 pillow 或者其他的然后卸载scipy 再重新安装 scipypip install pillowpip install scipy原创 2019-01-13 22:05:06 · 1214 阅读 · 0 评论 -
numpy 数据的存取
numpy 数组的存取常用的有两种方式:注意: 一定要注意 tofile 和 fromfile 成对使用,不可与 load 和 save 混用,否则数组长度不一致。numpy.tofile() 和 numpy.fromfile(): 保存为二进制格式,但是不保存数组形状和数据类型, 即都压缩为一维的数组,需要自己记录数据的形状,读取的时候再reshape.>>> import numpy>>原创 2017-07-07 12:05:33 · 1213 阅读 · 0 评论 -
mac下安装pyaudio
小小搬运工: https://stackoverflow.com/questions/33851379/pyaudio-installation-on-mac-python-3主要步骤是:xcode-select --install #安装xcode, 已经装好的的话,执行的时候会提示brew remove portaudio #先卸载brew install portaudio #重新安转载 2017-06-09 17:28:35 · 7341 阅读 · 0 评论 -
python 读取文件列表
先介绍两种python的方法,都是基于os库中的方法:demo1:import osfiles = os.listdir("./")这个只列出当前目录下的所有文件名称(不管是文件合适文件夹,只列出名称),这个适用于已知具体的文件路径的情况demo2:for root, dirs, files in os.walk("./"): for file in files: if ".w原创 2017-05-25 12:36:42 · 1225 阅读 · 0 评论 -
python的join函数
做数据预处理时遇到一个问题,就是如何把一个list的元素用一个特殊符号连接起来,当然可以用for循环,看起来比较麻烦。 b = ['American', 'Avocet', '2', 'individual'] s = '_' spk_name = s.join(b) print spk_name #结果是 'American_Avocet_2_individ原创 2016-10-25 00:20:00 · 480 阅读 · 0 评论 -
numpy中的 dot, outer and *
每次用到都得先测试一下,整理一下以后会方便一些,先举几个例子吧:demo1:import numpy as npa = [[1,2,3],[4,5,6]]a = np.array(a)b = [[1,2],[4,5],[3,6]]b= np.array(b)#一个(2,3), 一个(3,2),是可以正常的矩阵相乘的c = np.dot(a, b)print c-->result: a原创 2016-11-25 22:17:05 · 10883 阅读 · 3 评论 -
python topn
如果要取一个数组的前 N 个最大的数或者是最小的, Python 内建模块 heapq.import heapqA = [12,3,4,7,9,1,23]result = heapq.nlargest(3, A)用快排的思想实现 topn: 占坑 用最小堆的方法实现topn: 占坑原创 2017-02-09 11:31:38 · 2541 阅读 · 1 评论 -
python数组的赋值和拷贝
直接看demo>>> a = np.array([1,2,3])>>> b = np.array([4,5,6])>>> (a==b).all() #比较两个数组元素是否都相等, 可以用于k-meansFalse>>> c=a>>> (a==c).all()True>>> c[0] = 10 #等号这种赋值相当对象引用(内存地址)传递,改变c, a也跟着变了,所有不管怎么改都是tru原创 2016-12-17 23:13:52 · 12472 阅读 · 0 评论 -
numpy.newaxis
第一次见到这个东西,来研究一下:从字面上是插入新的维度的意思demo1: 针对一维的情况>>> b = np.array([1, 2, 3, 4, 5, 6])>>> b[np.newaxis]array([[1, 2, 3, 4, 5, 6]])>>> c = b[np.newaxis] #equals c = b[np.newaxis,:]>>> b.shape(6,)>>> c原创 2016-11-28 13:37:42 · 33778 阅读 · 0 评论 -
parser.add_argument中的action
有一个比较有意思的传参方式: 比如在 demo1.py 中指定 action=’store_true’的时候: parser.add_argument(‘–is_train’, action=’store_true’, default=False)在运行的时候: python demo1.py 默认是False python demo1.py –is_train 是True, 注意原创 2017-08-02 10:46:00 · 26059 阅读 · 4 评论 -
sort函数慎用
今天晚上一个大坑,python自带的sort函数, 首先他是原地排序的方法,就是会改变自身的值,没有返回值。但是比如我一个numpy矩阵 M.shape(10, 8):N = M[:,3] #取M的第三列作为NN.sort() #对N自排序, 这是N是有序的!!! 但是 M这时候也是 第三列有序的!!!原创 2017-08-11 00:05:33 · 1103 阅读 · 0 评论 -
python多进程demo
任务需求,在声纹识别任务中,模型库可能比较大,如果单线程的话,每个测试句都跟几百个模型计算得分,那么测试过程太慢了。这里采用 多进程 和 多线程(暂时空白) 的方式进行处理。多进程版本(省略了内部函数实现)print("Test Stage")print("test length ", len(testList))#这个test的数据太多了,并且每一个要跟500个model进行计算,所以必须采用原创 2017-08-05 15:48:55 · 641 阅读 · 0 评论 -
pythob 浮点数比较(0.3)
关于浮点数的比较, 比如下面, 当等于0.3的是时候是不想等的,而其他的数值都可以.这是因为0.3在计算机内部转二进制, 然后再转浮点数的时候, 会得到一个 比0.3稍微大一点的数, 所以在浮点数比较的时候一定不要直接比较,而是要加 round(0.3, 1).计算机组成原理已经彻底还给老师了…for i in np.arange(start=0.1, stop=0.5, step=0.1)...原创 2018-11-29 21:15:17 · 577 阅读 · 0 评论 -
python2 与 python3 编码问题总结
之前遇到过好多各种各样的问题, 特别是在python2下, 先在准备总结, 遇到一个总结一个:unicode 字符在python2下采用 ,在python3下统一为 string 类型. 那么如何在python2下将一句话中的所有unicode字符转化为str类型呢? 下面几种都试试, 不行就直接换python3吧.# 数字true = u'1'print(type(true...原创 2018-09-16 14:26:35 · 295 阅读 · 0 评论 -
python pylot 画图
本文主要是记录最基本的用法:def plot_cccs(cccs, target_index): import matplotlib.pyplot as plt # 创建一个图片窗体 plt.figure(figsize=(10, 10)) # 设置横坐标的刻度 plt.xticks(np.arange(len(cccs))) # 先画一...原创 2018-04-09 22:54:37 · 1279 阅读 · 0 评论 -
sklearn 的 Normalizer的L1和 L2
Normalizer 正则化,跟z-score,对数转换,指数转换 这种数据转换方式不同。 L1 norm 是指对每个样本的每一个元素都除以该样本的L1范数. L2 norm 是指对每个样本的每一个元素都除以该样本的L2范数. bag of words features need to normalize with L1 norm fisher vector features need...原创 2017-09-06 21:59:11 · 6531 阅读 · 2 评论 -
python3 中元素的类型为 “ numpy.bytes_”
在一些框架中,里边的数据的dtype可能是“|S33”等类型, 当查看每一个元素的类型时显示 “numpy.bytes_”, 这时候如何转化为 str 类型呢? 将list每一个元素都decode一下就好了: segset = np.array([s.decode(‘UTF-8’) for s in segset])原创 2016-12-12 22:09:50 · 10691 阅读 · 0 评论 -
python import 不同层级导入
比如现在有这么一个问题:toolkit --eval_cap --bleu --bleu.pycaption --expr --run1.py要在run1.py中调用bleu.py中的函数,现在需要导入 bleu.py。step1: 首先在 toolkit,eval_cap, bleu **分别** 新建空的 __init原创 2017-10-16 00:25:12 · 1427 阅读 · 0 评论 -
numpy tostring() fromstring()
must specify the right dtype !!!aOut[54]: array([1, 2, 3])b = a.tostring()np.fromstring(b)Out[52]: array([ 4.94065646e-324, 9.88131292e-324, 1.48219694e-323]) #Wrong!Out[56]: array([1, 2, 3])原创 2017-10-30 13:16:04 · 12152 阅读 · 1 评论 -
python 画三维散图以及在图上加均值点
分析VAD情感的数据分布,画出对应的散点图,并在图上标记处每个类别的均值点。import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D # 必须要加这个,否则下面的projection会报错#new_line = re.sub('[,\[\]\n\%\t-]', ' ', lin原创 2017-10-18 23:34:09 · 2842 阅读 · 0 评论 -
python 读写 ndarray
h5py需要自己安装,看官方文档吧,装起来可能会遇到很多问题import numpy as npimport scipy.io as scioimport h5pya = np.array([[1,2],[3,4]])# mat typescio.savemat("stat1.mat", {'A':a})data = scio.loadmat("stat1.mat")print type(原创 2016-12-11 16:35:40 · 3029 阅读 · 0 评论 -
numpy.max() , sum()
也是之前没有好好研究过多维的情况:demo:>>>a = np.array(range(0,12))>>>b = a.reshape((3,4))>>>barray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])>>> np.max(b) #max number11>>> np.max(b, a原创 2016-11-28 15:31:05 · 26308 阅读 · 0 评论 -
python 输出是省略号的问题
这个问题非常非常重要,搞了一晚上都没解决好,但是真的很简单很简单, 如果你也 是用的numpy array, 如果你也想得到输出矩阵的全部内容,而不是省略形式,[[ 0.10284943 0.0959931 0.00076021 ..., -0.01035775 0.02561938 0.09741836] [-0.01446581 -0.0427694原创 2016-06-24 10:56:49 · 28558 阅读 · 4 评论 -
python中threading的常用方法的详解
详情请查看: http://blog.chinaunix.net/uid-27571599-id-3484048.html转载 2015-10-18 12:36:09 · 587 阅读 · 0 评论 -
关于python中with 和 try 块的联合使用的问题
最近学习python,看到with的用法,感觉不用try except就ok,但是事实证明并不是这样,如果不用try except,with语句只是帮你关闭没有释放的资源,并且抛出异常,但是后面的语句是不能执行的,所以为了即能够输出我们自定义的错误信息,又能不影响后面代码的执行,必须还得使用try except 语句。但是此时又会问:那使用with ,还有啥用呢?其实还有有用的,不用担心资源没有关原创 2015-10-17 23:52:30 · 8339 阅读 · 0 评论 -
the built-in function of reduce in python
Now,there is a practical function, reduce(arg1,arg2),then we could learn how use this function in a example.#!/usr/bin/env pythondef myadd(x,y): return x+ydef mysub(x,y): return x-ydef m原创 2015-09-28 00:00:58 · 490 阅读 · 0 评论 -
raw_input() and input() in python
there are two common input function,it's raw-input() and input(),thier function is receive the input from console ,but they have received and operated the data ,which is different. Then let's discuss原创 2015-09-23 23:56:46 · 461 阅读 · 0 评论 -
the file opeartion in python(一)
#! /usr/bin/env python# coding = utf-8file_path = "/home/zjm/python_test/file_fold/hello"f = open(file_path,"w") #write mode ,if not exits,then create one filef.write("firstline\n")f.write("secon原创 2015-09-23 00:14:50 · 437 阅读 · 0 评论 -
slice operation of consequence in python
the slice operation consequence is easy.the prototype of the function is str[start:end:step] .then let's see the following examples:>>>str1='abcde'>>>str1[1:3]'bc'>>>str1[:] #defau原创 2015-09-24 13:07:34 · 447 阅读 · 0 评论 -
关于python实现二维数组所有元素求和
今天遇到一个需求,求二维数组的和,尽量在一行代码中完成: 关于二维数组求和的几种方法: a = [[1,2],[3,4],[5,6]] 1.sum(map(sum,a)) #first, map(func,a) 函数是对a中的每一个元素进行sum操作 解释一下map函数, map(fund, a) equals [func(i) fo原创 2016-04-17 14:12:34 · 38632 阅读 · 5 评论 -
正则表达式中匹配字符中括号'['
>>> import re>>> a = "abc[123]abc">>> b = re.match(r"(.*)\[([^\[\]]*)\](.*)",a,re.I|re.M)>>> b.group()'abc[123]abc'>>> b.group(1)'abc'>>> b.group(2)'123'用python 来实现这段正则中的重点在匹配中括号下面我尽量解释一原创 2016-02-23 12:39:49 · 23666 阅读 · 4 评论 -
python使用yield来减少内存开销
本文参考自:http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/以斐波那契数列的实现来说明这个问题:demo1:def fab(max): n, a, b = 0, 0, 1 L = [] while n < max: L.append(b)原创 2016-01-17 15:58:53 · 5530 阅读 · 0 评论 -
python 中类的重载以及logging中的level
由于之前深受java思想的洗礼,以至于现在学python满满的还都是抽象抽象抽象,一个简单的日志打印,轻微强迫症的我也必须 写一个通用的日志打印工具类,花费了差不多一天的时间,遇到了很多问题,当然最后都解决了:下面将完整的代码share to you:#!/usr/bin/pythonimport loggingimport timeimport commands'''func原创 2015-11-15 14:11:25 · 1004 阅读 · 0 评论 -
the basic operation of sequence in python
in the last passage , we hava learned slice operation of consquence , then we will learn the basic operation of consquence.First , part of basic function is built-in function .Then , if you want原创 2015-09-24 14:09:14 · 518 阅读 · 0 评论