Python 是否存在重边阶跃函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15121048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does a heaviside step function exist?
提问by 8765674
采纳答案by Warren Weckesser
If you are using numpy version 1.13.0 or later, you can use numpy.heaviside:
如果您使用的是 numpy 1.13.0 或更高版本,则可以使用numpy.heaviside:
In [61]: x
Out[61]: array([-2. , -1.5, -1. , -0.5, 0. , 0.5, 1. , 1.5, 2. ])
In [62]: np.heaviside(x, 0.5)
Out[62]: array([ 0. , 0. , 0. , 0. , 0.5, 1. , 1. , 1. , 1. ])
With older versions of numpy you can implement it as 0.5 * (numpy.sign(x) + 1)
使用旧版本的 numpy,您可以将其实现为 0.5 * (numpy.sign(x) + 1)
In [65]: 0.5 * (numpy.sign(x) + 1)
Out[65]: array([ 0. , 0. , 0. , 0. , 0.5, 1. , 1. , 1. , 1. ])
回答by netcoder
I'm not sure if it's there out-of-the-box, but you can always write one:
我不确定它是否是开箱即用的,但你总是可以写一个:
def heaviside(x):
if x == 0:
return 0.5
return 0 if x < 0 else 1
回答by munk
It's part of sympy, which you can install with pip install sympy
它是sympy的一部分,您可以使用它进行安装pip install sympy
From the docs:
从文档:
class sympy.functions.special.delta_functions.Heaviside
Heaviside Piecewise function. Heaviside function has the following properties:
1) diff(Heaviside(x),x) = DiracDelta(x) ( 0, if x<0 )
2) Heaviside(x) = < [*] 1/2 if x==0 ( 1, if x>0 )
You would use it like this:
你会像这样使用它:
In [1]: from sympy.functions.special.delta_functions import Heaviside
In [2]: Heaviside(1)
Out[2]: 1
In [3]: Heaviside(0)
Out[3]: 1/2
In [4]: Heaviside(-1)
Out[4]: 0
You could also write your own:
你也可以自己写:
heaviside = lambda x: 0.5 if x == 0 else 0 if x < 0 else 1
Although that may not meet your needs if you require a symbolic variable.
尽管如果您需要符号变量,这可能无法满足您的需求。
回答by drewski
Not sure if the best way getting things done... but here is function that I hacked up.
不确定是否是完成任务的最佳方式……但这是我修改的功能。
def u(t):
unit_step = numpy.arange(t.shape[0])
lcv = numpy.arange(t.shape[0])
for place in lcv:
if t[place] == 0:
unit_step[place] = .5
elif t[place] > 0:
unit_step[place] = 1
elif t[place] < 0:
unit_step[place] = 0
return unit_step


回答by clwainwright
Probably the simplest method is just
可能最简单的方法就是
def step(x):
return 1 * (x > 0)
This works for both single numbers and numpy arrays, returns integers, and is zero for x = 0. The last criteria may be preferable over step(0) => 0.5in certain circumstances.
这适用于单个数字和 numpy 数组,返回整数,对于 x = 0 为零step(0) => 0.5。在某些情况下,最后一个标准可能更可取。
回答by Paul Price
def heaviside(xx):
return numpy.where(xx <= 0, 0.0, 1.0) + numpy.where(xx == 0.0, 0.5, 0.0)
Or, if numpy.whereis too slow:
或者,如果numpy.where太慢:
def heaviside(xx):
yy = numpy.ones_like(xx)
yy[xx < 0.0] = 0.0
yy[xx == 0.0] = 0.5
return yy
The following timings are with numpy 1.8.2; some optimisations were made in numpy 1.9.0, so try this yourself:
以下时间使用 numpy 1.8.2;在 numpy 1.9.0 中进行了一些优化,因此请自己尝试:
>>> import timeit
>>> import numpy
>>> array = numpy.arange(10) - 5
>>> def one():
... return numpy.where(array <= 0, 0.0, 1.0) + numpy.where(array == 0.0, 0.5, 0.0)
...
>>> def two():
... yy = numpy.ones_like(array)
... yy[array < 0] = 0.0
... yy[array == 0] = 0.5
... return yy
...
>>> timeit.timeit(one, number=100000)
3.026144027709961
>>> timeit.timeit(two, number=100000)
1.5265140533447266
>>> numpy.__version__
'1.8.2'
On a different machine, with a different numpy:
在不同的机器上,使用不同的 numpy:
>>> timeit.timeit(one, number=100000)
0.5119631290435791
>>> timeit.timeit(two, number=100000)
0.5458788871765137
>>> numpy.__version__
'1.11.1'
>>> def three():
... return 0.5*(numpy.sign(array) + 1)
...
>>> timeit.timeit(three, number=100000)
0.313539981842041
回答by Neil G
As of numpy 1.13, it is numpy.heaviside.
从 numpy 1.13 开始,它是numpy.heaviside.
回答by Rajan saha Raju
Easy Solution:
简单的解决方案:
import numpy as np
amplitudes = np.array([1*(x >= 0) for x in range(-5,6)])

