<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.extremist.software/index.php?action=history&amp;feed=atom&amp;title=User%3AElgreengeeto%2FPython_Perceptron</id>
	<title>User:Elgreengeeto/Python Perceptron - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.extremist.software/index.php?action=history&amp;feed=atom&amp;title=User%3AElgreengeeto%2FPython_Perceptron"/>
	<link rel="alternate" type="text/html" href="https://wiki.extremist.software/index.php?title=User:Elgreengeeto/Python_Perceptron&amp;action=history"/>
	<updated>2026-04-09T18:45:29Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.13</generator>
	<entry>
		<id>https://wiki.extremist.software/index.php?title=User:Elgreengeeto/Python_Perceptron&amp;diff=3573&amp;oldid=prev</id>
		<title>Elgreengeeto at 23:46, 14 March 2009</title>
		<link rel="alternate" type="text/html" href="https://wiki.extremist.software/index.php?title=User:Elgreengeeto/Python_Perceptron&amp;diff=3573&amp;oldid=prev"/>
		<updated>2009-03-14T23:46:17Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:46, 14 March 2009&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l67&quot;&gt;Line 67:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 67:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;		perceptron.learn(case[0],case[1],case[2], case[3])&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;		perceptron.learn(case[0],case[1],case[2], case[3])&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#define a dataset to try and train a perceptron into becoming a &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;NAN &lt;/del&gt;gate&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#define a dataset to try and train a perceptron into becoming a &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;NAND &lt;/ins&gt;gate&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#(turns out this data set is too small, oh Josh is a tricky teacher!)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#(turns out this data set is too small, oh Josh is a tricky teacher!)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;data = [[1,0,0,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;data = [[1,0,0,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Elgreengeeto</name></author>
	</entry>
	<entry>
		<id>https://wiki.extremist.software/index.php?title=User:Elgreengeeto/Python_Perceptron&amp;diff=3530&amp;oldid=prev</id>
		<title>Elgreengeeto: New page: &lt;pre&gt; #a dot product is the sum of the products of aligned-elements from two #same-lengthed arrays def dot_product(a, b):    sum = 0    i = 0    while i &lt; len(a):        sum += a[i] * b[i]...</title>
		<link rel="alternate" type="text/html" href="https://wiki.extremist.software/index.php?title=User:Elgreengeeto/Python_Perceptron&amp;diff=3530&amp;oldid=prev"/>
		<updated>2009-03-13T16:56:31Z</updated>

		<summary type="html">&lt;p&gt;New page: &amp;lt;pre&amp;gt; #a dot product is the sum of the products of aligned-elements from two #same-lengthed arrays def dot_product(a, b):    sum = 0    i = 0    while i &amp;lt; len(a):        sum += a[i] * b[i]...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
#a dot product is the sum of the products of aligned-elements from two&lt;br /&gt;
#same-lengthed arrays&lt;br /&gt;
def dot_product(a, b):&lt;br /&gt;
   sum = 0&lt;br /&gt;
   i = 0&lt;br /&gt;
   while i &amp;lt; len(a):&lt;br /&gt;
       sum += a[i] * b[i]&lt;br /&gt;
       i += 1&lt;br /&gt;
   return sum&lt;br /&gt;
&lt;br /&gt;
class Perceptron:&lt;br /&gt;
&lt;br /&gt;
	#percieve method takes three boolean inputs&lt;br /&gt;
	def percieve(self, ip1, ip2, ip3):&lt;br /&gt;
		#get the dot product of those inputs and the associated input&amp;#039;s weights&lt;br /&gt;
		sum = dot_product([ip1, ip2, ip3],[self.ip1_weight, self.ip2_weight, self.ip3_weight])&lt;br /&gt;
		#return the boolean evaulation of whether or not that sum is greater than&lt;br /&gt;
		#or equal to the perceptron&amp;#039;s threshhold&lt;br /&gt;
		return ( sum &amp;gt;= self.threshhold )&lt;br /&gt;
&lt;br /&gt;
	#learn method compares output of percieve() to the expected output&lt;br /&gt;
	def learn(self, ip1, ip2, ip3, expected):&lt;br /&gt;
		#if output is less than expected increment the weights of&lt;br /&gt;
		#_non-null_ inputs by 0.1 (the weights applied to null inputs do not affect&lt;br /&gt;
		#output so we can&amp;#039;t know if those weights were right or wrong)&lt;br /&gt;
		if self.percieve(ip1, ip2, ip3) &amp;lt; expected:&lt;br /&gt;
			if ip1:&lt;br /&gt;
				self.ip1_weight += self.learn_rate&lt;br /&gt;
				print self.ip1_weight&lt;br /&gt;
			if ip2:&lt;br /&gt;
				self.ip2_weight += self.learn_rate&lt;br /&gt;
				print self.ip2_weight&lt;br /&gt;
			if ip3:&lt;br /&gt;
				self.ip3_weight += self.learn_rate&lt;br /&gt;
				print self.ip3_weight&lt;br /&gt;
		#if less than expected decrement by 0.1&lt;br /&gt;
		elif self.percieve(ip1, ip2, ip3) &amp;gt; expected:&lt;br /&gt;
			if ip1:&lt;br /&gt;
				self.ip1_weight -= self.learn_rate&lt;br /&gt;
				print self.ip1_weight&lt;br /&gt;
			if ip2:&lt;br /&gt;
				self.ip2_weight -= self.learn_rate&lt;br /&gt;
				print self.ip1_weight&lt;br /&gt;
			if ip3:&lt;br /&gt;
				self.ip3_weight -= self.learn_rate&lt;br /&gt;
				print self.ip1_weight&lt;br /&gt;
&lt;br /&gt;
	#this defines how a Perceptron object represents itself in the interpreter&lt;br /&gt;
	def __str__(self):&lt;br /&gt;
		return &amp;quot;Weights: %s, %s, %s. Threshhold: %s. Learn rate: %s.&amp;quot; % (self.ip1_weight, self.ip2_weight, self.ip3_weight, self.threshhold, self.learn_rate)&lt;br /&gt;
	#same as above&lt;br /&gt;
	__repr__ = __str__&lt;br /&gt;
	&lt;br /&gt;
	#defines initial values for a new Perceptron object&lt;br /&gt;
	def __init__(self):&lt;br /&gt;
		self.ip1_weight = 0.0&lt;br /&gt;
		self.ip2_weight = 0.0&lt;br /&gt;
		self.ip3_weight = 0.0&lt;br /&gt;
		self.threshhold = 0.5&lt;br /&gt;
		self.learn_rate = 0.1&lt;br /&gt;
&lt;br /&gt;
#trains a perceptron according to data, where data is a list of lists in the&lt;br /&gt;
#form [[boolean_value_1,boolean_value_2,boolean_value_3,expected_boolean_output],...]&lt;br /&gt;
def train(data, perceptron):&lt;br /&gt;
	for case in data:&lt;br /&gt;
		perceptron.learn(case[0],case[1],case[2], case[3])&lt;br /&gt;
&lt;br /&gt;
#define a dataset to try and train a perceptron into becoming a NAN gate&lt;br /&gt;
#(turns out this data set is too small, oh Josh is a tricky teacher!)&lt;br /&gt;
data = [[1,0,0,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]]&lt;br /&gt;
&lt;br /&gt;
#do the damned thing&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
	subject = Perceptron()&lt;br /&gt;
	for i in range(1000):&lt;br /&gt;
		train(data, subject)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Elgreengeeto</name></author>
	</entry>
</feed>