首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制scipy.optimize.differential_evolution的收敛性结果

绘制scipy.optimize.differential_evolution的收敛性结果
EN

Stack Overflow用户
提问于 2020-09-13 13:13:07
回答 2查看 981关注 0票数 0

我有两个数据(df_1,df_2),一些变量(A,B,C),一个函数( fun )和一个全局的遗传优化器,它在给定的A、B、C的范围内找到乐趣的最大值。

代码语言:javascript
复制
from scipy.optimize import differential_evolution

df_1 = pd.DataFrame({'O' : [1,2,3], 'M' : [2,8,3]})

df_2 = pd.DataFrame({'O' : [1,1,1, 2,2,2, 3,3,3],
                     'M' : [9,2,4, 6,7,8, 5,3,4],
                     'X' : [2,4,6, 4,8,7, 3,1,9],
                     'Y' : [3,6,1, 4,6,5, 1,0,7],
                     'Z' : [2,4,8, 3,5,4, 7,5,1]})

# Index
df_1 = df_1.set_index('O')
df_1_M = df_1.M
df_1_M = df_1_M.sort_index()

# Fun
def fun(z, *params):
    A,B,C = z
        
    # Score
    df_2['S'] = df_2['X']*A + df_2['Y']*B + df_2['Z']*C
    
    # Top score
    df_Sort = df_2.sort_values(['S', 'X', 'M'], ascending=[False, True, True])
    df_O    = df_Sort.set_index('O')
    M_Top   = df_O[~df_O.index.duplicated(keep='first')].M
    M_Top   = M_Top.sort_index()
        
    # Compare the top scoring row for each O to df_1
    df_1_R = df_1_M.reindex(M_Top.index) # Nan
    T_N_T  = M_Top == df_1_R

    # Record the results for the given values of A,B,C
    df_Res = pd.DataFrame({'it_is':T_N_T}) # is this row of df_1 the same as this row of M_Top?
        
    # p_hat =         TP / (TP + FP)
    p_hat = df_Res.sum() / len(df_Res.index)
    
    print(z)
        
    return -p_hat[0]

# Bounds
min_ = 0
max_ = 1
ran_ge = (min_, max_)
bounds = [ran_ge,ran_ge,ran_ge]

# Params
params = (df_1, df_2)

# DE
DE = differential_evolution(fun, bounds, args=params)

它在每次迭代中打印A、B、C,例如,最后三行是:

代码语言:javascript
复制
[0.04003901 0.50504249 0.56332845]
[0.040039   0.5050425  0.56332845]
[0.040039   0.50504249 0.56332846]

要了解它是如何收敛的,请使用绘图 A、B、C来反对迭代。

我试着把A,B,C储存在:

代码语言:javascript
复制
df_P = pd.DataFrame({0})

在增加乐趣的同时:

代码语言:javascript
复制
df_P.append(z)

但我得到了:

代码语言:javascript
复制
RuntimeError: The map-like callable must be of the form f(func, iterable), returning a sequence of numbers the same length as 'iterable'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-13 13:50:23

所以我不确定是否找到了最好的方法,但我找到了一个。它借鉴了列表是通过参考传递的事实。这意味着,如果将列表传递给函数并对其进行修改,则即使该函数没有返回列表,也会在程序的其余部分对其进行修改。

代码语言:javascript
复制
# Params
results = []  # this list will hold our restuts
params = (df_1, df_2, results)  # add it to the params of the functions

# now in the function add the output to the list, Instead of the mean here I used the distance to the origin (as if you 3 value were a 3d vector) 

p_hat = df_Res.sum() / len(df_Res.index)

distance_to_zeros = sum([e**2 for e in z]) ** 1/2
results.append(distance_to_zeros)
# Indeed you can also append z directly.

# Then after DE call
DE = differential_evolution(fun, bounds, args=params)

x = range(0, len(results))

plt.scatter(x, results, alpha=0.5)
plt.show()
票数 2
EN

Stack Overflow用户

发布于 2020-09-13 14:56:37

使用RomainL的代码绘制A、B、C:

代码语言:javascript
复制
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import differential_evolution

df_1 = pd.DataFrame({'O' : [1,2,3], 'M' : [2,8,3]})

df_2 = pd.DataFrame({'O' : [1,1,1, 2,2,2, 3,3,3],
                     'M' : [9,2,4, 6,7,8, 5,3,4],
                     'X' : [2,4,6, 4,8,7, 3,1,9],
                     'Y' : [3,6,1, 4,6,5, 1,0,7],
                     'Z' : [2,4,8, 3,5,4, 7,5,1]})

# Index
df_1 = df_1.set_index('O')
df_1_M = df_1.M
df_1_M = df_1_M.sort_index()

# Fun
def fun(z, *params):
    A,B,C = z
        
    # Score
    df_2['S'] = df_2['X']*A + df_2['Y']*B + df_2['Z']*C
    
    # Top score
    df_Sort = df_2.sort_values(['S', 'X', 'M'], ascending=[False, True, True])
    df_O    = df_Sort.set_index('O')
    M_Top   = df_O[~df_O.index.duplicated(keep='first')].M
    M_Top   = M_Top.sort_index()
        
    # Compare the top scoring row for each O to df_1
    df_1_R = df_1_M.reindex(M_Top.index) # Nan
    T_N_T  = M_Top == df_1_R

    # Record the results for the given values of A,B,C
    df_Res = pd.DataFrame({'it_is':T_N_T}) # is this row of df_1 the same as this row of M_Top?
        
    # p_hat =         TP / (TP + FP)
    p_hat = df_Res.sum() / len(df_Res.index)
    
    results.append(z)
        
    return -p_hat[0]

# Bounds
min_ = 0
max_ = 1
ran_ge = (min_, max_)
bounds = [ran_ge,ran_ge,ran_ge]

# Params
results = []
params = (df_1, df_2, results)

# DE
DE = differential_evolution(fun, bounds, args=params)

# Plot
df_R = pd.DataFrame(list(map(np.ravel, results)), columns=('A','B','C'))
plt.figure()
ax = df_R.plot()
ax.set_ylim(min_,max_)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63871200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档