{"id":401,"date":"2019-02-19T00:28:15","date_gmt":"2019-02-19T08:28:15","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=401"},"modified":"2021-11-12T14:45:03","modified_gmt":"2021-11-12T22:45:03","slug":"neural-network-prediction","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/neural-network-prediction\/","title":{"rendered":"#60 Neural network prediction using python neurolab"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport neurolab as nl\r\nos.chdir(r'D:\\projects\\wordpress\\ex60')\r\nos.getcwd()\r\n#create train sets\r\nx=np.linspace(-10,10,60)\r\ny=np.cos(x)*0.9\r\nsize=len(x)\r\nx_train=x.reshape(size,1)\r\ny_train=y.reshape(size,1)\r\n#create network with 4 layers and randomly initiate \r\nd=&#x5B;&#x5B;1,1],&#x5B;45,1],&#x5B;45,45,1],&#x5B;45,45,45,1]]\r\nfor i in range(4):\r\n    net=nl.net.newff(&#x5B;&#x5B;-10,10]],d&#x5B;i])\r\n    train_net=nl.train.train_gd(net,x_train,y_train,epochs=1000,show=100)\r\n    outp=net.sim(x_train)\r\n    plt.subplot(2,1,1)\r\n    plt.grid(True)\r\n    plt.plot(train_net)\r\n    plt.title('Hidden Layers: ' + str(i))\r\n    plt.xlabel('Epochs')\r\n    plt.ylabel('quadratic error')\r\n    x2=np.linspace(-10,10,150)\r\n    y2=net.sim(x2.reshape(x2.size,1)).reshape(x2.size)\r\n    y3=outp.reshape(size)\r\n    plt.subplot(2,1,2)\r\n    plt.grid(True)\r\n    plt.plot(x2,y2,'-',x,y,'.',x,y3,'p')\r\n    plt.legend(&#x5B;'y predicted','y actual'])\r\n    plt.savefig(&quot;example60_&quot; + str(i) + '.png', dpi=100)\r\n    plt.show()\r\n    plt.close()\r\n    \r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_0.png?resize=600%2C400&#038;ssl=1\" alt=\"example69_0\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-402\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_0.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_0.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_1.png?resize=600%2C400&#038;ssl=1\" alt=\"example69_1\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-403\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_1.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_1.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_2.png?resize=600%2C400&#038;ssl=1\" alt=\"example69_2\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-404\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_2.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_2.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?resize=600%2C400&#038;ssl=1\" alt=\"example69_3\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-405\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/02\/example69_3.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np import neurolab as nl os.chdir(r&#8217;D:\\projects\\wordpress\\ex60&#8242;) os.getcwd() #create train sets x=np.linspace(-10,10,60) y=np.cos(x)*0.9 size=len(x) x_train=x.reshape(size,1) y_train=y.reshape(size,1) #create network with 4 layers and randomly initiate d=&#x5B;&#x5B;1,1],&#x5B;45,1],&#x5B;45,45,1],&#x5B;45,45,45,1]] for i in range(4): net=nl.net.newff(&#x5B;&#x5B;-10,10]],d&#x5B;i]) train_net=nl.train.train_gd(net,x_train,y_train,epochs=1000,show=100) outp=net.sim(x_train) plt.subplot(2,1,1) plt.grid(True) plt.plot(train_net) plt.title(&#8216;Hidden Layers: &#8216; + str(i)) plt.xlabel(&#8216;Epochs&#8217;) plt.ylabel(&#8216;quadratic error&#8217;) x2=np.linspace(-10,10,150) y2=net.sim(x2.reshape(x2.size,1)).reshape(x2.size) y3=outp.reshape(size) plt.subplot(2,1,2) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-6t","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":5,"url":"https:\/\/gantovnik.com\/bio-tips\/2016\/12\/example-1-interpolation\/","url_meta":{"origin":401,"position":0},"title":"Example 1: Interpolation","author":"gantovnik","date":"2016-12-08","format":false,"excerpt":"Example 1: Interpolation [code language=\"python\"] #Example 1: Interpolation import numpy as np from scipy import interpolate import matplotlib.pyplot as plt import os os.chdir('C:\\\\Anaconda\\\\mycodes\\\\aerospace') os.getcwd() #create the data points and add noise as follows x = np.linspace(-18,18,36) noise = 0.1 * np.random.random(len(x)) signal = np.sinc(x) + noise #create a linear interpolation\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example1","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1-300x200.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1738,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2\/","url_meta":{"origin":401,"position":1},"title":"#329 Golden section method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py [code language=\"python\"] import math as mt def golden(f,a,b,tol=1.0e-10): # Golden section method for determining x # that minimizes the scalar function f(x). # The minimum must be bracketed in (a,b). c1 = (mt.sqrt(5.0)-1.0)\/2.0 c2 = 1.0 - c1 nIt = int(mt.ceil(mt.log(tol\/abs(a-b))\/mt.log(c1))) # first step x1 = c1*a + c2*b\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex329.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":260,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/interpolation\/","url_meta":{"origin":401,"position":2},"title":"Interpolation","author":"gantovnik","date":"2019-01-15","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import interp1d os.chdir(r'D:\\data\\scripts\\wordpress\\ex50') os.getcwd() A,nu,k=10,4,2 def f(x,A,nu,k): return A*np.exp(-k*x)*np.cos(2*np.pi*nu*x) xmax,nx=0.5,8 x=np.linspace(0,xmax,nx) y=f(x,A,nu,k) f_nearest=interp1d(x,y,kind='nearest') f_linear =interp1d(x,y) f_cubic =interp1d(x,y,kind='cubic') x2=np.linspace(0,xmax,100) plt.plot(x,y,'o',label='datapoints') plt.plot(x2,f(x2,A,nu,k),label='exact') plt.plot(x2,f_nearest(x2),label='nearest') plt.plot(x2, f_linear(x2), label='linear') plt.plot(x2, f_cubic(x2), label='cubic') plt.legend() plt.show() plt.tight_layout() plt.savefig(\"example50.png\", dpi=100) plt.show() plt.close() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example50","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example50-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example50-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example50-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1984,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/12\/398-find-points-of-intersection-of-two-circles\/","url_meta":{"origin":401,"position":3},"title":"#398 Find points of intersection of two circles","author":"gantovnik","date":"2023-12-26","format":false,"excerpt":"- how to find an equation of the common chord of two intersected circles. [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import math def get_intersections(x0, y0, r0, x1, y1, r1): # circle 1: (x0, y0), radius r0 # circle 2: (x1, y1), radius r1 d=math.sqrt((x1-x0)**2 + (y1-y0)**2)\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":243,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/classification\/","url_meta":{"origin":401,"position":4},"title":"#48 Classification","author":"gantovnik","date":"2019-01-13","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn import linear_model from sklearn import metrics from sklearn import tree from sklearn import neighbors from sklearn import svm from sklearn import ensemble from sklearn import cluster import seaborn\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example48","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example48.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example48.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example48.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1139,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/200-plot-in-matplotlib\/","url_meta":{"origin":401,"position":5},"title":"#200 Plot in matplotlib","author":"gantovnik","date":"2021-11-22","format":false,"excerpt":"[code language=\"python\"] # Plot in matplotlib import numpy as np import matplotlib.pyplot as plt from math import pi x = np.linspace(-2.0*pi,2.0*pi,200) plt.plot(x,np.sin(x)) samples=x[::2] plt.plot(samples,np.sin(samples),'r*') plt.title('Function sin(x) and some points plotted') plt.grid() plt.xlabel(\"x\") plt.ylabel(\"y\") plt.savefig('ex200.png', dpi=72) plt.show() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex200.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex200.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex200.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex200.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}