官方文档地址
class numpy.vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None)
广义函数类?

Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns an single or tuple of numpy array as output. The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy.

定义了一个矢量函数,输入是嵌套化的对象序列或者是numpy数组,
输出是单个或元组的numpy数组。跟 map()很类似。将函数pyfunc作用在序列化的对象上。
numpy.vectorize()只是为了方便,效率比较低。
官方例子:

import numpy as np
def myfunc(a, b):'Return a-b if a>b, otherwise return a+b'if a>b:return a-belse:return a+bvfunc = np.vectorize(myfunc)print vfunc([1, 2, 3, 4], 2)
#[3 4 1 2]

相当于 2 与列表中的1,2,3,4 依次 调用函数。即1,2; 2,2; 3,2; 4,2做vfunc计算。

print vfunc.__doc__
#Return a-b if a>b, otherwise return a+bvfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`')
print vfunc.__doc__
#Vectorized `myfunc`

打印以及修改函数信息。

out = vfunc([1, 2, 3, 4], 2)
print out[0]
#3
print type(out[0])
#<type 'numpy.int64'>
vfunc = np.vectorize(myfunc, otypes=[np.float])
out = vfunc([1, 2, 3, 4], 2)
print out[0]
#3.0
print type(out[0])
#<type 'numpy.float64'>

调用函数,默认返回的值的类型是由被调用函数的第一个值的类型决定的。比如第一个例子,第一次调用函数的参数是 1,2,则决定了输出的类型是int。
可以通过otype参数进行修改。

The data type of the output of vectorized is determined by calling the function with the first element of the input. This can be avoided by specifying the otypes argument.

def mypolyval(p, x):_p = list(p)res = _p.pop(0)while _p:res = res*x + _p.pop(0)return resvpolyval = np.vectorize(mypolyval, excluded=['p'])
print vpolyval(p=[1, 2, 3], x=[0, 1])
#[3 6]

使用excluded参数来确保某个参数不被矢量化,比如我们有一个x的多项式,我们想依次计算每个x的值在表达式中的值时,我们的多项式参数不能够被矢量化。
也可通过add()来将某个位置上的参数不被矢量化。

vpolyval.excluded.add(0)
vpolyval([1, 2, 3], x=[0, 1])
#array([3, 6])

(补充)
再看一下list的pop方法:

#lsit pop This method returns the removed object from the list.
aList = [123, 'xyz', 'zara', 'abc'];print "A List : ", aList.pop()
print "B List : ", aList.pop(2)
print "C List : ", aList.pop(0)# A List :  abc
# B List :  zara
# C List :  123
print aList
#['xyz']

pop按index从list中弹出数据。

signature参数的使用:
计算皮尔逊相关系数以及进行卷积操作。
此参数略高级。

import scipy.stats
pearsonr = np.vectorize(scipy.stats.pearsonr,signature='(n),(n)->(),()')
print pearsonr([[0, 1, 2, 3]], [[1, 2, 3, 4], [4, 3, 2, 1]])
#(array([ 1., -1.]), array([ 0.,  0.]))convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)')
print convolve(np.eye(4), [1, 2, 1])
# array([[ 1.,  2.,  1.,  0.,  0.,  0.],
#        [ 0.,  1.,  2.,  1.,  0.,  0.],
#        [ 0.,  0.,  1.,  2.,  1.,  0.],
#        [ 0.,  0.,  0.,  1.,  2.,  1.]])

python-numpy.vectorize()相关推荐

  1. [转载] python numpy 总结

    参考链接: Python中的numpy.compress 先决条件 在阅读这个教程之前,你多少需要知道点python.如果你想重新回忆下,请看看Python Tutorial. 如果你想要运行教程中的 ...

  2. 初学--Python numpy教程

    本文转载自:http://blog.chinaunix.net/uid-21633169-id-4408596.html 先决条件 在阅读这个教程之前,你多少需要知道点python.如果你想重新回忆下 ...

  3. dataframe,python,numpy 问题索引1

    # 找出只有赌场数据的账户 gp=data.groupby(['查询账号','场景标签'],as_index=True) tj=gp.size().reset_index()按查询账号和场景标签分组并 ...

  4. python numpy 欧氏距离

    python numpy 欧氏距离 import numpy as np a1 = np.arange(5) print(a1) [0 1 2 3 4] a2 = np.arange(5,10) pr ...

  5. Python Numpy多维数组.sum(axis=0/1/2...) 详解

    Python Numpy多维数组.sum(axis=0/1/2-) 详解 numpy中axis取值的说明 首先对numpy中axis取值进行说明:一维数组时axis=0,二维数组时axis=0,1,维 ...

  6. python numpy.array 与list类似,不同点:前者区分元素不用逗号,中间用空格,矩阵用[]代表行向量,两个行向量中间仍无逗号;  而list区分元素用逗号

    python numpy.array 与list类似,不同点:前者区分元素不用逗号,中间用空格,矩阵用[]代表行向量,两个行向量中间仍无逗号: 而list区分元素用逗号.而 numpy.array 的 ...

  7. python求向量函数的雅可比矩阵_在python Numpy中求向量和矩阵的范数实例

    np.linalg.norm(求范数):linalg=linear(线性)+algebra(代数),norm则表示范数. 函数参数 x_norm=np.linalg.norm(x, ord=None, ...

  8. python numpy数组和one-hot编码相互转换

    a=[0,0,1,0,1,0,1]result=[] for i, x in enumerate(a):if x==1:result.append(i)print(result) python num ...

  9. python绘制灰度图片直方图-python – numpy图像中灰度值的直方图

    我将图像加载到numpy数组中,并希望在直方图中绘制其颜色值. import numpy as np from skimage import io from skimage import color ...

  10. python numpy库安装-Python Numpy库安装与基本操作示例

    本文实例讲述了Python Numpy库安装与基本操作.分享给大家供大家参考,具体如下: 概述 NumPy(Numeric Python)扩展包提供了数组功能,以及对数据进行快速处理的函数. NumP ...

最新文章

  1. 面试官:来说说单点登录的三种实现方式
  2. java对于数组的定义_Java中方法的定义与使用,以及对数组的初步了解。
  3. 东方金信:让大数据为民服务
  4. 《mongodb权威指南》学习
  5. 【Matlab】你想知道在图表标题中显示变量的一切
  6. Python之深入解析Vulture如何一键找出项目中所有无效的代码
  7. python--学习笔记6 pandas
  8. CAP 发布 5.0 版本正式发布
  9. 133. Clone Graph
  10. c语言学习进阶-C语言程序出错处理
  11. Sklearn——交叉验证(Cross Validation)
  12. Pytorch 的迁移学习的理解
  13. 基于单片机的电子秤系统设计(电路+流程)
  14. 3d真人试衣php源码,VR3D虚拟试衣App开发 实现在线试衣购买
  15. 直播提醒|今晚八点半,最硬核情感分析技术讲解来袭!内附SKEP详解+大作业指导!...
  16. uniapp uni-swipe-action 滑动删除
  17. 使用commons-lang3实现Unicode码转中文
  18. python预测股票价格tushare_Python:tushare获取A股指数数据,使用LSTM进行预测,pythontushare,并...
  19. Hyperledger Fabric网络节点架构
  20. css弹性盒模型详解----flex-direction

热门文章

  1. jQuery插件管理方案
  2. 主流手机user-agent与支持图像尺寸对照表(联通的)
  3. 算法导论笔记 第三十章 多项式与快速傅里叶变化
  4. 自己整理出来的一些标签
  5. 分立元件封装尺寸及PCB板材工艺与设计实例
  6. 今日收获:图片数据的存和取示例
  7. System.DateTime.Now.ToString
  8. [Remoting]在.NET環境實作Flex 3 Remoting - (2) Flex Builder 環境設定
  9. 宾大最新《图神经网络》课程,附视频与课件
  10. 【一分钟论文】IJCAI2019 | Self-attentive Biaffine Dependency Parsing