Wednesday, 26 June 2019

Neural network to work out cashflow and timeflow

The following neural network in Python was trained on available cashflow in my bank account prior to the time of purchase of a coffee and snack at a set location. The time spent at this activity at the cafe was measured using a stopwatch. To normalise the data used in training, the available balances were divided by a 1000 and minutes were divided by 100 so all the values fell between 0 and 1. So after running the network, the predicted output for cashflow is multiplied by 1000 and the minutes multiplied by 100. Predicted X for this example is 0.369 * 1000 = £369 predicted available cashflow on approach to the cafe, and the predicted Y  is  0.42 * 100 = 42 minutes predicted time spent there.

After saving the code to a file filename.py it can be run from a terminal by typing python filename.py   

Here is the code:


import numpy as np
from numpy import  array
from sklearn import svm

clf = svm.SVC(gamma=0.001, C=100)

y = np.array([0.381, 0.96, 0.385, 0.369, 0.3225, 0.28, 0.2776, 0.2641, 0.3415,
0.6881, 0.4925, 0.5263, 0.2965, 0.8622, 0.4678, 0.3493, 0.3008, 0.2553, 0.178,
0.3826, 0.3378, 0.4217, 0.42, 0.4197, 0.3938, 0.5988, 0.5358]).T

y = np.array(y, ndmin = 1).T
print(y, y.shape)

x = np.array([0.14569, 0.17944, 0.15496, 0.07607, 0.01223, 0.00873, 0.26456, 0.19928, 0.01417, 0.00220, 0.16451, 0.09408, 0.23073, 0.13403, 0.11177, 0.29657, 0.21266,
0.11302, 0.29185, 0.24873, 0.15997, 0.09582, 0.30616, 0.11861, 0.18292, 0.12121, 0.08206 ]).T

x = np.array(x, ndmin = 2).T
print(x, x.shape)  

xtrain, ytrain =  x[:-1], y[:-1]  

clf.fit(xtrain, ytrain) #train the data

print('Prediction for x:', clf.predict(x[-1])) #predict the data
print('Prediction for y:', clf.predict(y[-1])) #predict the data



Here is the screen / terminal output:

david@debian:~/Timecfnn$ python cashflowtime+time.py
(array([ 0.381 ,  0.96  ,  0.385 ,  0.369 ,  0.3225,  0.28  ,  0.2776,
        0.2641,  0.3415,  0.6881,  0.4925,  0.5263,  0.2965,  0.8622,
        0.4678,  0.3493,  0.3008,  0.2553,  0.178 ,  0.3826,  0.3378,
        0.4217,  0.42  ,  0.4197,  0.3938,  0.5988,  0.5358]), (27,))
(array([[ 0.14569],
       [ 0.17944],
       [ 0.15496],
       [ 0.07607],
       [ 0.01223],
       [ 0.00873],
       [ 0.26456],
       [ 0.19928],
       [ 0.01417],
       [ 0.0022 ],
       [ 0.16451],
       [ 0.09408],
       [ 0.23073],
       [ 0.13403],
       [ 0.11177],
       [ 0.29657],
       [ 0.21266],
       [ 0.11302],
       [ 0.29185],
       [ 0.24873],
       [ 0.15997],
       [ 0.09582],
       [ 0.30616],
       [ 0.11861],
       [ 0.18292],
       [ 0.12121],
       [ 0.08206]]), (27, 1))
('Prediction for x:', array([ 0.369]))
('Prediction for y:', array([ 0.42]))
david@debian:~/Timecfnn$


Acknowledgements: I  would like to thank Jack's Snax of Barum Arcade in Barnstaple for the endless supply of coffee, and houmous and haloumi wraps with lettuce and tomato and pleasant conversation.

Picture: me outside Jack's Snax


No comments:

Post a Comment