NP multiplication using matmul

Posted under » Python Data Analysis on 14 July 2023

Simplest way to create ones

>>> np.ones(5)
array([1., 1., 1., 1., 1.])

>>> np.ones((5,), dtype=int)
array([1, 1, 1, 1, 1]

If you want 6s instead.

>>> C = np.ones((5,), dtype=int)
>>> D = C + 5
array([6, 6, 6, 6, 6])

2d

>>> s = (2,2)
>>> np.ones(s)
array([[1.,  1.],
       [1.,  1.]])

>>> np.ones((2, 1))
array([[1.],
       [1.]])

When you multiply arrays you get.

>>> np.array([[1,2],[3,4]]) * np.array([[1,1],[1,1]])
array([[1, 2],
       [3, 4]])

But we want to multiply using matrix instead

>>> np.matrix([[1,2],[3,4]]) * np.matrix([[1,1],[1,1]])
matrix([[3, 3],
        [7, 7]])

You can multiply arrays as matrix with matmul and get the same result as an array

>>> A = np.array([[1,2],[3,4]])
>>> B = np.array([[1,1],[1,1]])
>>> np.matmul (A, B)
array([[3, 3],
       [7, 7]])

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data