In [2]:
import numpy as np
In [3]:
s = np.array(5)
In [4]:
print s
In [5]:
s.shape
Out[5]:
In [6]:
x = s + 3
In [7]:
print x
In [8]:
v = np.array([1,2,3])
print v
In [9]:
x = v[1]
print x
In [11]:
v[1:]
Out[11]:
In [12]:
m = np.array([[1,2,3], [4,5,6], [7,8,9]])
print m
In [13]:
m.shape
Out[13]:
In [14]:
print m[1][2]
In [16]:
t = np.array([[[[1],[2]],[[3],[4]],[[5],[6]]],[[[7],[8]],\
[[9],[10]],[[11],[12]]],[[[13],[14]],[[15],[16]],[[17],[17]]]])
print t
In [17]:
v = np.array([1,2,3,4])
print v
In [18]:
x = v.reshape(1,4)
print x
In [19]:
x = v.reshape(4,1)
print x
In [20]:
x = v.reshape(2,2)
print x
In [21]:
x = v[None, :]
print x
In [22]:
x = v[:, None]
print x
In [23]:
a = np.arange(6) # 1d array
print(a)
In [24]:
b = np.arange(12).reshape(4,3) # 2d array
print(b)
In [25]:
c = np.arange(24).reshape(2,3,4) # 3d array
print(c)
In [27]:
d = np.arange(24*5).reshape(2,3,4,5) # 4d array
print(d)
In [33]:
d.sum()
Out[33]:
In [28]:
print(np.arange(10000))
In [29]:
print(np.arange(10000).reshape(100,100))
In [32]:
a = np.random.random((2,3))
a
Out[32]:
In [37]:
values = [1,2,3,4,5]
print values
for i in range(len(values)):
values[i] += 5
print len(values)
print values
In [38]:
values = [1,2,3,4,5]
values = np.array(values) + 5
print values
In [39]:
values += 5
print values
In [43]:
x = np.multiply(values,2)
print x
x = values * 3
print x
In [44]:
x *= 0
print x
Element-wise Matrix Operations
In [46]:
a = np.array([[1,3],[5,7]])
print a
# displays the following result:
# array([[1, 3],
# [5, 7]])
b = np.array([[2,4],[6,8]])
print b
# displays the following result:
# array([[2, 4],
# [6, 8]])
a + b
# displays the following result
# array([[ 3, 7],
# [11, 15]])
Out[46]:
In [47]:
m = np.array([[1,2,3],[4,5,6]])
m
# displays the following result:
# array([[1, 2, 3],
# [4, 5, 6]])
Out[47]:
In [48]:
n = m * 0.25
n
# displays the following result:
# array([[ 0.25, 0.5 , 0.75],
# [ 1. , 1.25, 1.5 ]])
Out[48]:
In [49]:
m * n
# displays the following result:
# array([[ 0.25, 1. , 2.25],
# [ 4. , 6.25, 9. ]])
Out[49]:
In [50]:
np.multiply(m, n) # equivalent to m * n
# displays the following result:
# array([[ 0.25, 1. , 2.25],
# [ 4. , 6.25, 9. ]])
Out[50]:
In [51]:
a = np.array([[1,2,3,4],[5,6,7,8]])
a
# displays the following result:
# array([[1, 2, 3, 4],
# [5, 6, 7, 8]])
Out[51]:
In [52]:
a.shape
# displays the following result:
# (2, 4)
Out[52]:
In [53]:
b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
b
# displays the following result:
# array([[ 1, 2, 3],
# [ 4, 5, 6],
# [ 7, 8, 9],
# [10, 11, 12]])
Out[53]:
In [54]:
b.shape
# displays the following result:
# (4, 3)
Out[54]:
In [55]:
c = np.matmul(a, b)
c
# displays the following result:
# array([[ 70, 80, 90],
# [158, 184, 210]])
Out[55]:
In [56]:
c.shape
# displays the following result:
# (2, 3)
Out[56]:
In [57]:
a = np.array([[1,2],[3,4]])
a
# displays the following result:
# array([[1, 2],
# [3, 4]])
Out[57]:
In [58]:
np.dot(a,a)
# displays the following result:
# array([[ 7, 10],
# [15, 22]])
Out[58]:
In [59]:
a.dot(a) # you can call `dot` directly on the `ndarray`
# displays the following result:
# array([[ 7, 10],
# [15, 22]])
Out[59]:
In [60]:
np.matmul(a,a)
# array([[ 7, 10],
# [15, 22]])
Out[60]:
In [61]:
m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
m
# displays the following result:
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
Out[61]:
In [62]:
m.T
# displays the following result:
# array([[ 1, 5, 9],
# [ 2, 6, 10],
# [ 3, 7, 11],
# [ 4, 8, 12]])
Out[62]:
In [63]:
m_t = m.T
m_t[3][1] = 200
m_t
# displays the following result:
# array([[ 1, 5, 9],
# [ 2, 6, 10],
# [ 3, 7, 11],
# [ 4, 200, 12]])
Out[63]:
In [64]:
m
# displays the following result:
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 200],
# [ 9, 10, 11, 12]])
Out[64]:
A real use case
In [65]:
inputs = np.array([[-0.27, 0.45, 0.64, 0.31]])
inputs
# displays the following result:
# array([[-0.27, 0.45, 0.64, 0.31]])
Out[65]:
In [66]:
inputs.shape
# displays the following result:
# (1, 4)
Out[66]:
In [67]:
weights = np.array([[0.02, 0.001, -0.03, 0.036], \
[0.04, -0.003, 0.025, 0.009], [0.012, -0.045, 0.28, -0.067]])
In [68]:
weights
# displays the following result:
# array([[ 0.02 , 0.001, -0.03 , 0.036],
# [ 0.04 , -0.003, 0.025, 0.009],
# [ 0.012, -0.045, 0.28 , -0.067]])
Out[68]:
In [ ]:
weights.shape
# displays the following result:
# (3, 4)
In [70]:
np.matmul(inputs, weights.T)
# displays the following result:
# array([[-0.01299, 0.00664, 0.13494]])
Out[70]:
In [71]:
np.matmul(weights, inputs.T)
# displays the following result:
# array([[-0.01299],#
# [ 0.00664],
# [ 0.13494]])
Out[71]:
In [ ]: