2017-10-16 8 views
-2

私は機械学習を勉強していますが、githubでこのコードを見つけましたが、正しく動作させるためにいくつかの問題があります。あなたのn_filhosn_varsタイプが整数ではないため、このエラーが出る正しくpythonでnumpy.zerosを使用する方法

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

多すぎるコードは[mcve]に減らしてください。 –

+0

ヒントのおかげで、今最小限に抑えようとしました –

答えて

0

笑物事がはるかに簡単になっていません。最初の変数のみを別々に実行し、配列を返します。

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

実行する前に一部のタイプを確認します。

0

np.random.randint(1,3+1, 1)は整数ではなく配列を返します。次元の仕様では、整数の組が必要です。代わりに、数値配列と整数のタプルがあります。

>>> np.random.randint(1,3+1, 1) 
array([2]) 
関連する問題