在对numpy的数组进行操作时,我们应该尽量避免循环操作,尽可能利用矢量化函数来避免循环.

  但是,直接将自定义函数应用在numpy数组之上会报错,我们需要将函数进行矢量化转换.

def Theta(x):
"""
Scalar implemenation of the Heaviside step function.
"""
if x >= 0:
return 1
else:
return 0
Theta(array([-3,-2,-1,0,1,2,3]))

  出错信息:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-6658efdd2f22> in <module>()
----> 1 Theta(array([-3,-2,-1,0,1,2,3])) <ipython-input-8-9a0cb13d93d4> in Theta(x)
3 Scalar implemenation of the Heaviside step function.
4 """
----> 5 if x >= 0:
6 return 1
7 else: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

  为了得到矢量的Theta,我们可以使用Numpy函数vectorize。在许多情况下它可以自动矢量化一个函数:

Theta_vec = vectorize(Theta)
Theta_vec(array([-3,-2,-1,0,1,2,3]))
array([0, 0, 0, 1, 1, 1, 1])

  我们也可以使用该函数从头来接受矢量输入(需要更多工作但是也表现更好):

def Theta(x):
"""
Vector-aware implemenation of the Heaviside step function.
"""
return 1 * (x >= 0)
Theta(array([-3,-2,-1,0,1,2,3]))
array([0, 0, 0, 1, 1, 1, 1])
# 对标量依然行得通
Theta(-1.2), Theta(2.6)
(0, 1)

python中numpy对函数进行矢量化转换的更多相关文章

  1. Python中numpy.apply_along_axis()函数的用法

    numpy.apply_along_axis(func, axis, arr, *args, **kwargs): 必选参数:func,axis,arr.其中func是我们自定义的一个函数,函数fun ...

  2. python 中numpy dot函数的使用方法

    这个函数在的数字信号处理中用处还是比较广泛的,函数的具体定义如下所示: numpy.dot(a, b, out=None) 该函数的作用是获取两个元素a,b的乘积,表示的含义如下所示: dot(a, ...

  3. python中numpy.sum()函数

    讲解清晰,转载自:https://blog.csdn.net/rifengxxc/article/details/75008427 众所周知,sum不传参的时候,是所有元素的总和.这里就不说了. 1 ...

  4. Python中Numpy.nonzero()函数

    Numpy.nonzero()返回的是数组中,非零元素的位置.如果是二维数组就是描述非零元素在几行几列,三维数组则是描述非零元素在第几组中的第几行第几列. 举例如下: 二维数组: a = np.arr ...

  5. python中numpy.concatenate()函数的使用

    numpy库数组拼接np.concatenate 原文:https://blog.csdn.net/zyl1042635242/article/details/43162031 思路:numpy提供了 ...

  6. python中numpy矩阵运算操作大全(非常全)!

    python中numpy矩阵运算操作大全(非常全) //2019.07.10晚python矩阵运算大全1.矩阵的输出形式:对于任何一个矩阵,python输出的模板是:import numpy as n ...

  7. Python中Numpy及Matplotlib使用

    Python中Numpy及Matplotlib使用 1. Jupyter Notebooks 作为小白,我现在使用的python编辑器是Jupyter Notebook,非常的好用,推荐!!! 你可以 ...

  8. python --- Python中的callable 函数

    python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...

  9. python中使用zip函数出现<zip object at 0x02A9E418>

    在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...

随机推荐

  1. protobuf与json互相转换

    Java http://code.google.com/p/protobuf-java-format/ maven <dependency> <groupId>com.goog ...

  2. Nginx配置-伪静态,隐藏index.php大入口

    server { listen ; server_name ; root E:/www/wvtuan/; index index.php index.html; log_not_found off; ...

  3. Logcat过滤及常见用法整理

    Usage: logcat [options] [filterspecs] options include:-s              Set default filter to silent.  ...

  4. jvm attach

    http://ayufox.iteye.com/blog/655761 管道通信

  5. javascript 用call来继承实例属性

    xxx.call(thisObj, arg1,...)的调用可以改变当前函数的执行环境为传入的thisObj对象.利用这一点可以实现继承————当前的对象获得XXX的属性和方法. 例子: functi ...

  6. HDU 1074 Doing Homework(状态压缩)

    之前做过一个题,是在学贪心的时候做的,所以这个题就想当然的跑偏了,当看到N是<=16 的时候,状态压缩就理所当然了 #include<iostream> #include<cs ...

  7. 使用Bootstrap建立网站微金所——头部

    1.微金所链接:http://www.weijinsuo.com/ 2.头部分为:topbar和nav上下两个部分 (1)topbar详解 topbar使用bootstrap的栅格系统 (2)nav分 ...

  8. hibernate--联合主键--annotation

    有3种方式: 1.@Embeddedable 2.@EmbeddedId 3. @IdClass 2,3 最常用 一, @Embeddedable 1.新建TeacherPK.java, 加入@Emb ...

  9. sersync 实时同步文件

    sersync 主要用于服务器同步,web镜像等功能.sersync是使用c++编写,在结合rsync同步的时候,节省了运行时耗和网络资源.因此更快.sersync配置起来很简单.另外本项目相比较其他 ...

  10. URL与String转换

    NSString *str = @"www.baidu.com"; NSURL *URL = [NSURL URLWithString:str];  //string>url ...