简体   繁体   English

Numpy.piecewise 条件/函数数量作为变量

[英]Numpy.piecewise with number of conditions/functions as variable

n = len(x_coords) conditions = [x_coords[i] <= x <= x_coords[i+1] for i in range(n-1)] functions = [f(x,i) for i in range(n-1)] def g(x): return np.piecewise(x, conditions, functions)

'x_coords' is just a list of x_coordinates between which I define different functions 'x_coords' 只是一个 x_coordinates 列表,我在它们之间定义了不同的函数

When I run this code I get the error “'<=' not supported between instances of 'float' and 'list' ”当我运行此代码时,我收到错误“'float' 和 'list' 的实例之间不支持'<='”

I think I know what the problem is - that the elements in the lists I create are statements rather than values.我想我知道问题出在哪里——我创建的列表中的元素是语句而不是值。 However, I don't know how to get around this as I need a list of condition statements and functions for the但是,我不知道如何解决这个问题,因为我需要一个条件语句和函数列表

np.piecewise

function parameters function参数

Just use np.arrays instead of a list as this operation is not defined for built-in list.只需使用 np.arrays 而不是列表,因为此操作未为内置列表定义。 do:做:

 x_coords = np.array(x_coords)

You also may need to split the conditions:您可能还需要拆分条件:

 conditions = [ (x_coords[i] <= x) & (x <= x_coords[i+1]) for i in range(n-1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM