In [2]:
import numpy as np

In [3]:
L = [1,2,3]
print(L)
A = np.array([1,2,3])
print(A)


[1, 2, 3]
[1 2 3]

In [4]:
for e in L:
    print(e)

print('numpy array goes below:')

for e in A:
    print(e)


1
2
3
numpy array goes below:
1
2
3

In [5]:
L.append(4)

In [6]:
L


Out[6]:
[1, 2, 3, 4]

In [7]:
A.append(4)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-a17c4d6940c3> in <module>()
----> 1 A.append(4)

AttributeError: 'numpy.ndarray' object has no attribute 'append'

In [8]:
L = L + [5]

In [9]:
L


Out[9]:
[1, 2, 3, 4, 5]

In [10]:
A = A + [4,5]


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-6ccf893548dc> in <module>()
----> 1 A = A + [4,5]

ValueError: operands could not be broadcast together with shapes (3,) (2,) 

In [11]:
# Vector Addition

# python list
L2 = []
for e in L:
    L2.append(e+e)

print(L2)

# numpy array
A2 = A + A
print(A2)


[2, 4, 6, 8, 10]
[2 4 6]

In [12]:
print('L*2 = ', L*2)
# Scalar Multiplication

L3 = []
# python list
for e in L:
    L3.append(e*3)

print(L3)

# numpy array
A3 = A*3
print(A3)


L*2 =  [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[3, 6, 9, 12, 15]
[3 6 9]

In [15]:
A**2


Out[15]:
array([1, 4, 9])

In [16]:
np.sqrt(A)


Out[16]:
array([1.        , 1.41421356, 1.73205081])

In [17]:
np.log(A)


Out[17]:
array([0.        , 0.69314718, 1.09861229])

In [18]:
np.exp(A)


Out[18]:
array([ 2.71828183,  7.3890561 , 20.08553692])

In [19]:
# DOT Product

In [20]:
a = np.array([1,2])
b = np.array([3,4])

In [21]:
dot = 0
for e,f in zip(a,b):
    dot += e*f

In [22]:
dot


Out[22]:
11

In [23]:
np.dot(a,b)
print(dot)


11

In [24]:
a*b


Out[24]:
array([3, 8])

In [26]:
np.sum(a*b)


Out[26]:
11

In [29]:
(a*b).sum()


Out[29]:
11

In [30]:
a.dot(b)


Out[30]:
11

In [33]:
amag = np.sqrt(np.sum(np.square(a)))

In [34]:
amag


Out[34]:
2.23606797749979

In [35]:
amag = np.linalg.norm(a)
print(amag)


2.23606797749979

In [37]:
cosangle = a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cosangle)


0.9838699100999074

In [39]:
angle = np.arccos(cosangle)
print(angle)


0.17985349979247847

In [40]:
######### Speed comparision:

In [47]:
import numpy as np
from datetime import datetime

a = np.random.randn(100)
b = np.random.randn(100)
T = 100000

#custom dot product using python list
def slow_dot_product(a,b):
    result = 0
    for e,f in zip(a,b):
        result += e*f
    
    return result

t0 = datetime.now()
for t in range(T):
    slow_dot_product(a,b)

dt1 = datetime.now() - t0

t0 = datetime.now()
for t in range(T):
    a.dot(b)

dt2 = datetime.now() - t0

print('speedfactor: dt1/dt2 = ', dt1.total_seconds() / dt2.total_seconds())


speedfactor: dt1/dt2 =  38.20582726694466

In [48]:
# Matrix and nd-arrays

M = np.array([[1,2],[3,4]])
L = [[1,2],[3,4]]

In [49]:
M


Out[49]:
array([[1, 2],
       [3, 4]])

In [50]:
L


Out[50]:
[[1, 2], [3, 4]]

In [51]:
M[0][0]


Out[51]:
1

In [52]:
M[0,0]


Out[52]:
1

In [53]:
L[0][0]


Out[53]:
1

In [54]:
L[0,0]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-8cddded1c44f> in <module>()
----> 1 L[0,0]

TypeError: list indices must be integers or slices, not tuple

In [56]:
M2 = np.matrix([[1,2],[3,4]])

In [57]:
M2


Out[57]:
matrix([[1, 2],
        [3, 4]])

In [58]:
M2[0][0]


Out[58]:
matrix([[1, 2]])

In [59]:
M2[0,0]


Out[59]:
1

In [60]:
M2[0]


Out[60]:
matrix([[1, 2]])

In [65]:
print(M)
print('\ntranspose:')
print(M.T)


[[1 2]
 [3 4]]

transpose:
[[1 3]
 [2 4]]

In [66]:
print(M2)
print('\ntranspose:')
print(M2.T)


[[1 2]
 [3 4]]

transpose:
[[1 3]
 [2 4]]

In [77]:
print(M[0])
print('\ntranspose:')
print(len([M[0]]))

print('\ntranspose:')
print(np.transpose([M[0]]).shape)


[1 2]

transpose:
1

transpose:
(2, 1)

In [78]:
np.zeros(3)


Out[78]:
array([0., 0., 0.])

In [79]:
np.zeros((3,7))


Out[79]:
array([[0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0.]])

In [80]:
np.ones(10)


Out[80]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [81]:
np.ones(10,10)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-81-fec025371b7c> in <module>()
----> 1 np.ones(10,10)

/usr/local/lib/python3.5/dist-packages/numpy/core/numeric.py in ones(shape, dtype, order)
    186 
    187     """
--> 188     a = empty(shape, dtype, order)
    189     multiarray.copyto(a, 1, casting='unsafe')
    190     return a

TypeError: data type not understood

In [82]:
np.ones((10,10))


Out[82]:
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

In [83]:
np.random.random((10,10))


Out[83]:
array([[0.54770378, 0.68139432, 0.38726032, 0.07036305, 0.85445766,
        0.65222472, 0.43732202, 0.71330118, 0.42599428, 0.25474694],
       [0.11488229, 0.77723463, 0.70914966, 0.28374595, 0.52987378,
        0.61024798, 0.47821993, 0.24659613, 0.92993732, 0.05136316],
       [0.30345791, 0.38519891, 0.42684806, 0.28145062, 0.56021622,
        0.43887946, 0.66452444, 0.86042457, 0.5540225 , 0.216286  ],
       [0.48534116, 0.77592412, 0.68310157, 0.8998669 , 0.6567857 ,
        0.3139594 , 0.67863029, 0.18928358, 0.95853442, 0.34086236],
       [0.58385933, 0.86877151, 0.16076   , 0.69423102, 0.53907128,
        0.74630821, 0.32849175, 0.06198872, 0.65279674, 0.58498915],
       [0.39309197, 0.68738218, 0.31562567, 0.20046428, 0.42308754,
        0.32016282, 0.64979437, 0.91294629, 0.14136401, 0.40598342],
       [0.2551902 , 0.87903491, 0.23778312, 0.29911026, 0.49826995,
        0.78883124, 0.75906683, 0.72735567, 0.72995504, 0.34788448],
       [0.05265112, 0.44987218, 0.06395436, 0.74681219, 0.95631985,
        0.74065846, 0.56759951, 0.78587909, 0.18137329, 0.17669407],
       [0.76977751, 0.14579109, 0.99061136, 0.54550947, 0.10677015,
        0.17178508, 0.648104  , 0.31135768, 0.83723642, 0.64196938],
       [0.35611258, 0.21296819, 0.55160659, 0.40308894, 0.4828597 ,
        0.15394885, 0.84925287, 0.61982114, 0.62687965, 0.9142046 ]])

In [85]:
np.random.random((10,10))


Out[85]:
array([[0.24667749, 0.6558834 , 0.93280596, 0.8237273 , 0.06287493,
        0.80211469, 0.38151742, 0.12687166, 0.31075885, 0.70977937],
       [0.36247738, 0.71308852, 0.86889834, 0.26452117, 0.00251282,
        0.8324485 , 0.14033495, 0.65636436, 0.79874838, 0.35592245],
       [0.43206716, 0.29648772, 0.79512456, 0.51663656, 0.55494342,
        0.74995454, 0.55105281, 0.29711927, 0.68214661, 0.11565221],
       [0.35898435, 0.57069877, 0.46627688, 0.91274871, 0.56949607,
        0.37543208, 0.47932182, 0.32145609, 0.54549214, 0.91852784],
       [0.42976094, 0.80962824, 0.16925084, 0.749154  , 0.26198632,
        0.25383814, 0.16982518, 0.70272667, 0.16354702, 0.28569073],
       [0.87500226, 0.96249286, 0.46332236, 0.64542017, 0.24895339,
        0.60345013, 0.52983456, 0.6679105 , 0.80807816, 0.94407928],
       [0.02914349, 0.37179657, 0.6025306 , 0.51000609, 0.68602474,
        0.28044684, 0.22979896, 0.55441931, 0.24417636, 0.02882097],
       [0.29184994, 0.73758547, 0.42160747, 0.69655305, 0.44869556,
        0.72910167, 0.75609117, 0.55487032, 0.14957755, 0.50072925],
       [0.6569574 , 0.85296341, 0.50965996, 0.51556106, 0.65542085,
        0.15131459, 0.4066001 , 0.26094095, 0.76782609, 0.30592357],
       [0.67118123, 0.74285744, 0.93350026, 0.66021306, 0.11636612,
        0.91262357, 0.871183  , 0.98913301, 0.97370129, 0.70213219]])

In [89]:
G = np.random.randn(10,10)
print(G)
print(G.mean())
print(G.var())


[[-0.01822716  0.36909166 -0.630191    0.67436458  0.86552565  0.48186928
   0.52021347  0.24225279  0.32779549 -0.00581185]
 [ 1.51050425 -0.57218444  2.05313442  1.18990112 -0.26595754  0.83358421
  -0.93171218  0.11457257 -0.35104368 -0.99054611]
 [ 1.09276997 -0.01707079 -0.08923727  1.06782624 -0.69468388 -0.9064498
   0.19125353 -1.03590567  0.24177702 -1.10820159]
 [-1.42117414 -0.20172442 -0.40044444 -0.4119652  -1.50817233 -0.59583188
  -0.47728095  0.04255614  0.14988432 -0.90515083]
 [-0.29050998 -0.30118961  1.68059145 -1.16343397 -1.16031744  0.15981583
   1.74772295  1.33546065 -0.6876939  -1.51179134]
 [ 0.03189812  0.87828171  1.86513328  0.29447355 -0.69776794  0.70954355
   0.12096932  1.6787779  -0.26503716 -0.25730509]
 [-0.40246723  0.08494707  0.01007295  1.37562207 -0.67651397 -0.77348383
   1.158959   -1.32326408  0.49455299  0.8467344 ]
 [ 0.44669018 -0.02296609 -0.46408611 -1.50040481 -0.04267929 -0.48654401
   0.07308839  0.18919737  0.09115942  0.45288304]
 [ 0.66105966 -1.52575881 -1.09198008  0.47613708  0.95969261 -0.09691981
  -0.56740186 -0.14812388  0.46479577  0.0618949 ]
 [-0.3258352   2.59085034 -2.07853199  0.91647478  0.1651227   0.87112383
  -1.76735999  0.25664279  0.63994668 -0.2870175 ]]
0.023038409008757248
0.7955617945233981

In [93]:
A = np.random.randn(3,3)
Ainv = np.linalg.inv(A)
print(A)
print('--------------')
print(Ainv)
print('--------------')
print(A.dot(Ainv))
print('--------------')
print(Ainv.dot(A))
print('--------------')
print(np.diag(A))
print('--------------')
print(np.diag([1,2,3]))


[[-2.94728088  0.79195317  1.30310378]
 [ 0.42096328  0.20206389 -0.4751916 ]
 [ 0.19369653  1.52017286 -0.37127562]]
--------------
[[-0.60266846 -2.11794553  0.59548854]
 [-0.05981555 -0.78373832  0.79315738]
 [-0.55932776 -4.31392833  0.86480332]]
--------------
[[ 1.00000000e+00 -8.35961664e-16 -1.08948710e-16]
 [ 6.50261751e-18  1.00000000e+00 -2.79900433e-17]
 [-2.70740337e-18  1.19429984e-16  1.00000000e+00]]
--------------
[[ 1.00000000e+00 -3.49172935e-17 -8.13785306e-17]
 [ 7.57627905e-18  1.00000000e+00  9.67556967e-18]
 [-5.18028715e-17 -4.71873572e-17  1.00000000e+00]]
--------------
[-2.94728088  0.20206389 -0.37127562]
--------------
[[1 0 0]
 [0 2 0]
 [0 0 3]]

In [105]:
a = np.random.randn(3)

In [106]:
a


Out[106]:
array([ 1.01495298, -1.04632945,  0.28614042])

In [107]:
b = np.random.randn(3)
b


Out[107]:
array([ 1.30572782, -0.92157189, -0.04920838])

In [108]:
np.outer(a,b)


Out[108]:
array([[ 1.32525235, -0.93535214, -0.0499442 ],
       [-1.36622148,  0.96426781,  0.05148818],
       [ 0.37362151, -0.26369897, -0.01408051]])

In [109]:
np.inner(a,b)


Out[109]:
2.275439649761211

In [110]:
a.dot(b)


Out[110]:
2.275439649761211

In [111]:
np.dot(a,b)


Out[111]:
2.275439649761211

In [112]:
np.outer(b,a)


Out[112]:
array([[ 1.32525235, -1.36622148,  0.37362151],
       [-0.93535214,  0.96426781, -0.26369897],
       [-0.0499442 ,  0.05148818, -0.01408051]])

In [113]:
np.outer(a,b)


Out[113]:
array([[ 1.32525235, -0.93535214, -0.0499442 ],
       [-1.36622148,  0.96426781,  0.05148818],
       [ 0.37362151, -0.26369897, -0.01408051]])

In [114]:
np.trace(np.outer(a,b))


Out[114]:
2.275439649761211

In [115]:
np.diag(np.outer(a,b)).sum()


Out[115]:
2.275439649761211

In [119]:
# """
# x1 + x2 = 2200
# 1.5 * x1 + 4.0 * x2 = 5050
# """

A = np.array([[1,1], [1.5, 4]])
b = np.array([2200, 5050])

m_sol = np.linalg.solve(A,b)

print(m_sol)


[1500.  700.]

In [121]:
m_sol = np.linalg.inv(A).dot(b)
print(m_sol)


[1500.  700.]

In [ ]: