import numpy as np
import time
= np.random.randint(10E6,size=(50,1000))
a print(np.shape(a))
= np.random.randint(100,size=(50,1))
w print(np.shape(w))
(50, 1000)
(50, 1)
October 11, 2019
(50, 1000)
(50, 1)
#Vectorisation
t_start = time.time()
z = np.dot(w.T,a).T
t_stop = time.time()
print('Time take: {} ms'.format(1000*(t_stop-t_start)))
#Non vectorized version
z_for = []
t_start = time.time()
for j in range(np.shape(a)[1]):
_count = 0.0
for i in range(np.shape(a)[0]):
_count+=w[i,0]*a[i,j]
z_for.append(_count)
t_stop = time.time()
print('Time take for for-loop: {} ms'.format(1000*(t_stop-t_start)))
#Check the output
print('Check sum: {}'.format(np.sum(np.asarray(z_for).reshape(np.shape(z))-z)))
Time take: 0.3979206085205078 ms
Time take for for-loop: 33.74624252319336 ms
Check sum: 0.0
array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])
#Calculate % of calories from Carb, Protein, Fat for each food
carb = np.array([food_cal[0,i]/np.sum(food_cal[:,i])*100 for i in range(4)])
protein = np.array([food_cal[1,i]/np.sum(food_cal[:,i])*100 for i in range(4)])
fat = np.array([food_cal[2,i]/np.sum(food_cal[:,i])*100 for i in range(4)])
cal = np.array([carb,protein,fat])
print(cal)
[[94.91525424 0. 2.83140283 88.42652796]
[ 2.03389831 43.51464435 33.46203346 10.40312094]
[ 3.05084746 56.48535565 63.70656371 1.17035111]]
[ 59. 239. 155.4 76.9]
[[94.91525424 0. 2.83140283 88.42652796]
[ 2.03389831 43.51464435 33.46203346 10.40312094]
[ 3.05084746 56.48535565 63.70656371 1.17035111]]
(5,)
[1. 2. 3. 4. 5.] [11. 12. 13. 14. 15.] (5,)
(2, 3)
(3,)
(2, 3)
[[1 2 3]
[4 5 6]] [100 200 300]
[[101 202 303]
[104 205 306]]
(m,n) matrix with (+, -, *, /) with (1,n) or (m,1) lead of copying it to (m,n) before conducting computing.
[ 0.68281763 -1.3579685 0.99577659 0.31269709 0.595569 ]
Here a
is a array of rank 1. It is neither a row or a column vector. So this has some non-intuitive effects
So it is recommended for consistency to NOT use data-structures have rank 1 like the one above but instead instantiate the array as the fixed array of known size
ALWAYS COMMIT TO MAKING DEFINED ROW AND COLUMN VECTORS
[[-0.7474656 ]
[-0.75790159]
[ 0.30984002]
[ 0.18874051]
[-0.80470167]]
(5, 1)
Here there are two Square Brackets compared to the previous transport of a
suggesting in the case of a1
it is well-defined 1x5 row vector
[[ 0.55870482 0.56650536 -0.23159476 -0.14107704 0.60148682]
[ 0.56650536 0.57441482 -0.23482825 -0.14304673 0.60988468]
[-0.23159476 -0.23482825 0.09600084 0.05847936 -0.24932878]
[-0.14107704 -0.14304673 0.05847936 0.03562298 -0.1518798 ]
[ 0.60148682 0.60988468 -0.24932878 -0.1518798 0.64754478]]
(5, 1)
[[ 0.22469294 0.78832742 -1.13148285]
[-0.04070683 -0.74061401 -1.59838506]
[ 0.12821164 0.72892812 0.4912876 ]
[ 0.09323584 1.66090848 1.87905216]]