/
veta_el
/
brain_language_patterns
Обзор
Документация
Войти
/
veta_el
/
brain_language_patterns
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
data_features_selection.py
145 строк
6 KB
veta_el
upload files
18 апр 2025, 08:13
18 апр 2025, 08:13
9a1b94b
Код
Авторство
О чём код?
import pandas as pd import random from data_prediction import pred class result_gen: def __init__(self, features, prec): self.features = features self.prec = prec def genetic_search (train: pd.DataFrame, test: pd.DataFrame, cond: str, original_features: list, max_min: str, difference: float): def generate (): # Generate new combination result = [] for i in range (0, len (cols)): random.seed() num = random.randrange(2) result.append (str(num)) check=0 for i in range (0, len (result)): if result [i] == '1': check = 0 break else: check = 1 if check == 1: result [0] = '1' return ' '.join(result) def transform (combination, cols): # transform zero-one sequence into features' names zero_one = combination.split(' ') to_transf = [] for i in range (0, len (cols)): if zero_one [i] == '1': to_transf.append (cols [i]) return to_transf def child_combination (combination1, combination2): # Generate children combinations parent1 = combination1.split(' ') parent2 = combination2.split(' ') first_combination = [] second_combination = [] for i in range (0, len (parent1)): random.seed() num = random.randrange(2) if num == 1: first_combination.append (parent1 [i]) second_combination.append (parent2 [i]) else: first_combination.append (parent2 [i]) second_combination.append (parent1 [i]) random.seed() num = random.randrange(len (first_combination)) if first_combination [num] == '0': first_combination [num] = '1' else: first_combination [num] = '0' random.seed() num=random.randrange(len (second_combination)) if second_combination [num] == '0': second_combination [num] = '1' else: second_combination [num] = '0' return [' '.join (first_combination), ' '.join (second_combination)] cols = original_features beings = [] children = [] for i in range (0, 20): # Create first generation combination = generate() features = transform (combination, cols) beings.append (result_gen (combination, pred (test, train, cond, features))) # Cond - model if max_min == 'max': # Select the best example = max(element.prec for element in beings) else: example = min(element.prec for element in beings) best_being = 0 for i in range (0, len (beings)): if beings [i].prec == example: best_being = beings [i] break stable = [] the_best = best_being while len(stable)<10: # Repeat generations untill the result is stable for i in range (0, int (len (beings)/10)): # Remove the worst if max_min == 'max': the_worst = min(element.prec for element in beings) else: the_worst = max(element.prec for element in beings) stop=0 while stop != len (beings): if beings [stop].prec == the_worst: beings.remove (beings [stop]) break else: stop = stop+1 if (len (beings)%2) != 0: if max_min == 'max': the_worst = min(element.prec for element in beings) else: the_worst = max(element.prec for element in beings) for j in range (0, len (beings)): if beings [j].prec == the_worst: beings.remove (beings [j]) break children = [] for i in range (0, len (beings), 2): # Generate new children combinations two_combinations = child_combination (beings [i].features, beings [i+1].features) features1 = transform (two_combinations [0], cols) features2 = transform (two_combinations [1], cols) children.append (result_gen (two_combinations [0], pred (test, train, cond, features))) children.append (result_gen (two_combinations [1], pred (test, train, cond, features))) beings.clear () beings = children.copy () if max_min == 'max': curr_example = max(element.prec for element in beings) else: curr_example = min(element.prec for element in beings) diff = abs(example-curr_example) # Find difference to check if stable if diff<difference: stable.append (1) example = curr_example for i in range (0, len (beings)): if beings [i].prec == example: best_being = beings [i] else: stable.clear () example = curr_example for i in range (0, len (beings)): if beings [i].prec == example: best_being = beings [i] break if max_min == 'max': if best_being.prec>the_best.prec: the_best = best_being else: if best_being.prec<the_best.prec: the_best = best_being best_combination = str(transform (the_best.features, cols)) # Find the best combination return [best_combination, the_best.prec]