{"id":22,"date":"2007-10-25T17:34:14","date_gmt":"2007-10-25T16:34:14","guid":{"rendered":"http:\/\/robin.meier.free.fr\/site\/?page_id=22"},"modified":"2014-05-24T13:27:53","modified_gmt":"2014-05-24T12:27:53","slug":"self-organizing-map-som-in-python","status":"publish","type":"post","link":"https:\/\/robinmeier.net\/?p=22","title":{"rendered":"Self-organizing Map SOM in Python"},"content":{"rendered":"<pre lang=\"PYTHON\">## Self-Organizing Map (SOM)\r\n##\r\n## http:\/\/robin.meier.free.fr\r\n## http:\/\/www.neuromuse.org\r\n## paris 13eme, february 06\r\n##\r\n\r\n\"\"\"self organizing-map a la kohonen. to interact, send and receive data via udp\"\"\"\r\n\r\nimport sys\r\n#psyco for intel procs only...\r\n#from psyco import *\r\nfrom numarray import *\r\nfrom numarray.random_array import *\r\nfrom math import *\r\nfrom socket import *\r\nfrom cPickle import *\r\n\r\nclass Networking:\r\n\tdef __init__(self, rechost=\"\", recport=12345, buf=32768, verbose=0):\r\n\t\tself.recaddr = (rechost,recport)\r\n\t\tself.buf = buf\r\n\t\tself.UDPSock = socket(AF_INET,SOCK_DGRAM)\r\n\t\tself.TCPSock = socket(AF_INET, SOCK_STREAM)\r\n\t\tself.verbose = verbose\r\n\tdef bindUDPSocket(self):\r\n\t\tself.UDPSock.bind(self.recaddr)\r\n\t\tif self.verbose:\r\n\t\t\tprint \"UDP socket bound on\", self.recaddr\r\n\tdef bindTCPSocket(self):\r\n\t\tself.TCPSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)\r\n\t\tself.TCPSock.bind(self.recaddr)\r\n\t\tself.TCPSock.listen(5)\t\t#let at most one connection wait to be processed. probably too small.\r\n\t\tif self.verbose:\r\n\t\t\tprint \"tcp server running on\", self.recaddr\r\n\tdef connectTCPServer(self,desthost=\"localhost\", destport=12345):\r\n\t\tself.TCPSock.connect((desthost,destport))\r\n\t\tif self.verbose:\r\n\t\t\tprint \"connected to\", desthost, destport\r\n\tdef receiveUDPData(self):\r\n\t\twhile 1:\r\n\t\t\tprint \"waiting for data\"\r\n\t\t\tdata,self.recaddr = self.UDPSock.recvfrom(self.buf)\r\n\t\t\tif not data:\r\n\t\t\t\tprint \"Client has exited!\"\r\n\t\t\t\tbreak\r\n\t\t\telse:\r\n\t\t\t\tinput = loads(data)\r\n\t\t\t\tif self.verbose:\r\n\t\t\t\t\tprint \"YO! message received '\", input,\"'\"\r\n\t\t\t\treturn input\r\n\t\t\t\tbreak\r\n\tdef receiveTCPData(self):\r\n\t\twhile 1:\r\n\t\t\tclientsock, clientaddr = self.TCPSock.accept()\r\n\t\t\tif self.verbose:\r\n\t\t\t\tprint \"got connection from\", clientsock.getpeername()\r\n\t\t\tdata = clientsock.recv(self.buf)\r\n\t\t\tif not data:\r\n\t\t\t\tprint \"Client has exited!\"\r\n\t\t\t\tbreak\r\n\t\t\telse:\r\n\t\t\t\tinput = loads(data)\r\n\t\t\t\tif self.verbose:\r\n\t\t\t\t\tprint \"YO! message received '\", input,\"'\"\r\n\t\t\t\treturn input\r\n\t\t\t\tbreak\r\n\tdef sendUDPData(self, datatosend, desthost=\"localhost\", destport=12345):\r\n\t\tdestaddr = (desthost,destport)\r\n\t\tif self.verbose:\r\n\t\t\tprint \"sending to\", destaddr\r\n\t\tself.UDPSock.sendto(dumps(datatosend),destaddr)\r\n\t\tif self.verbose:\r\n\t\t\tprint \"YO! data was sent '\", datatosend,\"'\"\r\n\t\telse:\r\n\t\t\tprint \"YO! data sent.\"\r\n\tdef sendTCPData(self, datatosend, desthost=\"localhost\", destport=12345):\r\n\t\tdestaddr = (desthost,destport)\r\n\t\tif self.verbose:\r\n\t\t\tprint \"sending to\", destaddr\r\n\t\tself.TCPSock.sendto(dumps(datatosend),destaddr)\r\n\t\tif self.verbose:\r\n\t\t\tprint \"YO! data was sent via tcp '\", datatosend, \"'\"\r\n\t\telse:\r\n\t\t\tprint \"YO! data sent (tcp).\"\r\n\r\nclass Som:\r\n\tdef __init__(self, somsize=100, vectorsize=10):\r\n\t\tself.somsize = somsize\r\n\t\tself.weights = random((somsize,vectorsize))\r\n\tdef randomizeWeights(self, inputvector):\r\n\t\tself.weights = random((self.somsize,len(inputvector)))\r\n\tdef getActivation(self, inputvector):\r\n\t\t\"\"\"euclidean distance without sqrt\"\"\"\r\n\t\tself.activation = sum(((self.weights-inputvector)*(self.weights-inputvector)),1)\r\n\tdef getWinner(self):\r\n\t\tself.winner = int((argsort(self.activation))[0])\r\n\tdef updateWeights(self, inputvector, learningrate=0.1, radius=3):\r\n\t\t#conversion index-&gt;coord\r\n\t\tseite = int(sqrt(self.somsize))\r\n\t\twiny = int(self.winner\/seite)\r\n\t\twinx = int(self.winner%seite)\r\n\t\t#calculate distances\r\n\t\tdef dist(x,y):\r\n\t\t\treturn (x-winx)**2+(y-winy)**2\r\n\t\tself.distances = fromfunction(dist, (seite, seite))\r\n\t\t#print self.distances\r\n\t\tfor i in range(self.somsize):\r\n\t\t\tiy=int(i\/seite)\r\n\t\t\tix = int(i%seite)\r\n\t\t\tn = float(self.distances[iy,ix])\r\n\t\t\tif n &lt; radius:#pour mettre \u00e0 jour seulement les neurones proche du winner (c plus vite)\r\n\t\t\t\tdelta = learningrate*(inputvector-(take(self.weights,i)))*(exp(-1*((n*n)\/(2*radius*radius))))#take(self.weights,winner) is vector of winner\r\n\t\t\t\tput(self.weights,i,((take(self.weights,i)+delta)))\r\n\r\n################################################################\r\n\r\n#mexican hat#\r\n#(2\/sqrt(3))*(pow(pi,-0.25))*(1-(n*n))*(exp(-1*((n*n)\/2)))\r\n\r\n#gaussian#\r\n#exp(-1*((n*n)\/(2*radius*radius)))\r\n\r\n################\r\n# stuff to send#\r\n################\r\n\r\n# send poids (somsize)\r\n# UDPSock.sendto(dumps(poids),destaddr)\r\n# print \"message sent '\", poids, \"'\"\r\n\r\n# send activation (somsize)\r\n# UDPSock.sendto(dumps(activation.getflat()),destaddr)\r\n# print \"message sent '\", activation, \"'\"\r\n\r\n# send winner (input)\r\n# UDPSock.sendto(dumps(list(poids[winner,:])),destaddr)\r\n# print \"message sent '\", list(poids[winner,:]), \"'\"<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>## Self-Organizing Map (SOM) ## ## http:\/\/robin.meier.free.fr ## http:\/\/www.neuromuse.org ## paris 13eme, february 06 ## &#8220;&#8221;&#8221;self organizing-map a la kohonen. to interact, send and receive data via udp&#8221;&#8221;&#8221; import sys #psyco for intel procs only&#8230; #from psyco import * from numarray import * from numarray.random_array import * from math import * from socket import * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1183,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[28,81],"class_list":["post-22","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-ai","tag-code"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Self-organizing Map SOM in Python - robin meier wiratunga<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/robinmeier.net\/?p=22\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Self-organizing Map SOM in Python - robin meier wiratunga\" \/>\n<meta property=\"og:description\" content=\"## Self-Organizing Map (SOM) ## ## http:\/\/robin.meier.free.fr ## http:\/\/www.neuromuse.org ## paris 13eme, february 06 ## &quot;&quot;&quot;self organizing-map a la kohonen. to interact, send and receive data via udp&quot;&quot;&quot; import sys #psyco for intel procs only... #from psyco import * from numarray import * from numarray.random_array import * from math import * from socket import * [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/robinmeier.net\/?p=22\" \/>\n<meta property=\"og:site_name\" content=\"robin meier wiratunga\" \/>\n<meta property=\"article:published_time\" content=\"2007-10-25T16:34:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-05-24T12:27:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/robinmeier.net\/wp-content\/uploads\/2007\/10\/Screenshot-2014-03-07-11.50.44.png\" \/>\n\t<meta property=\"og:image:width\" content=\"183\" \/>\n\t<meta property=\"og:image:height\" content=\"231\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/#\\\/schema\\\/person\\\/4bb244780550926b09928d8d11a13fd0\"},\"headline\":\"Self-organizing Map SOM in Python\",\"datePublished\":\"2007-10-25T16:34:14+00:00\",\"dateModified\":\"2014-05-24T12:27:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22\"},\"wordCount\":5,\"image\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/robinmeier.net\\\/wp-content\\\/uploads\\\/2007\\\/10\\\/Screenshot-2014-03-07-11.50.44.png\",\"keywords\":[\"ai\",\"code\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22\",\"url\":\"https:\\\/\\\/robinmeier.net\\\/?p=22\",\"name\":\"Self-organizing Map SOM in Python - robin meier wiratunga\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/robinmeier.net\\\/wp-content\\\/uploads\\\/2007\\\/10\\\/Screenshot-2014-03-07-11.50.44.png\",\"datePublished\":\"2007-10-25T16:34:14+00:00\",\"dateModified\":\"2014-05-24T12:27:53+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/#\\\/schema\\\/person\\\/4bb244780550926b09928d8d11a13fd0\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/robinmeier.net\\\/?p=22\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#primaryimage\",\"url\":\"https:\\\/\\\/robinmeier.net\\\/wp-content\\\/uploads\\\/2007\\\/10\\\/Screenshot-2014-03-07-11.50.44.png\",\"contentUrl\":\"https:\\\/\\\/robinmeier.net\\\/wp-content\\\/uploads\\\/2007\\\/10\\\/Screenshot-2014-03-07-11.50.44.png\",\"width\":183,\"height\":231},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/?p=22#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/robinmeier.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Self-organizing Map SOM in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/#website\",\"url\":\"https:\\\/\\\/robinmeier.net\\\/\",\"name\":\"robin meier wiratunga\",\"description\":\"artist website and portfolio\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/robinmeier.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/robinmeier.net\\\/#\\\/schema\\\/person\\\/4bb244780550926b09928d8d11a13fd0\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4300ef5db4c1119caab717a4f56d23ec6e575d5e9adcb709f19302aa42fdfdd1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4300ef5db4c1119caab717a4f56d23ec6e575d5e9adcb709f19302aa42fdfdd1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4300ef5db4c1119caab717a4f56d23ec6e575d5e9adcb709f19302aa42fdfdd1?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\\\/\\\/robinmeier.net\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Self-organizing Map SOM in Python - robin meier wiratunga","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/robinmeier.net\/?p=22","og_locale":"en_US","og_type":"article","og_title":"Self-organizing Map SOM in Python - robin meier wiratunga","og_description":"## Self-Organizing Map (SOM) ## ## http:\/\/robin.meier.free.fr ## http:\/\/www.neuromuse.org ## paris 13eme, february 06 ## \"\"\"self organizing-map a la kohonen. to interact, send and receive data via udp\"\"\" import sys #psyco for intel procs only... #from psyco import * from numarray import * from numarray.random_array import * from math import * from socket import * [&hellip;]","og_url":"https:\/\/robinmeier.net\/?p=22","og_site_name":"robin meier wiratunga","article_published_time":"2007-10-25T16:34:14+00:00","article_modified_time":"2014-05-24T12:27:53+00:00","og_image":[{"width":183,"height":231,"url":"https:\/\/robinmeier.net\/wp-content\/uploads\/2007\/10\/Screenshot-2014-03-07-11.50.44.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/robinmeier.net\/?p=22#article","isPartOf":{"@id":"https:\/\/robinmeier.net\/?p=22"},"author":{"name":"admin","@id":"https:\/\/robinmeier.net\/#\/schema\/person\/4bb244780550926b09928d8d11a13fd0"},"headline":"Self-organizing Map SOM in Python","datePublished":"2007-10-25T16:34:14+00:00","dateModified":"2014-05-24T12:27:53+00:00","mainEntityOfPage":{"@id":"https:\/\/robinmeier.net\/?p=22"},"wordCount":5,"image":{"@id":"https:\/\/robinmeier.net\/?p=22#primaryimage"},"thumbnailUrl":"https:\/\/robinmeier.net\/wp-content\/uploads\/2007\/10\/Screenshot-2014-03-07-11.50.44.png","keywords":["ai","code"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/robinmeier.net\/?p=22","url":"https:\/\/robinmeier.net\/?p=22","name":"Self-organizing Map SOM in Python - robin meier wiratunga","isPartOf":{"@id":"https:\/\/robinmeier.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/robinmeier.net\/?p=22#primaryimage"},"image":{"@id":"https:\/\/robinmeier.net\/?p=22#primaryimage"},"thumbnailUrl":"https:\/\/robinmeier.net\/wp-content\/uploads\/2007\/10\/Screenshot-2014-03-07-11.50.44.png","datePublished":"2007-10-25T16:34:14+00:00","dateModified":"2014-05-24T12:27:53+00:00","author":{"@id":"https:\/\/robinmeier.net\/#\/schema\/person\/4bb244780550926b09928d8d11a13fd0"},"breadcrumb":{"@id":"https:\/\/robinmeier.net\/?p=22#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/robinmeier.net\/?p=22"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/robinmeier.net\/?p=22#primaryimage","url":"https:\/\/robinmeier.net\/wp-content\/uploads\/2007\/10\/Screenshot-2014-03-07-11.50.44.png","contentUrl":"https:\/\/robinmeier.net\/wp-content\/uploads\/2007\/10\/Screenshot-2014-03-07-11.50.44.png","width":183,"height":231},{"@type":"BreadcrumbList","@id":"https:\/\/robinmeier.net\/?p=22#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/robinmeier.net\/"},{"@type":"ListItem","position":2,"name":"Self-organizing Map SOM in Python"}]},{"@type":"WebSite","@id":"https:\/\/robinmeier.net\/#website","url":"https:\/\/robinmeier.net\/","name":"robin meier wiratunga","description":"artist website and portfolio","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/robinmeier.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/robinmeier.net\/#\/schema\/person\/4bb244780550926b09928d8d11a13fd0","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4300ef5db4c1119caab717a4f56d23ec6e575d5e9adcb709f19302aa42fdfdd1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4300ef5db4c1119caab717a4f56d23ec6e575d5e9adcb709f19302aa42fdfdd1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4300ef5db4c1119caab717a4f56d23ec6e575d5e9adcb709f19302aa42fdfdd1?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/robinmeier.net\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinmeier.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":1,"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":1184,"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions\/1184"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robinmeier.net\/index.php?rest_route=\/wp\/v2\/media\/1183"}],"wp:attachment":[{"href":"https:\/\/robinmeier.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinmeier.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinmeier.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}