In [1]:import numpy as np a = np.arange(5) b = np.arange(9).reshape(3,3) a Out[1]:array([0, 1, 2, 3, 4]) In [2]:b Out[2]:array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [3]:a+1 Out[3]:array([1, 2, 3, 4, 5]) In [4]:a/2 Out[4]:array([ 0. , 0.5, 1. , 1.5, 2. ]) In [5]:b-2 Out[5]:array([[-2, -1, 0], [ 1, 2, 3], [ 4, 5, 6]]) In [6]:b/2 Out[6]:array([[ 0. , 0.5, 1. ], [ 1.5, 2. , 2.5], [ 3. , 3.5, 4. ]]) In [7]:a*3 Out[7]:array([ 0, 3, 6, 9, 12]) In [8]:b*4 Out[8]:array([[ 0, 4, 8], [12, 16, 20], [24, 28, 32]]) In [9]:np.exp(a) Out[9]:array([ 1. , 2.71828183, 7.3890561 , 20.08553692, 54.59815003]) In [10]:np.exp(b) Out[10]:array([[ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00], [ 2.00855369e+01, 5.45981500e+01, 1.48413159e+02], [ 4.03428793e+02, 1.09663316e+03, 2.98095799e+03]]) In [11]:np.exp Out[11]: In [12]:a = np.arange(12).reshape(3,4) b = np.arange(0,400,100) a.shape, b.shape Out[12]:((3, 4), (4,)) In [13]:a+b Out[13]:array([[ 0, 101, 202, 303], [ 4, 105, 206, 307], [ 8, 109, 210, 311]]) In [14]:a*b Out[14]:array([[ 0, 100, 400, 900], [ 0, 500, 1200, 2100], [ 0, 900, 2000, 3300]]) In [15]:c = np.arange(0,1200,100).reshape(3,4) In [16]:a+c Out[16]:array([[ 0, 101, 202, 303], [ 404, 505, 606, 707], [ 808, 909, 1010, 1111]]) In [17]:a*c Out[17]:array([[ 0, 100, 400, 900], [ 1600, 2500, 3600, 4900], [ 6400, 8100, 10000, 12100]]) In [18]:from scipy import sparse a = sparse.lil_matrix((5,5)) In [19]:a[0,0] = 1 a[1,2] = 2 a[3,4] = 3 a Out[19]:<5x5 sparse matrix of type '' with 3 stored elements in LInked List format> In [20]:a.toarray() Out[20]:array([[ 1., 0., 0., 0., 0.], [ 0., 0., 2., 0., 0.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 3.], [ 0., 0., 0., 0., 0.]]) In [21]:a = a.tocsr() In [22]:v = np.arange(5) v Out[22]:array([0, 1, 2, 3, 4]) In [23]:a.dot(v) Out[23]:array([ 0., 4., 0., 12., 0.]) In [24]:x = np.arange(15).reshape(5,3) x Out[24]:array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]]) In [25]:a.dot(x) Out[25]:array([[ 0., 1., 2.], [ 12., 14., 16.], [ 0., 0., 0.], [ 36., 39., 42.], [ 0., 0., 0.]]) In [26]:b = sparse.lil_matrix((5,5)) b[0,4] = 4 b[2,3] = 5 b[4,1] = 6 b = b.tocsr() b.toarray() Out[26]:array([[ 0., 0., 0., 0., 4.], [ 0., 0., 0., 0., 0.], [ 0., 0., 0., 5., 0.], [ 0., 0., 0., 0., 0.], [ 0., 6., 0., 0., 0.]]) In [27]:a.dot(b).toarray() Out[27]:array([[ 0., 0., 0., 0., 4.], [ 0., 0., 0., 10., 0.], [ 0., 0., 0., 0., 0.], [ 0., 18., 0., 0., 0.], [ 0., 0., 0., 0., 0.]]) In [28]:(a*b).toarray() Out[28]:array([[ 0., 0., 0., 0., 4.], [ 0., 0., 0., 10., 0.], [ 0., 0., 0., 0., 0.], [ 0., 18., 0., 0., 0.], [ 0., 0., 0., 0., 0.]]) In [29]:a.getrow(1).toarray() Out[29]:array([[ 0., 0., 2., 0., 0.]]) In [30]:a.getcol(2).toarray() Out[30]:array([[ 0.], [ 2.], [ 0.], [ 0.], [ 0.]]) In [31]:u = sparse.lil_matrix((5,1)) u[0,0] = 7 u[2,0] = 8 u = u.tocsr() a.dot(u).toarray() Out[31]:array([[ 7.], [ 16.], [ 0.], [ 0.], [ 0.]]) In [32]:np.dot(v,a) /home/kato/.pyenv/versions/3.5.1/lib/python3.5/site-packages/scipy/sparse/compressed.py:295: SparseEfficiencyWarning: Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead. "using <, >, or !=, instead.", SparseEfficiencyWarning) Out[32]:array([ <5x5 sparse matrix of type '' with 3 stored elements in Compressed Sparse Row format>, <5x5 sparse matrix of type '' with 3 stored elements in Compressed Sparse Row format>, <5x5 sparse matrix of type '' with 3 stored elements in Compressed Sparse Row format>, <5x5 sparse matrix of type '' with 3 stored elements in Compressed Sparse Row format>, <5x5 sparse matrix of type '' with 3 stored elements in Compressed Sparse Row format>], dtype=object) In [33]:v*a Out[33]:array([ 0., 0., 2., 0., 9.]) In [34]:a.T.dot(v).T Out[34]:array([ 0., 0., 2., 0., 9.])