轮盘赌轮选择:不支持的操作数类型&:“numpy”。float64’和‘numpy.float64”

时间:2021-09-23 01:31:53

I'm building parent selection of genetic algorithm with roulette wheel in python.

我正在构建python中使用轮盘赌轮的遗传算法的父选择。

acc['left'] is the left boundary and acc['right'] is the right boundary of probability cumulative for each individu. rw is random numbers of roulette wheel, while n_rw is the the number of rw.

acc['left']是左边界,acc['right']是每个个体累积概率的右边界。rw是轮盘赌轮的随机数,n_rw是rw的个数。

This is the acc dataframe:

这是acc dataframe:

    accuracy    rank    prob_fitness    left        right
0   0.825152    6.0     0.109091        0.000000    0.109091
1   0.839545    9.0     0.163636        0.109091    0.272727
2   0.807727    2.5     0.045455        0.272727    0.318182
3   0.840000    10.0    0.181818        0.318182    0.500000
4   0.807727    2.5     0.045455        0.500000    0.545455
5   0.820152    4.0     0.072727        0.545455    0.618182
6   0.832576    8.0     0.145455        0.618182    0.763636
7   0.821364    5.0     0.090909        0.763636    0.854545
8   0.802727    1.0     0.018182        0.854545    0.872727
9   0.829091    7.0     0.127273        0.872727    1.000000

And this is the rw:

这是rw:

'array([ 0.89676,  0.8007 ,  0.35212,  0.08043,  0.51044,  0.61213,  0.3392 ,  0.96687,  0.2554 ,  0.97215])'

I'm trying to determine which one will be the parent candidate with this code. But it does't work.

我正在尝试确定哪一个将是这个代码的父候选。但它不工作。

acc['parent'] = np.zeros(pop_size)
o = 0
b = 0
while o < pop_size:
    o = o+1
    while b < n_rw:
        acc['parent'] = (rw[b] > acc['left'][o] & rw[b] <= acc['right'][o])
        if acc['parent'] == True:
            b = n_rw
        else:
            b = b+1
acc

It results in:

结果:

TypeError: unsupported operand type(s) for &: 'numpy.float64' and 'numpy.float64'

类型错误:不支持&:numpy的操作数类型。float64’和‘numpy.float64”

Can you help me please? Thanks in advance

你能帮我一下吗?谢谢提前

1 个解决方案

#1


1  

In your case, something like this should do the job:

在你的情况下,像这样的东西应该是有用的:

acc['parent'] = np.full(pop_size, False, dtype=bool)
o = 0

while o < pop_size:
    b = 0
    while b < n_rw:
        acc.loc[o,'parent'] = ((rw[b] > acc['left'][o]) & (rw[b] <= acc['right'][o]))

Now you are writing into the DataFrame and actually changing the values that were previously initiated.

现在,您正在写入DataFrame并实际更改先前发起的值。

        if acc.loc[o,'parent'] == True:
            break
        else:
            b = b+1
    o = o+1
print acc

However, I'm not sure what your code is doing.

但是,我不确定您的代码在做什么。

#1


1  

In your case, something like this should do the job:

在你的情况下,像这样的东西应该是有用的:

acc['parent'] = np.full(pop_size, False, dtype=bool)
o = 0

while o < pop_size:
    b = 0
    while b < n_rw:
        acc.loc[o,'parent'] = ((rw[b] > acc['left'][o]) & (rw[b] <= acc['right'][o]))

Now you are writing into the DataFrame and actually changing the values that were previously initiated.

现在,您正在写入DataFrame并实际更改先前发起的值。

        if acc.loc[o,'parent'] == True:
            break
        else:
            b = b+1
    o = o+1
print acc

However, I'm not sure what your code is doing.

但是,我不确定您的代码在做什么。