我收到语法错误:嵌套括号太多。
File /home/tilen/genetic_programming/.venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3577 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[4], line 80
pop, log = eaSimple(pop, toolbox, 0.5, 0.2, 100000, stats=stats, halloffame=hof, verbose=True)
File /home/tilen/genetic_programming/.venv/lib/python3.10/site-packages/deap/algorithms.py:173 in eaSimple
for ind, fit in zip(invalid_ind, fitnesses):
Cell In[4], line 52
func = toolbox.compile(expr=individual)
File /home/tilen/genetic_programming/.venv/lib/python3.10/site-packages/deap/gp.py:494 in compile
return eval(code, pset.context, {})
File <string>:1
lambda Lag1,Lag2,Lag3: multiply(protectedSqrt(multiply(Lag3, Lag2)), sin(sin(subtract(subtract(subtract(sin(subtract(multiply(sin(sin(
## Preprocessing:
def preprocessing(df):
df['Return'] = df['close'].pct_change().fillna(0)
df['Lag1'] = df['Return'].shift(1).fillna(0)
df['Lag2'] = df['Return'].shift(2).fillna(0)
df['Lag3'] = df['Return'].shift(3).fillna(0)
df['Target'] = df['Return'].shift(-1).fillna(0)
X = df[features]
y = df['Target']
return df, X, y
features = ['Lag1', 'Lag2', 'Lag3']
btc_data, X, y = preprocessing(df)
#print(btc_data)
print(X)
print(y)
## gp
def protectedDiv(left, right):
if right == 0:
return 1
try:
return np.divide(left, right + 2.226e-308)
except ZeroDivisionError:
return 1
def protectedLog(x):
if x <= 0:
return 1
return np.log(x)
def protectedSqrt(x):
if x < 0:
return 0
return np.sqrt(x)
def protectedExp(x):
if x > np.log(np.finfo(np.float32).max):
return np.finfo(np.float32).max
return np.exp(x)
gp.staticLimit(operator.attrgetter('height'), 50)
pset = gp.PrimitiveSet("MAIN", len(features))
pset.addPrimitive(np.add, 2)
pset.addPrimitive(np.subtract, 2)
pset.addPrimitive(np.multiply, 2)
pset.addPrimitive(protectedDiv, 2)
pset.addPrimitive(np.sin, 1)
pset.addPrimitive(np.cos, 1)
#pset.addPrimitive(np.tan, 1)
pset.addPrimitive(np.negative, 1)
pset.addPrimitive(protectedLog, 1)
pset.addPrimitive(protectedExp, 1)
pset.addPrimitive(protectedSqrt, 1)
pset.addEphemeralConstant("rand101", partial(random.uniform, -1, 1))
pset.renameArguments(ARG0='Lag1', ARG1='Lag2', ARG2='Lag3')
if 'FitnessMin' in creator.__dict__:
del creator.FitnessMin
if 'Individual' in creator.__dict__:
del creator.Individual
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
def evalSymbReg(individual, X, y):
func = toolbox.compile(expr=individual)
predictions = np.array([func(*x) for x in X], dtype=np.float128)
return mean_squared_error(y, predictions),
toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=2)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
toolbox.register("compile", gp.compile, pset=pset)
toolbox.register("evaluate", evalSymbReg, X=X.values, y=y.values)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr, pset=pset)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("map", map)
pop = toolbox.population(n=100)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)
pop, log = eaSimple(pop, toolbox, 0.5, 0.2, 100, stats=stats, halloffame=hof, verbose=True)
print(hof[0])
我想我用静态限制限制了gp:gp.staticLimit(operator.attrgetter('height'), 50)。我认为90以上就是极限了。
自 3.8 版本以来,CPython 中嵌套括号的最大数量为 200。
除非重新编译 Python,否则无法更改该限制 - 您需要弄清楚如何使该库不生成使用超过 200 个嵌套括号的代码。