[razvan@ohio data]$ python3 Python 3.8.5 (default, Sep 4 2020, 02:22:02) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> a = np.array([1, 2, 3, 4]) >>> a array([1, 2, 3, 4]) >>> a.shape (4,) >>> a.ndim 1 >>> len(a.shape) 1 >>> list(range(8)) [0, 1, 2, 3, 4, 5, 6, 7] >>> a = np.array(list(range(8))) >>> a array([0, 1, 2, 3, 4, 5, 6, 7]) >>> b = np.arange(8) >>> b array([0, 1, 2, 3, 4, 5, 6, 7]) >>> a = np.array([1, 2, 3, 4]) >>> a array([1, 2, 3, 4]) >>> b array([0, 1, 2, 3, 4, 5, 6, 7]) >>> b.shape (8,) >>> b.reshape(4,2) array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> b array([0, 1, 2, 3, 4, 5, 6, 7]) >>> b = b.reshape(4,2) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> a array([1, 2, 3, 4]) >>> b.shape (4, 2) >>> b.ndim 2 >>> np.column_stack((a, b)) array([[1, 0, 1], [2, 2, 3], [3, 4, 5], [4, 6, 7]]) >>> test = np.loadtxt('test.txt') >>> test.shape (31, 2) >>> test[:5] array([[ 2227., 356700.], [ 2596., 233000.], [ 2476., 233000.], [ 2556., 299000.], [ 2391., 325000.]]) >>> test[:5,1] array([356700., 233000., 233000., 299000., 325000.]) >>> test[:5,0] array([2227., 2596., 2476., 2556., 2391.]) >>> test[:5,:] array([[ 2227., 356700.], [ 2596., 233000.], [ 2476., 233000.], [ 2556., 299000.], [ 2391., 325000.]]) >>> t = test[:,1] >>> t[:5] array([356700., 233000., 233000., 299000., 325000.]) >>> t.shape (31,) >>> X = test[:,0] >>> X[:5] array([2227., 2596., 2476., 2556., 2391.]) >>> w = np.array([1, 100]) >>> w[0] 1 >>> w[1] 100 >>> X.size 31 >>> X.shape[1] Traceback (most recent call last): File "", line 1, in IndexError: tuple index out of range >>> X.shape[0] 31 >>> ones = np.ones(X.shape[0]) >>> ones[:5] array([1., 1., 1., 1., 1.]) >>> np.column_stack((ones,X)) array([[1.000e+00, 2.227e+03], [1.000e+00, 2.596e+03], [1.000e+00, 2.476e+03], [1.000e+00, 2.556e+03], [1.000e+00, 2.391e+03], [1.000e+00, 1.580e+03], [1.000e+00, 3.157e+03], [1.000e+00, 3.088e+03], [1.000e+00, 1.568e+03], [1.000e+00, 2.774e+03], [1.000e+00, 1.888e+03], [1.000e+00, 1.800e+03], [1.000e+00, 1.876e+03], [1.000e+00, 1.800e+03], [1.000e+00, 1.614e+03], [1.000e+00, 1.568e+03], [1.000e+00, 2.425e+03], [1.000e+00, 2.462e+03], [1.000e+00, 3.282e+03], [1.000e+00, 2.741e+03], [1.000e+00, 2.656e+03], [1.000e+00, 2.606e+03], [1.000e+00, 1.506e+03], [1.000e+00, 1.614e+03], [1.000e+00, 4.224e+03], [1.000e+00, 3.000e+03], [1.000e+00, 1.884e+03], [1.000e+00, 3.170e+03], [1.000e+00, 3.396e+03], [1.000e+00, 2.738e+03], [1.000e+00, 3.712e+03]]) >>> newX = np.column_stack((ones,X)) >>> newX.shape (31, 2) >>> w array([ 1, 100]) >>> newX.dot(w) array([222701., 259601., 247601., 255601., 239101., 158001., 315701., 308801., 156801., 277401., 188801., 180001., 187601., 180001., 161401., 156801., 242501., 246201., 328201., 274101., 265601., 260601., 150601., 161401., 422401., 300001., 188401., 317001., 339601., 273801., 371201.]) >>> @ File "", line 1 @ ^ SyntaxError: invalid syntax >>> newX @ w array([222701., 259601., 247601., 255601., 239101., 158001., 315701., 308801., 156801., 277401., 188801., 180001., 187601., 180001., 161401., 156801., 242501., 246201., 328201., 274101., 265601., 260601., 150601., 161401., 422401., 300001., 188401., 317001., 339601., 273801., 371201.]) >>> newX * w array([[1.000e+00, 2.227e+05], [1.000e+00, 2.596e+05], [1.000e+00, 2.476e+05], [1.000e+00, 2.556e+05], [1.000e+00, 2.391e+05], [1.000e+00, 1.580e+05], [1.000e+00, 3.157e+05], [1.000e+00, 3.088e+05], [1.000e+00, 1.568e+05], [1.000e+00, 2.774e+05], [1.000e+00, 1.888e+05], [1.000e+00, 1.800e+05], [1.000e+00, 1.876e+05], [1.000e+00, 1.800e+05], [1.000e+00, 1.614e+05], [1.000e+00, 1.568e+05], [1.000e+00, 2.425e+05], [1.000e+00, 2.462e+05], [1.000e+00, 3.282e+05], [1.000e+00, 2.741e+05], [1.000e+00, 2.656e+05], [1.000e+00, 2.606e+05], [1.000e+00, 1.506e+05], [1.000e+00, 1.614e+05], [1.000e+00, 4.224e+05], [1.000e+00, 3.000e+05], [1.000e+00, 1.884e+05], [1.000e+00, 3.170e+05], [1.000e+00, 3.396e+05], [1.000e+00, 2.738e+05], [1.000e+00, 3.712e+05]]) >>> a array([1, 2, 3, 4]) >>> a * a array([ 1, 4, 9, 16]) >>> c = np.array([1, -1, 2, -2]) >>> a * c array([ 1, -2, 6, -8]) >>> a array([1, 2, 3, 4]) >>> c array([ 1, -1, 2, -2]) >>> 2 * a array([2, 4, 6, 8]) >>> d = np.array([2, 2, 2, 2]) >>> d * a array([2, 4, 6, 8]) >>> a / 2 array([0.5, 1. , 1.5, 2. ]) >>> a / 2 array([0.5, 1. , 1.5, 2. ]) >>> w array([ 1, 100]) >>> w * w array([ 1, 10000]) >>> np.sum(w * w) 10001 >>> w.dot(w) 10001 >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> d array([2, 2, 2, 2]) >>> d[0] = 1 >>> d[2] = -1 >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> d array([ 1, 2, -1, 2]) >>> b.shape (4, 2) >>> d.shape (4,) >>> d.dot(b) array([12, 16]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> b.ravel() array([0, 1, 2, 3, 4, 5, 6, 7]) >>> b.ravel(order = 'F') array([0, 2, 4, 6, 1, 3, 5, 7]) >>> b.ravel(order = 'C') array([0, 1, 2, 3, 4, 5, 6, 7]) >>> v = b.ravel(order = 'C') >>> v array([0, 1, 2, 3, 4, 5, 6, 7]) >>> v.reshape(4, -1) array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> v.reshape(-1, -1) Traceback (most recent call last): File "", line 1, in ValueError: can only specify one unknown dimension >>> v.reshape(-1, 2) array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> v array([0, 1, 2, 3, 4, 5, 6, 7]) >>> d array([ 1, 2, -1, 2]) >>> from np import newaxis Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'np' >>> from numpy import newaxis >>> d[:,newaxis] array([[ 1], [ 2], [-1], [ 2]]) >>> d[:,newaxis].shape (4, 1) >>> d[newaxis,:] array([[ 1, 2, -1, 2]]) >>> d[newaxis,:].shape (1, 4) >>> d.reshape(4,-1) array([[ 1], [ 2], [-1], [ 2]]) >>> np.ones(10) array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) >>> np.zeros(10) array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) >>> np.eye(10) array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]) >>> np.eye(10) + 10 array([[11., 10., 10., 10., 10., 10., 10., 10., 10., 10.], [10., 11., 10., 10., 10., 10., 10., 10., 10., 10.], [10., 10., 11., 10., 10., 10., 10., 10., 10., 10.], [10., 10., 10., 11., 10., 10., 10., 10., 10., 10.], [10., 10., 10., 10., 11., 10., 10., 10., 10., 10.], [10., 10., 10., 10., 10., 11., 10., 10., 10., 10.], [10., 10., 10., 10., 10., 10., 11., 10., 10., 10.], [10., 10., 10., 10., 10., 10., 10., 11., 10., 10.], [10., 10., 10., 10., 10., 10., 10., 10., 11., 10.], [10., 10., 10., 10., 10., 10., 10., 10., 10., 11.]]) >>> np.ones(5) array([1., 1., 1., 1., 1.]) >>> np.ones(4, 2) Traceback (most recent call last): File "", line 1, in File "/Users/razvan/opt/anaconda3/lib/python3.8/site-packages/numpy/core/numeric.py", line 192, in ones a = empty(shape, dtype, order) TypeError: Cannot interpret '2' as a data type >>> np.ones((4, 2)) array([[1., 1.], [1., 1.], [1., 1.], [1., 1.]]) >>> np.zeros((4, 2)) array([[0., 0.], [0., 0.], [0., 0.], [0., 0.]]) >>> np.ones((4, 2)) * 2 array([[2., 2.], [2., 2.], [2., 2.], [2., 2.]]) >>> d array([ 1, 2, -1, 2]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> v = np.ones(4) >>> v array([1., 1., 1., 1.]) >>> vv = no.ones((2,4)) Traceback (most recent call last): File "", line 1, in NameError: name 'no' is not defined >>> vv = np.ones((2,4)) >>> vv array([[1., 1., 1., 1.], [1., 1., 1., 1.]]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> b + vv Traceback (most recent call last): File "", line 1, in ValueError: operands could not be broadcast together with shapes (4,2) (2,4) >>> b + vv.T array([[1., 2.], [3., 4.], [5., 6.], [7., 8.]]) >>> vv.T.shape (4, 2) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> v array([1., 1., 1., 1.]) >>> b + v Traceback (most recent call last): File "", line 1, in ValueError: operands could not be broadcast together with shapes (4,2) (4,) >>> v array([1., 1., 1., 1.]) >>> v.shape (4,) >>> b + v[:,newaxis] array([[1., 2.], [3., 4.], [5., 6.], [7., 8.]]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> v array([1., 1., 1., 1.]) >>> v[:,newaxis] array([[1.], [1.], [1.], [1.]]) >>> v[:,newaxis].shape (4, 1) >>> b.shape (4, 2) >>> v.column_stack(v) Traceback (most recent call last): File "", line 1, in AttributeError: 'numpy.ndarray' object has no attribute 'column_stack' >>> np.column_stack((v,v)) array([[1., 1.], [1., 1.], [1., 1.], [1., 1.]]) >>> a = np.arange(4) * 10 >>> a array([ 0, 10, 20, 30]) >>> np.column_stack((a, a, a)) array([[ 0, 0, 0], [10, 10, 10], [20, 20, 20], [30, 30, 30]]) >>> a np.column_stack((a, a, a)) File "", line 1 a np.column_stack((a, a, a)) ^ SyntaxError: invalid syntax >>> a = np.column_stack((a, a, a)) >>> a array([[ 0, 0, 0], [10, 10, 10], [20, 20, 20], [30, 30, 30]]) >>> b = np.arange(3) >>> b array([0, 1, 2]) >>> a * b array([[ 0, 0, 0], [ 0, 10, 20], [ 0, 20, 40], [ 0, 30, 60]]) >>> b.shape (3,) >>> a.shape (4, 3) >>> c = np.arange(4) >>> c array([0, 1, 2, 3]) >>> a * c Traceback (most recent call last): File "", line 1, in ValueError: operands could not be broadcast together with shapes (4,3) (4,) >>> a * b array([[ 0, 0, 0], [ 0, 10, 20], [ 0, 20, 40], [ 0, 30, 60]]) >>> a.shape (4, 3) >>> b.shape (3,) >>> a + b array([[ 0, 1, 2], [10, 11, 12], [20, 21, 22], [30, 31, 32]]) >>> a array([[ 0, 0, 0], [10, 10, 10], [20, 20, 20], [30, 30, 30]]) >>> a = np.arange(4) + 1 >>> a array([1, 2, 3, 4]) >>> b = np.arange(7).reshape(4,-1) Traceback (most recent call last): File "", line 1, in ValueError: cannot reshape array of size 7 into shape (4,newaxis) >>> b = np.arange(8).reshape(4,-1) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> a array([1, 2, 3, 4]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> np.sum(a) 10 >>> a.sum() 10 >>> a.max() 4 >>> a.min() 1 >>> b.sum() 28 >>> b.sum(axis = 0) array([12, 16]) >>> b.sum(axis = 1) array([ 1, 5, 9, 13]) >>> b.max(axis = 1) array([1, 3, 5, 7]) >>> b.amax(axis = 1) Traceback (most recent call last): File "", line 1, in AttributeError: 'numpy.ndarray' object has no attribute 'amax' >>> b.argmax(axis = 1) array([1, 1, 1, 1]) >>> b.max() 7 >>> b.max(axis = 1) array([1, 3, 5, 7]) >>> b.argmax(axis = 1) array([1, 1, 1, 1]) >>> a array([1, 2, 3, 4]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> np.maximum(a,b) Traceback (most recent call last): File "", line 1, in ValueError: operands could not be broadcast together with shapes (4,) (4,2) >>> np.maximum(a[:,newaxis], b) array([[1, 1], [2, 3], [4, 5], [6, 7]]) >>> a array([1, 2, 3, 4]) >>> a[:2] array([1, 2]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> b.shape (4, 2) >>> a[:2].shape (2,) >>> np.maximum(a[:2], b) array([[1, 2], [2, 3], [4, 5], [6, 7]]) >>> a[:2] * b array([[ 0, 2], [ 2, 6], [ 4, 10], [ 6, 14]]) >>> a[:2] + b array([[1, 3], [3, 5], [5, 7], [7, 9]]) >>> b / a[:2] array([[0. , 0.5], [2. , 1.5], [4. , 2.5], [6. , 3.5]]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> b.mean() 3.5 >>> b.sum() / b.size() Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> b.sum() / b.size 3.5 >>> b.mean(axis = 0) array([3., 4.]) >>> b.mean(axis = 1) array([0.5, 2.5, 4.5, 6.5]) >>> b.std() 2.29128784747792 >>> b.std(axis = 0) array([2.23606798, 2.23606798]) >>> a array([1, 2, 3, 4]) >>> np.column_stack((a, a)) array([[1, 1], [2, 2], [3, 3], [4, 4]]) >>> np.vstack((a, a)) array([[1, 2, 3, 4], [1, 2, 3, 4]]) >>> np.stack((a, a)) array([[1, 2, 3, 4], [1, 2, 3, 4]]) >>> np.stack((a, a), axis = 0) array([[1, 2, 3, 4], [1, 2, 3, 4]]) >>> np.stack((a, a), axis = 1) array([[1, 1], [2, 2], [3, 3], [4, 4]]) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> np.split(b, 2) [array([[0, 1], [2, 3]]), array([[4, 5], [6, 7]])] >>> np.split(b, 2, axis = 1) [array([[0], [2], [4], [6]]), array([[1], [3], [5], [7]])] >>> [b1, b2] = np.split(b, 2) >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> b1 array([[0, 1], [2, 3]]) >>> b2 array([[4, 5], [6, 7]]) >>> b1[0,0] = -1 >>> b1 array([[-1, 1], [ 2, 3]]) >>> b2 array([[4, 5], [6, 7]]) >>> b array([[-1, 1], [ 2, 3], [ 4, 5], [ 6, 7]]) >>> from numpy import linalg as la >>> a array([1, 2, 3, 4]) >>> la.norm(a) 5.477225575051661 >>> np.sum(a*a) 30 >>> np.sqrt(np.sum(a*a)) 5.477225575051661 >>> np.sqrt(a.dot(a)) 5.477225575051661 >>> np.sqrt(a @ a) 5.477225575051661 >>> b array([[-1, 1], [ 2, 3], [ 4, 5], [ 6, 7]]) >>> b[0,0] = 0 >>> b array([[0, 1], [2, 3], [4, 5], [6, 7]]) >>> la.norm(b) 11.832159566199232 >>> la.norm(b, axis = 0) array([7.48331477, 9.16515139]) >>> la.norm(b, axis = 1) array([1. , 3.60555128, 6.40312424, 9.21954446]) >>> a array([1, 2, 3, 4]) >>> a[:, newaxis].shape (4, 1) >>> aa = a[:, newaxis].shape >>> aa @ aa.T Traceback (most recent call last): File "", line 1, in AttributeError: 'tuple' object has no attribute 'T' >>> aa = a[:, newaxis] >>> aa array([[1], [2], [3], [4]]) >>> aa @ aa.T array([[ 1, 2, 3, 4], [ 2, 4, 6, 8], [ 3, 6, 9, 12], [ 4, 8, 12, 16]]) >>> np.outer(a) Traceback (most recent call last): File "", line 1, in File "<__array_function__ internals>", line 4, in outer TypeError: _outer_dispatcher() missing 1 required positional argument: 'b' >>> np.outer(a, a) array([[ 1, 2, 3, 4], [ 2, 4, 6, 8], [ 3, 6, 9, 12], [ 4, 8, 12, 16]]) >>> la.matrix_rank(np.outer(a,a)) 1 >>> c = np.random.randint(0, 10, (4, 4)) >>> c array([[7, 5, 7, 4], [1, 6, 6, 3], [3, 3, 0, 6], [3, 9, 7, 0]]) >>> la.matrix_rank(c) 4 >>> la.eig(np.eye(3)) (array([1., 1., 1.]), array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])) >>> c array([[7, 5, 7, 4], [1, 6, 6, 3], [3, 3, 0, 6], [3, 9, 7, 0]]) >>> np.trace(c) 13 >>> qr = la.qr(c) >>> qr (array([[-0.84887469, 0.36635641, 0.38083549, 0.01261239], [-0.12126781, -0.60936669, 0.33928632, -0.70629395], [-0.36380344, 0.04970665, -0.84591815, -0.38678002], [-0.36380344, -0.70141604, -0.15579344, 0.59278242]]), array([[-8.24621125, -9.33762156, -9.21635375, -5.94212281], [ 0. , -7.98804253, -6.00161758, -0.06443454], [ 0. , 0. , 3.61101232, -2.53430799], [ 0. , 0. , 0. , -4.3891124 ]])) >>> Q = qr[0] >>> R = qr[1] >>> Q array([[-0.84887469, 0.36635641, 0.38083549, 0.01261239], [-0.12126781, -0.60936669, 0.33928632, -0.70629395], [-0.36380344, 0.04970665, -0.84591815, -0.38678002], [-0.36380344, -0.70141604, -0.15579344, 0.59278242]]) >>> R array([[-8.24621125, -9.33762156, -9.21635375, -5.94212281], [ 0. , -7.98804253, -6.00161758, -0.06443454], [ 0. , 0. , 3.61101232, -2.53430799], [ 0. , 0. , 0. , -4.3891124 ]]) >>> Q @ R array([[7.00000000e+00, 5.00000000e+00, 7.00000000e+00, 4.00000000e+00], [1.00000000e+00, 6.00000000e+00, 6.00000000e+00, 3.00000000e+00], [3.00000000e+00, 3.00000000e+00, 4.99600361e-16, 6.00000000e+00], [3.00000000e+00, 9.00000000e+00, 7.00000000e+00, 8.88178420e-16]]) >>> Q[:0] @ Q[:1] Traceback (most recent call last): File "", line 1, in ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 4) >>> Q[:0] array([], shape=(0, 4), dtype=float64) >>> Q[0] array([-0.84887469, 0.36635641, 0.38083549, 0.01261239]) >>> Q[0] @ Q[0] 1.0000000000000002 >>> Q[0] @ Q[1] 1.3877787807814457e-17 >>> np.round(Q[0] @ Q[1]) 0.0 >>> la.det(c) 1043.9999999999995 >>> from numpy import linalg as la >>> numpy.linalg.det(c) Traceback (most recent call last): File "", line 1, in NameError: name 'numpy' is not defined >>> np.linalg.det(c) 1043.9999999999995 >>> la.solve >>> la.inv(c) array([[ 0.12931034, -0.24137931, 0.03448276, 0.07758621], [-0.12356322, -0.08045977, 0.12260536, 0.19252874], [ 0.10344828, 0.20689655, -0.17241379, -0.13793103], [-0.00287356, 0.16091954, 0.08812261, -0.13505747]]) >>> # Solve the system of equations x0 + 2 * x1 = 1 and 3 * x0 + 5 * x1 = 2 >>> a = np.array([[1, 2], [3, 5]]) >>> b = np.array([1, 2]) >>> x = np.linalg.solve(a, b) >>> x array([-1., 1.]) >>> a @ x array([1., 2.]) >>>