How to create Numpy Array and Matrix with examples | applied electronics engineering

Latest

How to create Numpy Array and Matrix with examples

By Applied Electronics - Monday, July 11, 2016 No Comments
Numpy is a python package that is specifically developed to handle array and matrices. Array is an important programming construct that allows one to store data. Matrices are mathematical constructs to easy do computation. With normal python you can also do array operation and matrix operations but with numpy you can do it more easily. That is with normal python you have to create specific loops and iteration to do for example element wise operation. This can take number of lines. But with Numpy this can be easily done is single line of code.

Here are some example of what you can do with numpy python package.

In Numpy you create an 1D, 2D array by using the following command.

import numpy as np

A1 = np.array([1,2,3])
print(A1)
 
output:
[1 2 3]

A2 = np.array([[1,2,3],[4,5,6]])
print(A2)
 
output:
[[1 2 3]
 [4 5 6]] 

 In the first case, A1 is a 1D array, a row array. In the second case, A2 is a 2D array.


Another way to create 1D array in Numpy is using the arange() function.
 
A3 = np.arange(3)
print(A3)
 
output:
[0 1 2]  

 The arrange() function just creates a list of numbers starting from 0.

Another function that is similar to arrange() function is the linspace() function. The linspace() function accepts multiple arguments but the most important are the starting value, the end value and the number of steps. Consider the following python numpy code:

A4 = np.linspace(0,1,10)
print(A4)
print(len(A4))

The first argument paramter is 0 which is the start, the second argument parameter is 1 which is the end and 10 is the number of points between 0 and 1. Here is the output:

[ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
0.66666667  0.77777778  0.88888889  1.        ]
10

 This linspace() is a handy function that is used for creating x-axis values in case you want to plot with matplotlib package.

Now once you have created an array you can do lots of things with the array. You can change its shape, split it, stack another array onto it, calculate statistical means, standard deviation, correlation etc from the array elements.

We showed you how to create an array with Numpy. Now let's see how to create a matrix with Numpy. There are 3 different ways in which you can create a matrix with Numpy. First is to use the mat( ) function, second is to use the matrix( ) function and the third is to use the bmat( ) function.

It is very simple to create matrix using these functions. Below are some examples.

First let's use mat( ) function to create 1D and 2D matrices.

M1 = np.mat([1,2,3])
print(M1)
 
output:
[[1 2 3]]

M2 = np.mat([[1,2],[3,4]])
print(M2)
 
output:
[[1 2]
 [3 4]] 

As you can see this is like creating array. If you want to use the matrix() function then just replace the word mat with matrix and you are done. The bmat( ) stands for blockmat and it is used to create matrix using other smaller matrices which are then called blocks.

Consider we have two matrices M3 and M4. We can create a larger matrix using the bmat() function. This is illustrated by the following numpy code:

M3 = np.matrix([1,2])
M4 = np.matrix([3,4])

M5 = np.bmat([M3,M4])
print(M5)
[[1 2 3 4]]

M6 = np.bmat([[M3],[M4]])
print(M6)
[[1 2]
 [3 4]] 

No Comment to " How to create Numpy Array and Matrix with examples "