trevorstephens / gplearn

Genetic Programming in Python, with a scikit-learn inspired API
http://gplearn.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.56k stars 274 forks source link

Is there any way to get the formula expresssion of each individual? Thanks. #281

Open OneWingAngel opened 1 year ago

OneWingAngel commented 1 year ago

Greetings,

Thank you very much for taking time from your busy schedule. I'm currently working on this gplearn package, and I'm using _my_metric function to create my own customized fitness function. And now, my critical problem is, is there any way to know or get the formula expresssion of each individual? Is this formula hidden in some object or other functions in this package? I have already known that I can get some best fitness values in the end using a for loop, however, I want to get this formula in _my_metric function in order to check whether my customized time-series functions are correct.

Best wishes.

eiyawokao commented 1 year ago

I may not be able to answer your question, but I am wondering how you apply your own customized fitness function for each individual asset on cross-section not on time-series. In my programme, I can only generate factor expressions for one specific asset(like stock, future,etc.)

eiyawokao commented 1 year ago

I can generate the best 10 expressions with code below, hope this may help u best_programs = est_gp._best_programs best_programs_dict = {}

for p in best_programs: factorname = 'alpha' + str(best_programs.index(p) + 1) best_programs_dict[factorname] = {'fitness':p.fitness, 'expression':str(p), 'depth':p.depth, 'length':p.length}

best_programs_dict = pd.DataFrame(best_programs_dict).T best_programs_dict = best_programs_dict.sort_values(by='fitness') best_programs_dict

fitness expression  depth   length

alpha_10 0.156346 sin(cube(close)) 2 3 alpha_9 0.325947 mul(low, sin(money)) 2 4 alpha_7 0.383441 sub(add(ts_sum(_abs(min(close, money)), SEQUEN... 5 57 alpha_6 0.388041 sub(inv(ts_std(volume, 0.670)), div(min(open, ... 3 11 alpha_8 0.388041 sub(ts_lowday(min(close, high), ts_sum(low, hi... 3 11 alpha_5 0.404158 min(cube(sin(close)), corr(ts_scale(open, volu... 3 14 alpha_4 0.834534 sin(mul(open, high)) 2 4 alpha_3 6.596452 log(square(sin(low))) 3 4 alpha_2 6.71933 inv(sin(high)) 2 3 alpha_1 12.384383 log(sub(add(rank(inv(low)), ts_rank(div(close,... 5 27

trevorstephens commented 1 year ago

Hi @OneWingAngel this is not currently supported inside the metric.

llamm-de commented 10 months ago

Hi @trevorstephens. Having the possibility to access the expression for each individual would be a great feature for the custom metrics. It gives you much more flexibility and is especially helpful, if you have to work with derivatives etc. inside the metric. I would also appreciate this feature very much.