# How to create and initialize a matrix in python using numpy ?

Daidalos October 11, 2019

To create and initialize a matrix in python, there are several solutions, some commons examples using the python module numpy:

### Create a simple matrix

Create a 1D matrix of 9 elements:

A = \left( \begin{array}{ccc}
1&7& 3& 7& 3& 6& 4& 9& 5
\end{array}\right)

>>> import numpy as np
>>> A = np.array([1,7,3,7,3,6,4,9,5])
>>> A
array([1, 7, 3, 7, 3, 6, 4, 9, 5])


Notice: the shape of the matrix A is here (9,) and not (9,1)

>>> A.shape
(9,)


it is then useful to add an axis to the matrix A using np.newaxis (ref):

>>> A = A[:, np.newaxis]
>>> A
array([[1],
       [7],
       [3],
       [7],
       [3],
       [6],
       [4],
       [9],
       [5]])
>>> A.shape
(9, 1)


Create a matrix of shape (3,3):

A = \left( \begin{array}{ccc}
4 & 7 & 6\\
1 & 2 & 5\\
9 & 3 & 8
\end{array}\right)

using numpy:

>>> A = np.array([[4,7,6],[1,2,5],[9,3,8]])
>>> A
array([[4, 7, 6],
       [1, 2, 5],
       [9, 3, 8]])
>>> A.shape
(3, 3)


Another example with a shape (3,3,2)

>>> A = np.array([[[4,1],[7,1],[6,1]],[[1,1],[2,1],[5,1]],[[9,1],[3,1],[8,1]]])
>>> A
array([[[4, 1],
        [7, 1],
        [6, 1]],

       [[1, 1],
        [2, 1],
        [5, 1]],

       [[9, 1],
        [3, 1],
        [8, 1]]])
>>> A.shape
(3, 3, 2)


### Create a matrix containing only 0

To create a matrix containing only 0, a solution is to use the numpy function zeros

A = \left( \begin{array}{ccc}
0&0& 0& 0& 0& 0& 0& 0& 0&0
\end{array}\right)

>>> A = np.zeros((10))
>>> A
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> A.shape
(10,)


Another example

A = \left( \begin{array}{ccc}
0 & 0 & 0\\
0 & 0 & 0\\
0 & 0 & 0
\end{array}\right)

>>> A = np.zeros((3,3))
>>> A
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
>>> A.shape
(3, 3)


### Create a matrix containing only 1

To create a matrix containing only 0, a solution is to use the numpy function ones

A = \left( \begin{array}{ccc}
1&1& 1& 1& 1& 1& 1& 1& 1&1
\end{array}\right)

>>> A = np.ones((10))
>>> A
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])
>>> A.shape
(10,)


Another example

A = \left( \begin{array}{ccc}
1 & 1 & 1\\
1 & 1 & 1\\
1 & 1 & 1
\end{array}\right)

>>> A = np.ones((3,3))
>>> A
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
>>> A.shape
(3, 3)


### Create a matrix from a range of numbers (using arange)

To create a matrix from a range of numbers between [1,10[ for example a solution is to use the numpy function arange

A = \left( \begin{array}{ccc}
1&2& 3& 4& 5& 6& 7& 8& 9
\end{array}\right)

>>> A = np.arange(1,10)
>>> A
array([1, 2, 3, 4, 5, 6, 7, 8, 9])


Another example with a step of 2

A = \left( \begin{array}{ccc}
1&3& 5& 7& 9
\end{array}\right)

>>> A = np.arange(1,10,2)
>>> A
array([1, 3, 5, 7, 9])


Another example

A = \left( \begin{array}{ccc}
1&6& 11& 16
\end{array}\right)

>>> A = np.arange(1,20,5)
>>> A
array([ 1,  6, 11, 16])
>>> A.shape
(4,)


It is then possible to reshape the matrix:

>>> A = A.reshape(2,2)
>>> A
array([[ 1,  6],
       [11, 16]])
>>> A.shape
(2, 2)


A = \left( \begin{array}{ccc}
1 & 6 \\
11 & 16
\end{array}\right)

### Create a matrix from a range of numbers (using linspace)

To create 20 numbers between [1,10[ a solution is to use the numpy function linspace

>>> A = np.linspace(1,10,20)
>>> A
array([  1.        ,   1.47368421,   1.94736842,   2.42105263,
         2.89473684,   3.36842105,   3.84210526,   4.31578947,
         4.78947368,   5.26315789,   5.73684211,   6.21052632,
         6.68421053,   7.15789474,   7.63157895,   8.10526316,
         8.57894737,   9.05263158,   9.52631579,  10.        ])
>>> A.shape
(20,)


### Create a matrix of random integers

To create a matrix of random integers, a solution is to use the numpy function randint. Example with a matrix of size (10,) with random integers between [0,10[

>>> A = np.random.randint(10, size=10)
>>> A
array([9, 5, 0, 2, 0, 6, 6, 6, 5, 5])
>>> A.shape
(10,)


Example with a matrix of size (3,3) with random integers between [0,10[

>>> A = np.random.randint(10, size=(3,3))
>>> A
array([[2, 4, 7],
       [7, 5, 4],
       [0, 9, 4]])
>>> A.shape
(3, 3)


Example with a matrix of size (3,3) with random integers between [0,100[

>>> A = np.random.randint(100, size=(3,3))
>>> A
array([[83, 51, 95],
       [74,  7, 70],
       [49, 18,  8]])


### Create a matrix of random floats

>>> A = A * 0.01
>>> A
array([[ 0.83,  0.51,  0.95],
       [ 0.74,  0.07,  0.7 ],
       [ 0.49,  0.18,  0.08]])
>>> type(A)
<class 'numpy.ndarray'>

>>> A.dtype
dtype('float64')


### Create a matrix of strings

Example of how to create a matrix of strings

>>> A = np.array(('Hello','Hola','Bonjour'))
>>> A
array(['Hello', 'Hola', 'Bonjour'], 
      dtype='<U7')
>>> A.dtype
dtype('<U7')


Note: the element type is here ('[HTML_REMOVED] 7

>>> A[0] = 'How are you ?'
>>> A
array(['How are', 'Hola', 'Bonjour'], 
  dtype='<U7')


it will be truncated. To fix that a solution is to change the type first:

>>> A = A.astype('<U20')
>>> A[0] = 'How are you ?'
>>> A
array(['How are you ?', 'Hola', 'Bonjour'], dtype='<U20')


### Create an identity matrix

To create an identity matrix a solution is to use the numpy function identity:

I = \left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{array}\right)

Example

>>> import numpy as np
>>> I = np.identity(3)
>>> I
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])


### References

numpy.chararray scipy doc
numpy.random.randint docs.scipy
numpy.arange docs.scipy
numpy.random.choice docs.scipy
numpy.empty docs.scipy
numpy.linspace docs.scipy
initialize a numpy array stackoverflow
How do I create character arrays in numpy? stackoverflow
How can I add new dimensions to a Numpy array? stackoverflow
numpy.identity numpy doc