In [1]:
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

In [2]:
def f(x):
    return 0.3*x*x + 2*np.sin(x) - 2*x + 0.5

In [3]:
x = np.arange(0.0, 10.0, 0.1)
x


Out[3]:
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,
        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,
        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,
        4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,  5.2,  5.3,  5.4,
        5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,  6.3,  6.4,  6.5,
        6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,  7.4,  7.5,  7.6,
        7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,  8.5,  8.6,  8.7,
        8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,  9.6,  9.7,  9.8,
        9.9])

In [4]:
y = f(x)
y


Out[4]:
array([ 0.5       ,  0.50266683,  0.50933866,  0.51804041,  0.52683668,
        0.53385108,  0.53728495,  0.53543537,  0.52671218,  0.50965382,
        0.48294197,  0.44541472,  0.39607817,  0.33411637,  0.25889946,
        0.16998997,  0.06714721, -0.04967038, -0.18030474, -0.32439982,
       -0.48140515, -0.65058127, -0.83100719, -1.02158958, -1.22107364,
       -1.42805571, -1.64099726, -1.85824024, -2.0780237 , -2.29850134,
       -2.51775998, -2.73383868, -2.94474829, -3.14849139, -3.3430822 ,
       -3.52656646, -3.69704089, -3.85267228, -3.99171578, -4.11253232,
       -4.21360499, -4.29355422, -4.35115154, -4.38533187, -4.39520415,
       -4.38006024, -4.33938201, -4.27284652, -4.18032922, -4.06190523,
       -3.91784855, -3.74862936, -3.55490931, -3.33753488, -3.09752898,
       -2.83608065, -2.55453328, -2.25437109, -1.93720436, -1.60475333,
       -1.258831  , -0.90132501, -0.53417881, -0.1593722 ,  0.22109841,
        0.60523998,  0.99108273,  1.37669984,  1.7602267 ,  2.13987953,
        2.5139732 ,  2.88093808,  3.23933573,  3.58787324,  3.92541619,
        4.25099995,  4.56383934,  4.86333647,  5.14908669,  5.42088268,
        5.67871649,  5.92277962,  6.15346111,  6.37134367,  6.57719782,
        6.77197423,  6.9567942 ,  7.13293846,  7.30183439,  7.46504171,
        7.62423697,  7.78119672,  7.93777983,  8.09590885,  8.25755085,
        8.42469776,  8.59934644,  8.78347875,  8.97904174,  9.18792821])

In [5]:
plt.plot(x, y, 'bo')


Out[5]:
[<matplotlib.lines.Line2D at 0x9e35b00>]

In [6]:
xmin = x.min()
xmin


Out[6]:
0.0

In [7]:
xmax = x.max()
xmax


Out[7]:
9.9000000000000004

In [8]:
ymin = y.min()
ymax = y.max()
ymax


Out[8]:
9.1879282124493606

In [9]:
x2 = x.copy()
y2 = y.copy()

In [10]:
x2 -= xmin
x2 /= (xmax-xmin)
y2 -= ymin
y2 /= (ymax - ymin)

In [11]:
# lets import our ANN
from ann import ANN

In [12]:
fann = ANN([1,10,1])

In [13]:
inputs = [[i] for i in x2]
targets = [[t] for t in y2]
fann.train(inputs, targets, 10000)


(0, 0.22340850210042842)
(100, 0.060816486733604777)
(200, 0.042505570278528387)
(300, 0.017987322726478357)
(400, 0.01274889724187859)
(500, 0.010924588330269136)
(600, 0.010142501369480202)
(700, 0.0097710726316911495)
(800, 0.0095677237539190628)
(900, 0.0094234203413849082)
(1000, 0.0092854739321701768)
(1100, 0.0091283797051466848)
(1200, 0.0089408215101882873)
(1300, 0.0087190589784596241)
(1400, 0.0084632852141428849)
(1500, 0.0081756179316208842)
(1600, 0.0078590835695568021)
(1700, 0.0075172006272896776)
(1800, 0.0071538867328859081)
(1900, 0.0067735010546152467)
(2000, 0.0063809056779764745)
(2100, 0.0059814830823583442)
(2200, 0.0055810812146126513)
(2300, 0.0051858778155934931)
(2400, 0.0048021683645192029)
(2500, 0.0044360922590330927)
(2600, 0.0040933215809184822)
(2700, 0.0037787453178705502)
(2800, 0.0034961870538454728)
(2900, 0.0032481935629931047)
(3000, 0.0030359241428255341)
(3100, 0.0028591564986868408)
(3200, 0.0027164072130679588)
(3300, 0.002605147354128996)
(3400, 0.0025220807067431717)
(3500, 0.0024634462082250563)
(3600, 0.0024253080134378005)
(3700, 0.0024038046401984379)
(3800, 0.0023953399819915721)
(3900, 0.0023967105536447527)
(4000, 0.0024051728145677905)
(4100, 0.0024184606064656987)
(4200, 0.0024347655575739846)
(4300, 0.0024526933909228203)
(4400, 0.0024712073866347724)
(4500, 0.002489567715019487)
(4600, 0.0025072726878776087)
(4700, 0.0025240056091593212)
(4800, 0.0025395890450097096)
(4900, 0.0025539470117621919)
(5000, 0.0025670747366233595)
(5100, 0.0025790151768668237)
(5200, 0.0025898412817175636)
(5300, 0.0025996429531981282)
(5400, 0.0026085177355905128)
(5500, 0.0026165643865087278)
(5600, 0.0026238786224024617)
(5700, 0.0026305504676650431)
(5800, 0.0026366627589452102)
(5900, 0.0026422904604403502)
(6000, 0.0026475005312837081)
(6100, 0.0026523521540383365)
(6200, 0.0026568971860748422)
(6300, 0.002661180735828491)
(6400, 0.0026652417960469524)
(6500, 0.0026691138883378389)
(6600, 0.0026728256894279186)
(6700, 0.0026764016210327471)
(6800, 0.0026798623932854926)
(6900, 0.0026832254971995767)
(7000, 0.0026865056453126632)
(7100, 0.0026897151620401396)
(7200, 0.0026928643266849414)
(7300, 0.002695961672857016)
(7400, 0.0026990142483965082)
(7500, 0.0027020278399566204)
(7600, 0.0027050071662738742)
(7700, 0.0027079560439170871)
(7800, 0.0027108775290004684)
(7900, 0.002713774038021555)
(8000, 0.0027166474506532942)
(8100, 0.0027194991969956162)
(8200, 0.0027223303314944931)
(8300, 0.0027251415954499332)
(8400, 0.0027279334697948177)
(8500, 0.0027307062195876322)
(8600, 0.0027334599314715402)
(8700, 0.002736194545172768)
(8800, 0.0027389098799522147)
(8900, 0.0027416056568023406)
(9000, 0.0027442815170507879)
(9100, 0.0027469370379398445)
(9200, 0.0027495717456671107)
(9300, 0.0027521851262909732)
(9400, 0.0027547766348447319)
(9500, 0.002757345702954524)
(9600, 0.0027598917452012093)
(9700, 0.0027624141644332634)
(9800, 0.0027649123562039249)
(9900, 0.0027673857124765324)

In [14]:
preds = np.array([fann.predict(input)[0] for input in inputs])
preds


Out[14]:
array([ 0.32982807,  0.33902976,  0.3471457 ,  0.35415221,  0.36002655,
        0.36474698,  0.3682929 ,  0.37064501,  0.3717857 ,  0.37169942,
        0.37037326,  0.36779768,  0.36396733,  0.35888202,  0.35254786,
        0.34497844,  0.33619613,  0.32623337,  0.31513395,  0.30295412,
        0.28976357,  0.27564606,  0.26069975,  0.24503704,  0.22878392,
        0.21207883,  0.19507098,  0.17791821,  0.16078446,  0.14383698,
        0.12724336,  0.11116857,  0.09577222,  0.08120595,  0.06761133,
        0.05511811,  0.04384302,  0.033889  ,  0.02534491,  0.01828559,
        0.01277227,  0.00885326,  0.00656473,  0.0059316 ,  0.00696851,
        0.00968068,  0.01406472,  0.02010941,  0.02779622,  0.03709987,
        0.04798865,  0.06042466,  0.074364  ,  0.08975682,  0.10654739,
        0.12467401,  0.14406909,  0.16465911,  0.18636467,  0.20910062,
        0.23277623,  0.25729549,  0.28255751,  0.30845702,  0.33488504,
        0.36172961,  0.38887664,  0.41621091,  0.44361709,  0.47098087,
        0.49819005,  0.52513576,  0.55171351,  0.57782431,  0.60337566,
        0.62828241,  0.65246753,  0.67586274,  0.69840893,  0.72005647,
        0.74076531,  0.76050494,  0.77925419,  0.79700086,  0.8137413 ,
        0.82947975,  0.84422776,  0.85800341,  0.87083056,  0.88273807,
        0.89375897,  0.90392972,  0.91328945,  0.92187923,  0.92974145,
        0.93691917,  0.94345564,  0.94939378,  0.95477577,  0.95964272])

In [15]:
predsscaled = preds * (ymax - ymin) + ymin

In [16]:
plt.plot(x, y, 'bo')


Out[16]:
[<matplotlib.lines.Line2D at 0xa04a978>]

In [17]:
plt.plot(x, predsscaled, 'g*')


Out[17]:
[<matplotlib.lines.Line2D at 0xab70b70>]

In [18]:
plt.plot(x, y, 'bo', x, predsscaled, 'g*')


Out[18]:
[<matplotlib.lines.Line2D at 0xad64518>,
 <matplotlib.lines.Line2D at 0xad645f8>]

In [ ]: