-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitness.py
52 lines (44 loc) · 1.37 KB
/
fitness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
from loguru import logger
from scipy.stats import pearsonr, spearmanr
def fitness_ic(individual, toolbox, X_train, y_train):
"""
计算IC适应度
:param individual: 个体
:return: 适应度
"""
func = toolbox.compile(expr=individual)
ic = pearsonr(func(X_train), y_train, axis=1)[0]
return (np.nanmean(ic),)
def fitness_rankic(individual, toolbox, X_train, y_train):
"""
计算RankIC适应度
:param individual: 个体
:return: 适应度
"""
func = toolbox.compile(expr=individual)
try:
rankic = np.nanmean(
func(**X_train).shift(2).corrwith(y_train, method="spearman", axis=1)
)
except Exception:
logger.error(f"{individual}的RankIC计算出错。")
return (rankic,)
def fitness_icir(individual, toolbox, X_train, y_train):
"""
计算ICIR适应度
:param individual: 个体
:return: 适应度
"""
func = toolbox.compile(expr=individual)
ic = pearsonr(func(X_train), y_train, axis=1)[0]
return (np.nanmean(ic) / np.nanstd(ic),)
def fitness_rankicir(individual, toolbox, X_train, y_train):
"""
计算RankICIR适应度
:param individual: 个体
:return: 适应度
"""
func = toolbox.compile(expr=individual)
rankic = spearmanr(func(X_train), y_train, axis=1)[0]
return (np.nanmean(rankic) / np.nanstd(rankic),)