2014-05-21 2 views
0

내 코드는 유전 알고리즘에 있습니다. 내 문제는 : 나는 가장 좋은 개인, 최악과 평균을 돌려주고 싶다. 내가 목록의 색인으로하고 싶은 모든 것. 예 : 최상의 가치 = 높은 비율. 최악의 가치 = 가장 작은 지수. 나는 방법을 모른다. 이것은 내 코드입니다.파이썬 목록의 인덱스 최대, 최소값 및 평균값을 가져옵니다.

def selecciona_mejor_peor_medio(self, population): 
    list = [] 
    cont = 0 
    arff = self.arff_reader_class 
    attributes_list = [] 
    attributes_counter = [] 

    for element in list: 

     if element[len(element)-1] in arff.consequences: 
      temporal_rule = element 
      # We take the first attribute 

      for attribute in element: 
      # Seek other sets that start with the same item and group them into lists. 
      # Subsequently removed from the initial list to avoid repeating values 
       for rule_list in self.population_my_method: 
        for rule in rule_list: 
         cont_attribute = 0 
         for rule_attribute in rule: 
          if rule_attribute == attribute: 
           cont_attribute += 1 
         if cont_attribute == len(element): 
          cont += 1 
        attributes_counter.append(cont) 
    # Find the index of the highest value, medium and low 

       peor = attributes_counter.index(min(attributes_counter)) 

       mejor = attributes_counter.index(max(attributes_counter)) 

       index, value = max(enumerate(list), key=operator.itemgetter(1)) 

    # With the rates you get the values ​​at the population 



       if mejor > list[cont]: 
        return mejor 
       elif peor < list[cont]: 
        return peor 
       else: 

        return list[cont] 

이 값을 가져오고 반환하는 방법을 모르겠습니다. 당신이 나를 도울 수 있기를 바랍니다. 대단히 감사합니다.

+0

평균적인 부분에 대한 질문에 다음과 같은 문제가 있습니다. 목록에서 평균 색인이 어디에 있는지 알고 싶습니까? 그렇다면 경고 : 목록의 값에 따라 평균값이 존재하지 않을 수도 있습니다 (예 : 평균은 부동 소수점 수, 목록의 숫자는 정수일 수 있음). – jrd1

+0

어쩌면 나는 심하게 설명했다. 각 반복에서 달성하고자하는 목표는 다음과 같습니다. \t - 가장 적합한 개인의 평가. \t - 인구의 최악 개인에 대한 평가. \t- 인구의 평균 점수. \t \t이 잉여 라인 : \t 인덱스 값 = 최대 (열거 (리스트), 키 = operator.itemgetter (1)) – user3659527

+0

난 당신이 단지 기본 알고리즘을 쓸 필요가 있다고 생각합니다. 나는 당신이 최대, 최소, 또는 평균을 위해 원하는 데이터를 정확히 모르겠다. 그래서 나는 답을 쓰는 것을 주저한다. – Paul

답변

0

목록 (이 경우 채우기)을 전달하지 않고 메서드를 호출하기 때문에이 오류가 발생했습니다.