2017-10-16 1 views
-2

Ich studiere maschinelles Lernen und ich fand diesen Code in GitHub, aber ich habe einige Probleme, damit es richtig funktioniert, und ich habe auch keine Erfahrung mit Python, die ist nicht die Dinge viel einfacher hahahaWie benutze ich numpy.zeros in python

filhos = np.zeros((n_filhos, n_vars)) is returning this error:

Traceback (most recent call last): File "D:\GitHub\evoman_framework\optimization_individualevolution_demo.py", line 272, in filhos = cruzamento(pop) # crossover File "D:\GitHub\evoman_framework\optimization_individualevolution_demo.py", line 171, in cruzamento filhos = np.zeros((n_filhos, n_vars)) TypeError: only integer scalar arrays can be converted to a scalar index

############################################################################### 
# EvoMan FrameWork - V1.0 2016         # 
# DEMO : Neuroevolution - Genetic Algorithm with perceptron neural network. # 
# Author: Karine Miras            # 
# [email protected]           # 
############################################################################### 

# imports framework 
import sys 
sys.path.insert(0, 'evoman') 
from environment import Environment 
from controller import Controller 

# imports other libs 
import time 
import numpy as np 
from math import fabs,sqrt 
import glob, os 

# genetic algorithm params 

run_mode = 'train' # train or test 
stateread = None # 'state_1' 
statesave = 'state_1' 
n_vars = (env.get_num_sensors()+1)*5 # perceptron 
#n_vars = (env.get_num_sensors()+1)*10 + 11*5 # multilayer with 10 neurons 
#n_vars = (env.get_num_sensors()+1)*50 + 51*5 # multilayer with 50 neurons 
dom_u = 1 
dom_l = -1 
npop = 100 
gens = 30 
mutacao = 0.2 
last_best = 0 

# crossover 
def cruzamento(pop): 

    total_filhos = np.zeros((0,n_vars)) 


    for p in range(0,pop.shape[0], 2):  
     p1 = torneio(pop) 
     p2 = torneio(pop) 

     n_filhos = np.random.randint(1,3+1, 1) 
     filhos = np.zeros((n_filhos, n_vars)) 

     for f in range(0,n_filhos): 

      cross_prop = np.random.uniform(0,1) 
      filhos[f] = p1*cross_prop+p2*(1-cross_prop) 

      # mutation 
      for i in filhos[f]: 
       if np.random.uniform(0 ,1)<=mutacao: 
        filhos[f][i] = filhos[f][i]+np.random.normal(dom_l, dom_u) 

      filhos[f] = np.array(map(lambda y: limites(y), filhos[f]))   

      total_filhos = np.vstack((total_filhos, filhos[f])) 

    return total_filhos 
+3

Zu viel Code, bitte reduzieren Sie dies zu einem [MCVE]. –

+0

reduziert, danke für die Tipps, versuchen, jetzt minimal zu halten –

Antwort

0

Sie diesen Fehler erhalten, weil Ihr n_filhos oder n_vars Typ nicht eine ganze Zahl ist. Ich kann nur die erste Variable separat ausführen und es gibt das Array zurück.

>>> n_filhos = np.random.randint(1,3+1, 1) 
>>> n_filhos 
array([3]) 

Überprüfen Sie den Typ von ihnen teilweise vor dem Ausführen.

0

np.random.randint(1,3+1, 1) gibt ein Array zurück, keine Ganzzahl. Dimensionsspezifikation erwartet das Tupel von Ganzzahlen. Stattdessen gibt es ein Tupel von numpy Array und eine ganze Zahl:

>>> np.random.randint(1,3+1, 1) 
array([2])