遺伝的アルゴリズムで特徴量を生成しようと試みています。
直接の問題点としてinvalid_indがslice(None,None,None)になっているのかどうか、またなっているとしたら何が問題なのか理解できていません。
最終的には遺伝的アルゴリズムの適用に対してどこが間違っているかを理解したいです。
参考にしたサイトはこちらです
遺伝的プログラミングによる特徴量生成
# 進化の実行 # 交叉確率50%、突然変異確率10%、10世代まで進化 start_time = time.time() pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 10, stats=mstats, halloffame=hof, verbose=True) end_time = time.time()
上のコマンドを実行すると下記のようなエラーが出ます
TypeError Traceback (most recent call last) <ipython-input-45-6e36a1a1e9f9> in <module> 2 # 交叉確率50%、突然変異確率10%、10世代まで進化 3 #start_time = time.time() ----> 4 pop, log = algorithms.eaSimple(pop, toolbox, 0.5, 0.1, 10, stats=mstats, halloffame=hof, verbose=True) 5 #end_time = time.time() /anaconda3/lib/python3.7/site-packages/deap/algorithms.py in eaSimple(population, toolbox, cxpb, mutpb, ngen, stats, halloffame, verbose) 149 invalid_ind = [ind for ind in population if not ind.fitness.valid] 150 fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) --> 151 for ind, fit in zip(invalid_ind, fitnesses): 152 ind.fitness.values = fit 153 <ipython-input-17-6b7270f13b1a> in eval_genfeat(individual) 3 def eval_genfeat(individual): 4 func = toolbox.compile(expr=individual) ----> 5 features_train = [X_train[:,i] for i in range(n_features)] 6 new_feat_train = func(*features_train) 7 X_train_tmp = np.c_[X_train, new_feat_train] <ipython-input-17-6b7270f13b1a> in <listcomp>(.0) 3 def eval_genfeat(individual): 4 func = toolbox.compile(expr=individual) ----> 5 features_train = [X_train[:,i] for i in range(n_features)] 6 new_feat_train = func(*features_train) 7 X_train_tmp = np.c_[X_train, new_feat_train] /anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key) 2925 if self.columns.nlevels > 1: 2926 return self._getitem_multilevel(key) -> 2927 indexer = self.columns.get_loc(key) 2928 if is_integer(indexer): 2929 indexer = [indexer] /anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2655 'backfill or nearest lookups') 2656 try: -> 2657 return self._engine.get_loc(key) 2658 except KeyError: 2659 return self._engine.get_loc(self._maybe_cast_indexer(key)) pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() TypeError: '(slice(None, None, None), 0)' is an invalid key
invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit
ここの3行目の invalid_indがslice(None,None,None)になっているために起こっていると思います。
invalid_indを出力したところ
[[<deap.gp.Primitive at 0x102b261d8>, <deap.gp.Primitive at 0x102b265e8>, <deap.gp.Primitive at 0x102b261d8>, <deap.gp.Terminal at 0x1a186c29d8>, <deap.gp.Primitive at 0x1a186b9f98>, <deap.gp.Terminal at 0x1a186d6048>, <deap.gp.Terminal at 0x1a186c2d80>], [<deap.gp.Primitive at 0x102b265e8>, ・ ・ ・
となっています。
またtype(invalid_ind)はlistでした。
長くなりましたがよろしくお願い致します。
あなたの回答
tips
プレビュー