title: Python inquiry using a neural network
body: <p>I am trying to modify a code written by a software developer (Kyle Dickerson) and have written it up like this:</p>
  
  <p>So I have this code:</p>
  
  <pre><code>from __future__ import division
  
  ## Kyle Dickerson
  ## kyle.dickerson@gmail.com
  ## Jan 15, 2008
  ##
  ## Self-organizing map using scipy
  ## This code is licensed and released under the GNU GPL
  
  ## This code uses a square grid rather than hexagonal grid, as scipy    allows for fast square grid computation.
  ## I designed sompy for speed, so attempting to read the code may not be  very intuitive.
  ## If you're trying to learn how SOMs work, I would suggest starting with     Paras Chopras SOMPython code:
  ##  http://www.paraschopra.com/sourcecode/SOM/index.php
  ## It has a more intuitive structure for those unfamiliar  with scipy, however it is much slower.
  
  ## If you do use this code for something, please  let me know, I'd like to know if has been useful to anyone.
  
  from random import *
  from math import *
  import sys
  import scipy
  import numpy
  class SOM:
  
  def __init__(self, height=4, width=4, FV_size=3, learning_rate=0.005):
      self.height = height
      self.width = width
      self.FV_size = FV_size
      self.radius = (height+width)/3
      self.learning_rate = learning_rate
      self.nodes = scipy.array([[ [random()*255 for 
       i in range(FV_size)]  for x in range(width)] for y in range(height)])
  
  
      self.nodes = scipy.array([[1,2,3],[4,5,6],[4,5,6],
     [4,5,6],[4,5,6],    [4,5,6],[4,5,6],[4,5,6],[4,5,6],
     [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]])
  
  
  
      print "SOM",self.nodes
  
    def train(self, iterations=1000, train_vector=[[]]):
      for t in range(len(train_vector)):
          train_vector[t] = scipy.array(train_vector[t])
      print "training",train_vector[t],t
      time_constant = iterations/log(self.radius)
      delta_nodes = scipy.array([[[0 for i in range(self.FV_size)] 
      for x in range(self.width)] for y in range(self.height)])
  
      for i in range(1, iterations+1):
          delta_nodes.fill(0)
          radius_decaying=self.radius*exp(-1.0*i/time_constant)
          rad_div_val = 2 * radius_decaying * i
                                                                                       learning_rate_decaying=self.learning_rate*exp(-1.0*i/time_constant)
           sys.stdout.write("\rTraining Iteration: 
         " + str(i) + "/" +  str(iterations))
  
          for j in range(len(train_vector)):
              best = self.best_match(train_vector[j])
              for loc in self.find_neighborhood(best, radius_decaying):
                  influence = exp( (-1.0 * (loc[2]**2)) / rad_div_val)
                  inf_lrd = influence*learning_rate_decaying
  
                  delta_nodes[loc[0],loc[1]] += inf_lrd*
         (train_vector[j]-  self.nodes[loc[0],loc[1]])
          self.nodes += delta_nodes
      sys.stdout.write("\n")
   # Returns a list of points which live within 'dist' of 'pt'
   # Uses the Chessboard distance
    # pt is (row, column)
    def find_neighborhood(self, pt, dist):
      min_y = max(int(pt[0] - dist), 0)
      max_y = min(int(pt[0] + dist), self.height)
      min_x = max(int(pt[1] - dist), 0)
      max_x = min(int(pt[1] + dist), self.width)
      neighbors = []
      for y in range(min_y, max_y):
          for x in range(min_x, max_x):
              dist = abs(y-pt[0]) + abs(x-pt[1])
              neighbors.append((y,x,dist))
      return neighbors
  
  # Returns location of best match, uses Euclidean distance
  # target_FV is a scipy array
  def best_match(self, target_FV):
      loc = scipy.argmin((((self.nodes - target_FV)**2).sum(axis=2))**0.5)
      r = 0
      while loc &gt; self.width:
          loc -= self.width
          r += 1
      c = loc
      return (r, c)
  
  # returns the Euclidean distance between two Feature Vectors
  # FV_1, FV_2 are scipy arrays
  def FV_distance(self, FV_1, FV_2):
      return (sum((FV_1 - FV_2)**2))**0.5
  
   if __name__ == "__main__":
       print "Initialization..."
  
        colors = [ [0, 0, 0], [0, 0, 255], [0, 255, 0], 
            [0, 255, 255], [255, 0,  0], [255, 0, 255],  
           [255, 255, 0], [255, 255, 255]]
        width = 32
       height = 32
       color_som = SOM(width,height,3,0.05)
       print "Training colors..."
       color_som.train(1000, colors)
       try:
           from PIL import Image
           print "Saving Image: sompy_test_colors.png..."
           img = Image.new("RGB", (width, height))
           for r in range(height):
               for c in range(width):
                     img.putpixel((c,r),(int(color_som.nodes[r,c,0]),  
                          int(color_som.nodes[r,c,1]),       int(color_som.nodes[r,c,2])))
           print "color nodes",color_som.nodes
           img = img.resize((width*10, height*10),Image.NEAREST)
           img.save("sompy_test_colors.png")
      except:
           print "Error saving the image, do you have PIL (Python Imaging       Library) installed?"
  </code></pre>
  
  <p>but when I try to go from </p>
  
  <pre><code>self.nodes = scipy.array([[ [random()*255 
       for i in range(FV_size)] for x in range(width)] 
       for y in range(height)])
  </code></pre>
  
  <p>which was in the original code to something like this:</p>
  
  <pre><code>     self.nodes = scipy.array([[1,2,3],[4,5,6],[4,5,6],
         [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],
       [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]])
  </code></pre>
  
  <p>I get the error message:</p>
  
  <pre><code>File "sompy5.py", line 112, in &lt;module&gt;
  color_som.train(1000, colors)
  File "sompy5.py", line 65, in train
  best = self.best_match(train_vector[j])
   File "sompy5.py", line 92, in best_match
  loc = scipy.argmin((((self.nodes - target_FV)**2).sum(axis=2))**0.5)
   File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", 
   line 25, in   _sum
    out=out, keepdims=keepdims)
    ValueError: 'axis' entry is out of bounds
  </code></pre>
  
  <p>Is there something that has to be done to get the vectors to match up? </p>