本文整理汇总了Python中mystic.math.measures.mean函数的典型用法代码示例。如果您正苦于以下问题:Python mean函数的具体用法?Python mean怎么用?Python mean使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mean函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_solve_constraint
def test_solve_constraint():
constraints = """
spread([x0,x1]) - 1.0 = mean([x0,x1])
mean([x0,x1,x2]) = x2"""
from mystic.math.measures import mean, spread
_constraints = solve(constraints)
solv = generate_solvers(_constraints)
constraint = generate_constraint(solv)
x = constraint([1.0, 2.0, 3.0])
assert all(x) == all([1.0, 5.0, 3.0])
assert mean(x) == x[2]
assert spread(x[:-1]) - 1.0 == mean(x[:-1])
示例2: test_solve_constraint
def test_solve_constraint():
# sympy can no longer do "spread([x0,x1])"... so use "x1 - x0"
constraints = """
(x1 - x0) - 1.0 = mean([x0,x1])
mean([x0,x1,x2]) = x2"""
from mystic.math.measures import mean
_constraints = solve(constraints)
solv = generate_solvers(_constraints)
constraint = generate_constraint(solv)
x = constraint([1.0, 2.0, 3.0])
assert all(x) == all([1.0, 5.0, 3.0])
assert mean(x) == x[2]
assert (x[1] - x[0]) - 1.0 == mean(x[:-1])
示例3: test_penalize
def test_penalize():
from mystic.math.measures import mean, spread
def mean_constraint(x, target):
return mean(x) - target
def range_constraint(x, target):
return spread(x) - target
@quadratic_equality(condition=range_constraint, kwds={'target':5.0})
@quadratic_equality(condition=mean_constraint, kwds={'target':5.0})
def penalty(x):
return 0.0
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin
from numpy import array
x = array([1,2,3,4,5])
y = fmin(cost, x, penalty=penalty, disp=False)
assert round(mean(y)) == 5.0
assert round(spread(y)) == 5.0
assert round(cost(y)) == 4*(5.0)
示例4: test_constrain
def test_constrain():
from mystic.math.measures import mean, spread
from mystic.math.measures import impose_mean, impose_spread
def mean_constraint(x, mean=0.0):
return impose_mean(mean, x)
def range_constraint(x, spread=1.0):
return impose_spread(spread, x)
@inner(inner=range_constraint, kwds={'spread':5.0})
@inner(inner=mean_constraint, kwds={'mean':5.0})
def constraints(x):
return x
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin_powell
from numpy import array
x = array([1,2,3,4,5])
y = fmin_powell(cost, x, constraints=constraints, disp=False)
assert mean(y) == 5.0
assert spread(y) == 5.0
assert almostEqual(cost(y), 4*(5.0))
示例5: constraints
def constraints(x):
# constrain the last x_i to be the same value as the first x_i
x[-1] = x[0]
# constrain x such that mean(x) == target
if not almostEqual(mean(x), target):
x = impose_mean(target, x)
return x
示例6: test_solve_constraint
def test_solve_constraint():
from mystic.math.measures import mean
@with_mean(1.0)
def constraint(x):
x[-1] = x[0]
return x
x = solve(constraint, guess=[2,3,1])
assert almostEqual(mean(x), 1.0, tol=1e-15)
assert x[-1] == x[0]
assert issolution(constraint, x)
示例7: test_generate_constraint
def test_generate_constraint():
constraints = """
spread([x0, x1, x2]) = 10.0
mean([x0, x1, x2]) = 5.0"""
from mystic.math.measures import mean, spread
solv = generate_solvers(constraints)
assert almostEqual(mean(solv[0]([1,2,3])), 5.0)
assert almostEqual(spread(solv[1]([1,2,3])), 10.0)
constraint = generate_constraint(solv)
assert almostEqual(constraint([1,2,3]), [0.0,5.0,10.0], 1e-10)
示例8: test_with_constraint
def test_with_constraint():
from mystic.math.measures import mean, impose_mean
@with_constraint(inner, kwds={'target':5.0})
def mean_of_squared(x, target):
return impose_mean(target, [i**2 for i in x])
from numpy import array
x = array([1,2,3,4,5])
y = impose_mean(5, [i**2 for i in x])
assert mean(y) == 5.0
assert mean_of_squared(x) == y
示例9: test_simplify
def test_simplify():
constraints = """
mean([x0, x1, x2]) <= 5.0
x0 <= x1 + x2"""
from mystic.math.measures import mean
_constraints = simplify(constraints)
solv = generate_solvers(_constraints)
constraint = generate_constraint(solv)
x = constraint([1.0, -2.0, -3.0])
assert all(x) == all([-5.0, -2.0, -3.0])
assert mean(x) <= 5.0
assert x[0] <= x[1] + x[2]
示例10: test_with_mean
def test_with_mean():
from mystic.math.measures import mean, impose_mean
@with_mean(5.0)
def mean_of_squared(x):
return [i**2 for i in x]
from numpy import array
x = array([1,2,3,4,5])
y = impose_mean(5, [i**2 for i in x])
assert mean(y) == 5.0
assert mean_of_squared(x) == y
示例11: test_with_mean_spread
def test_with_mean_spread():
from mystic.math.measures import mean, spread, impose_mean, impose_spread
@with_spread(50.0)
@with_mean(5.0)
def constrained_squared(x):
return [i**2 for i in x]
from numpy import array
x = array([1,2,3,4,5])
y = impose_spread(50.0, impose_mean(5.0,[i**2 for i in x]))
assert almostEqual(mean(y), 5.0, tol=1e-15)
assert almostEqual(spread(y), 50.0, tol=1e-15)
assert constrained_squared(x) == y
示例12: test_as_constraint
def test_as_constraint():
from mystic.math.measures import mean, spread
def mean_constraint(x, target):
return mean(x) - target
def range_constraint(x, target):
return spread(x) - target
@quadratic_equality(condition=range_constraint, kwds={'target':5.0})
@quadratic_equality(condition=mean_constraint, kwds={'target':5.0})
def penalty(x):
return 0.0
ndim = 3
constraints = as_constraint(penalty, solver='fmin')
#XXX: this is expensive to evaluate, as there are nested optimizations
from numpy import arange
x = arange(ndim)
_x = constraints(x)
assert round(mean(_x)) == 5.0
assert round(spread(_x)) == 5.0
assert round(penalty(_x)) == 0.0
def cost(x):
return abs(sum(x) - 5.0)
npop = ndim*3
from mystic.solvers import diffev
y = diffev(cost, x, npop, constraints=constraints, disp=False, gtol=10)
assert round(mean(y)) == 5.0
assert round(spread(y)) == 5.0
assert round(cost(y)) == 5.0*(ndim-1)
示例13: test_outer_constraint
def test_outer_constraint():
from mystic.math.measures import impose_mean, mean
def impose_constraints(x, mean, weights=None):
return impose_mean(mean, x, weights)
@outer(outer=impose_constraints, kwds={'mean':5.0})
def mean_of_squared(x):
return [i**2 for i in x]
from numpy import array
x = array([1,2,3,4,5])
y = impose_mean(5, [i**2 for i in x])
assert mean(y) == 5.0
assert mean_of_squared(x) == y
示例14: test_with_penalty
def test_with_penalty():
from mystic.math.measures import mean, spread
@with_penalty(quadratic_equality, kwds={'target':5.0})
def penalty(x, target):
return mean(x) - target
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin
from numpy import array
x = array([1,2,3,4,5])
y = fmin(cost, x, penalty=penalty, disp=False)
assert round(mean(y)) == 5.0
assert round(cost(y)) == 4*(5.0)
示例15: constrain
def constrain(rv):
"constrain: y >= m and sum(wi)_{k} = 1 for each k in K"
pm = scenario()
pm.load(rv, pts) # here rv is param: w,x,y
#impose: sum(wi)_{k} = 1 for each k in K
norm = 1.0
for i in range(len(pm)):
w = pm[i].weights
w[-1] = norm - sum(w[:-1])
pm[i].weights = w
#impose: y >= m
values, weights = pm.values, pm.weights
y = float(mean(values, weights))
if not (y >= float(target[0])):
pm.values = impose_mean(target[0]+target[1], values, weights)
rv = pm.flatten(all=True)
return rv