<jupyter_start><jupyter_text>1. Extract your dataset and split into train_x, train_y, test_x and test_y.
2. Execute the following cells --- Hybrid Social Group Optimization ---<jupyter_code>N = 5 # Number of persons in population
D = len(train_x.columns) # Number of features in dataset
g = 10 # Number of generations
c = 0.6 # Self Introspection factor
r0 = 1
r1 = 0.4
r2 = 0.6
print(r1, r2)<jupyter_output>0.4 0.6
time: 3.13 ms (started: 2021-01-22 18:19:07 +00:00)<jupyter_text>**Population Initialization**<jupyter_code>population = np.random.choice([0,1,2,3,4,5,6,7,8,9], (N,D), p=[0.16, 0.16, 0.16, 0.16, 0.16, 0.04, 0.04, 0.04, 0.04, 0.04]) #Determines no. of features selected by probablity
population = population.astype(float)
print(population.shape)
population
fitness = np.zeros(N)
test_x.shape
def fitter_trait(X_old, X_new):
  if X_new > X_old:
    return X_old
  else:
    return X_new<jupyter_output>time: 2.58 ms (started: 2021-01-22 18:19:07 +00:00)<jupyter_text>**Fitness Function**<jupyter_code>global classifier
classifier = Svc #Change classifier here
select = train_x.columns
selectno = len(train_x.columns)
classifier.fit(train_x, train_y)
select_acc = classifier.score(test_x, test_y) 
def fitness_function(pop):  #Fitness Function
  
  for i in range(N):
    new_train_x = train_x
    
    new_test_x = test_x
    
    global select
    global selectno
    global select_acc
    
    new_train_x = new_train_x.drop(train_x.columns[pop[i] < 4], axis = 1)
        
    new_test_x = new_test_x.drop(test_x.columns[pop[i] < 4], axis = 1)
        
    classifier.fit(new_train_x, train_y)  
    fitness[i] = classifier.score(new_test_x, test_y) 
    if (fitness[i] > select_acc):
      select = new_train_x.columns
      # print(select.shape)
      selectno = new_train_x.shape[1]
      select_acc = fitness[i]
    elif fitness[i] == select_acc and new_train_x.shape[1] < selectno:
      select = new_train_x.columns
      selectno = new_train_x.shape[1]
      
    print("\nPerson "+ str(i+1))
    
    print("No. of Features Used = "+ str(new_train_x.shape[1])+ "/"+str(D)+"\nFitness = " + str(fitness[i]))
    print("Feature Used = ", end = " ") 
    
    #print(new_train_x.columns)

    print(new_train_x.shape[1])
    
# Initializing Fitness values of population
# fitness_function(population) 
# selectno<jupyter_output>time: 785 µs (started: 2021-01-22 18:19:13 +00:00)<jupyter_text>**Gbest : Fittest person in population**<jupyter_code>###Determining GBest
gbest = 0
gbest_i = 0
def find_gbest():
  gbest = max(fitness)#This can be any function
  gbest_i = fitness.argmax()
  print("Best fitness value for the generation = "+str(gbest) + " Person " + str(gbest_i+1)+"\n")
find_gbest() 
#we chose maximum fitness value to be better for simplicity
def cal_fitness(person):
  new_train_x = train_x
  
  new_test_x = test_x

  new_train_x = new_train_x.drop(train_x.columns[person < 4], axis = 1)
      
  new_test_x = new_test_x.drop(test_x.columns[person < 4], axis = 1)
      
  classifier.fit(new_train_x, train_y)  
  return classifier.score(new_test_x, test_y)

cal_fitness(population[0])
# new_train_x = train_x
  
# new_test_x = test_x

# new_train_x = new_train_x.drop(train_x.columns[person < 4], axis = 1)
      
# new_test_x = new_test_x.drop(test_x.columns[person < 4], axis = 1)
per1 = np.zeros((1,10000))
print(per1.shape)
per1[0][5] = 8
per1[0][89] = 7
per1[0][45] = 6
cal_fitness(per1[0])<jupyter_output>(1, 10000)<jupyter_text>---**Mutation Phase**<jupyter_code>def mutate():
  gworst_i = fitness.argmin()
  gworst = min(fitness)
  mut = np.random.randint(0,2,size=(1,D))[0]
  print("Mutating the Generation's Worst....Person "+ str(gworst_i+1))
  for i in range(D):
    if mut[i] > 0:
      mut[i] = population[gbest_i][i]
    else:
      mut[i] = population[gworst_i][i]
  if cal_fitness(mut) > gworst:
    population[gworst_i] = mut
    print("Person "+str(gworst_i)+" mutated")
  else:
    print("No Mutations in this generation")
mut = np.random.randint(0,2,size=(1,D))[0]
mut
div = pd.DataFrame(np.random.randint(0,2,size=(1,D))[0])
# div.iloc[:,div > 0] = population[2][div>0]
# div<jupyter_output>time: 1.79 ms (started: 2021-01-22 18:19:15 +00:00)<jupyter_text>---**Improving Phase**<jupyter_code>## Improving Phase
# i = 1
def improve():
  print("Improving.......")
  for i in range(N):
    Xnew = population[i]
    print('Persona '+ str(i+1))
    for j in range(D):
      Xnew[j] = c * population[i][j] + r0 * (population[gbest_i][j] - population[i][j])
    try:
      if cal_fitness(Xnew) > fitness[i]:
        population[i] = Xnew
    except:
      print("Oops!", sys.exc_info()[0], "occurred.")
      print("Next entry.")<jupyter_output><empty_output><jupyter_text>---**Acquiring Phase**<jupyter_code>## Acquiring Phase
def acquire():
  random_person = np.random.randint(low=0, high=N)
  for i in range(N):
    if random_person == i:
      random_person = np.random.randint(low=0, high=N)
      i = i - 1
      continue
    X_new = population[i]
    if fitness[random_person] > fitness[i]:
      for j in range(D):
        X_new[j] = population[i][j] + r1*(population[random_person][j]-population[i][j]) + r2*(population[gbest_i][j]-population[i][j])
      if cal_fitness(X_new) > fitness[i]:
        population[i] = X_new
    else:
      for j in range(D):
        X_new[j] = population[i][j] + r1*(population[i][j]-population[random_person][j]) + r2*(population[gbest_i][j]-population[i][j])
      if cal_fitness(X_new) > fitness[i]:
        population[i] = X_new
      

#Run
try:
  for k in range(g):
    print("Generation "+ str(k+1) + "\n---------------")
    fitness_function(population)
    find_gbest()
    mutate()
    improve()
    acquire() 
except:
  print()
  print("........................")
  print("Optimal Solution Reached")
  print("........................")
select.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16239171054866655, 'fraction_numerical': 0.038389672158994394, 'mean_word_length': 3.7104, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '2886', 'license': 'Apache-2.0', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2286, 'n_words': 455, 'path': 'Hybrid_SGO.ipynb', 'repo_name': 'Enixes/Hybrid-Social-Group-Optimization-algorithm', 'size': '5887'}