self.__init__(self.mutation_rate, self.iterations, self.pool_size)
is_array = False
try:
X = np.array(X)
is_array = True
except:
X = X
self.pool = np.random.randint(0,2,(self.pool_size, X.shape[1]))
for iteration in range(1,self.iterations+1):
s_t = time.time()
scores = list(); fitness = list();
for chromosome in self.pool:
chosen_idx = [idx for gene, idx in zip(chromosome, range(X.shape[1])) if gene==1]
if is_array==True:
adj_X = X[:,chosen_idx]
elif is_array==False:
adj_X = X.iloc[:,chosen_idx]
pca==False
if pca==True:
adj_X = PCA(n_components=np.where(np.cumsum(PCA(n_components=adj_X.shape[1]).fit(adj_X).explained_variance_ratio_)>0.99)[0][0]+1).fit_transform(adj_X)
if _type == 'regression':
if cv==True:
score = np.mean(cross_val_score(model, adj_X, y, scoring='r2', cv=self.kf))
else:
score = r2_score(y, model.fit(adj_X,y).predict(adj_X))
elif _type == 'classification':
if cv==True:
score = np.mean(cross_val_score(model, adj_X, y, scoring='f1', cv=self.kf))
else:
score = f1_score(y, model.fit(adj_X,y).predict(adj_X),average=None)
scores.append(score)
fitness = [x/sum(scores) for x in scores]
fitness, self.pool, scores = (list(t) for t in zip(*sorted(zip(fitness, [list(l) for l in self.pool], scores),reverse=True)))
Я получаю «ValueError: Истинное значение массива с более чем одним элементом неоднозначно. Используйте a.any () или a.all ()», и я не знаю почему.
Строка, которая вызывает это:
fitness, self.pool, scores = (list(t) for t in zip(*sorted(zip(fitness, [list(l) for l in self.pool], scores),reverse=True)))
Я подозреваю, что * закороченное сравнение логических массивов, пожалуйста, помогите с решением.
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/enio/ML/decision_tree_classification.py', wdir='C:/Users/enio/ML')
File "C:\Users\nicko\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\nicko\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/enio/ML/decision_tree_classification.py", line 135, in
fs.fita(DecisionTreeClassifier(criterion = 'entropy', random_state = 0), 'classification', X, y, cv=False, pca=True)
File "C:\Users\nicko\OneDrive - Technological Educational Institution of Athens\Thesis\human_movement_recognition\Project\ML\GeneticAlgorithm.py", line 98, in fita
fitness, self.pool, scores = (list(t) for t in zip(*sorted(zip(fitness, [list(l) for l in self.pool], scores),reverse=True)))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()