np.power(x1,x2)

power(x1, x2, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True
[, signature, extobj])

First array elements raised to powers from second array, element-wise.

Raise each base in `x1` to the positionally-corresponding power in
`x2`.  `x1` and `x2` must be broadcastable to the same shape. Note that an
integer type raised to a negative integer power will raise a ValueError.

Parameters
----------
x1 : array_like
The bases.
x2 : array_like
The exponents.
out : ndarray, None, or tuple of ndarray and None, optional
A location into which the result is stored. If provided, it must have
a shape that the inputs broadcast to. If not provided or `None`,
a freshly-allocated array is returned. A tuple (possible only as a
keyword argument) must have length equal to the number of outputs.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
**kwargs
For other keyword-only arguments, see the
:ref:`ufunc docs <ufuncs.kwargs>`.

Returns
-------
y : ndarray
The bases in `x1` raised to the exponents in `x2`.

See Also
--------
float_power : power function that promotes integers to float

Examples
--------
Cube each element in a list.

>>> x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.power(x1, 3)
array([  0,   1,   8,  27,  64, 125])

Raise the bases to different exponents.

>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([  0.,   1.,   8.,  27.,  16.,   5.])

The effect of broadcasting.

>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
array([[1, 2, 3, 3, 2, 1],
[1, 2, 3, 3, 2, 1]])
>>> np.power(x1, x2)
array([[ 0,  1,  8, 27, 16,  5],
[ 0,  1,  8, 27, 16,  5]])
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请在不影响结果的条件下改变代码的样子:import numpy as np import matplotlib.pyplot as plt x1len = 21 x2len = 18 LEN = x1len + x2len POPULATION_SIZE = 100 GENERATIONS = 251 CROSSOVER_RATE = 0.7 MUTATION_RATE = 0.3 pop = np.random.randint(0,2,size=(POPULATION_SIZE,LEN)) def BinToX(pop): x1 = pop[:,0:x1len] x2 = pop[:,x1len:] x1 = x1.dot(2**np.arange(x1len)[::-1]) x2 = x2.dot(2**np.arange(x2len)[::-1]) x1 = -2.9 + x1*(12 + 2.9)/(np.power(2,x1len)-1) x2 = 4.2 + x2*(5.7 - 4.2)/(np.power(2,x2len)-1) return x1,x2 def func(pop): x1,x2 = BinToX(pop) return 21.5 + x1*np.sin(4*np.pi*x1) + x2*np.sin(20*np.pi*x2) def fn(pop): return func(pop); def selection(pop, fitness): idx = np.random.choice(np.arange(pop.shape[0]), size=POPULATION_SIZE, replace=True, p=fitness/fitness.sum()) return pop[idx] def crossover(IdxP1,pop): if np.random.rand() < CROSSOVER_RATE: C = np.zeros((1,LEN)) IdxP2 = np.random.randint(0, POPULATION_SIZE) pt = np.random.randint(0, LEN) C[0,:pt] = pop[IdxP1,:pt] C[0,pt:] = pop[IdxP2, pt:] np.append(pop, C, axis=0) return def mutation(idx,pop): if np.random.rand() < MUTATION_RATE: mut_index = np.random.randint(0, LEN) pop[idx,mut_index] = 1- pop[idx,mut_index] return best_chrom = np.zeros(LEN) best_score = 0 fig = plt.figure() for generation in range(GENERATIONS): fitness = fn(pop) pop = selection(pop, fitness) if generation%50 == 0: ax = fig.add_subplot(2,3,generation//50 +1, projection='3d', title = "generation:"+str(generation)+" best="+str(np.max(fitness))) x1,x2 = BinToX(pop) z = func(pop) ax.scatter(x1,x2,z) for idx in range(POPULATION_SIZE): crossover(idx,pop) mutation(idx,pop) idx = np.argmax(fitness) if best_score < fitness[idx]: best_score = fitness[idx] best_chrom = pop[idx, :] plt.show() print('最优解:', best_chrom, '| best score: %.2f' % best_score)
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值