Attend FREE Webinar on Data Science for Career Growth Register Now
Digital Vidya's 10th Anniversary Celebrations Offer
  • This field is for validation purposes and should be left unchanged.

A Complete Guide To Working With Numpy Matrix

 / 
A Complete Guide To Working With Numpy Matrix

When people start working with tools like NLP(Natural Language Processing), Python is the most suitable programming language. Initially, people start working on NLP using default python lists. However, as time passes most people switch over to the NumPy matrix.

NumPy matrices are important because as you begin bigger experiments that use more data, default python lists are not adequate. Python lists are not ideal for optimizing space and use up too much RAM. This is where the NumPy matrix comes in.

What Is A NumPy Matrix?

NumPy stands for Numerical Python. It is a Python package which forms the core library for scientific computing. The NumPy matrix contains an n-dimensional array object which is extremely powerful and allows for the integration of C and C++.

The NumPy matrix is also used in things like linear algebra and random number capability. NumPy is also used widely as a multi-dimensional container for generic data. Here’s a video that covers the basics of the NumPy matrix.

How do I Install NumPy?

You need to go to the command prompt and type “pip install NumPy”. This will begin the installation process. Once the installation is complete you can go to your IDE(Integrated Development Environment), for instance, PyCharm, and then import NumPy by simply typing “import NumPy as Np”.

What Is A NumPy Array?

To understand how the Python NumPy matrix we first need to understand the multi-dimensional NumPy array. A NumPy array is said to be two dimensional because it has both rows and columns. The example below shows a NumPy matrix with 3 columns and 4 rows.

NumPy Matrix Source - CloudFront

What is Multi-dimensional Array?

Here is how the Python array can be implemented in an IDE like PyCharm.

Single-Dimensional NumPy Array:

1

2

3

import NumPy as np

a=np.array([1,2,3])

print(a)

Output – [1 2 3]

Multi-Dimensional Array:

1

2

a=np.array([(1,2,3),(4,5,6)])

print(a)

O/P – [[ 1 2 3]
[4 5 6]]

Python NumPy Matrix vs Python List

Here’s why the NumPy matrix is preferred to Python Data lists for more complex operations.

(i) The NumPy matrix consumes much lesser memory than the list. This makes it a better choice for bigger experiments.

(ii) NumPy is much faster than list when it comes to execution.

(iii) NumPy has also been designed in a way that makes it more convenient to work within the long run.

Some Important Operations For NumPy Matrices

NumPy arrays work in a way that is similar to the arrays used in C. In other words, you create a NumPy matrix in advance, and then just fill it. You shouldn’t merge or append arrays in NumPy because NumPy will create just one array in the size of the contents of the arrays being merged. It will then just copy the contents on to this array.

Register For a
Free Webinar

Date: 21st Nov, 2019 (Thursday)
Time: 3 PM (IST/GMT +5:30)
  • This field is for validation purposes and should be left unchanged.

Here are some of the techniques you can use to manipulate NumPy matrices.

1. Create ndarray

Some methods to create NumPy matrix  include:

(i) Creating a NumPy matrix in the same shape as a different array. This uses  NumPy.empty_like():

# Creating ndarray from list
c = np.array([[1., 2.,],[1., 2.]])

# Creating new array in the shape of c, filled with 0
d = np.empty_like(c)

(ii) Casting the NumPy matrix from the Python list using NumPy.asarray() :

import NumPy as np

list = [1, 2, 3]
c = np.asarray(list)

(iii) Create an ndarray in whichever size you require. The array can be filled with ones, zeroes, or random values.

# Array items as ndarray
c = np.array([1, 2, 3])

# A 2×2 2d array shape for the arrays in the format (rows, columns)
shape = (2, 2)

# Random values
c = np.empty(shape)

d = np.ones(shape)
e = np.zeros(shape)

2. Slicing A NumPy Matrix

At times, you don’t need to use the entire NumPy matrix. You may need only some columns and rows from the 2d matrix. This is known as slicing. Here are some examples.

a = np.asarray([[1,1,2,3,4], # 1st row
               [2,6,7,8,9], # 2nd row
               [3,6,7,8,9], # 3rd row
               [4,6,7,8,9], # 4th row
               [5,6,7,8,9]  # 5th row
             ])

b = np.asarray([[1,1],
               [1,1]])

# Select row in the format a[start:end], if start or end omitted it means all range.
y = a[:1]  # 1st row
y = a[0:1] # 1st row
y = a[2:5] # select rows from 3rd to 5th row

# Select column in the format a[start:end, column_number]
x = a[:, 1] # -1 means first from the end
x = a[:,1:3] # select cols from 2nd col until 3rd

NumPy Matrix Source CloudFront

Slicing NumPy Array

3. Merge arrays

As discussed, merging NumPy matrices does not always go well. This is because NumPy simply creates a big array and then copies the content of the arrays into it. A better approach would be to create a NumPy matrix of the size you need and fill it up yourself.

However, merging is unavoidable in some cases. In such situations, you can use these functions:

(i) Concatenate

1d arrays:

a = np.array([1, 2, 3])
b = np.array([5, 6])
print np.concatenate([a, b, b])  
# >>  [1 2 3 5 6 5 6]

2d arrays:

a2 = np.array([[1, 2], [3, 4]])

# axis=0 – concatenate along rows
print np.concatenate((a2, b), axis=0)
# >>   [[1 2]
#       [3 4]
#       [5 6]]

# axis=1 – concatenate along columns, but first b needs to be transposed:
b.T
#>> [[5]
#    [6]]
np.concatenate((a2, b.T), axis=1)
#>> [[1 2 5]
#    [3 4 6]]

(ii) Append – Append function allows you to add values to the end of a NumPy matrix

1d arrays:

# 1d arrays
print np.append(a, a2)
# >> [1 2 3 1 2 3 4]

print np.append(a, a)
# >> [1 2 3 1 2 3]

2d arrays. For 2D arrays, both the arrays need to match the shape of the rows.

print np.append(a2, b, axis=0)
# >> [[1 2]
#     [3 4]
#     [5 6]]

print np.append(a2, b.T, axis=1)
# >> [[1 2 5]
#     [3 4 6]]

(iii) Hstack (stack horizontally) and vstack (stack vertically)

1d arrays:

print np.hstack([a, b])
# >> [1 2 3 5 6]

print np.vstack([a, a])
# >> [[1 2 3]
#     [1 2 3]]

2d arrays:

print np.hstack([a2,a2]) # arrays must match shape
# >> [[1 2 1 2]
#     [3 4 3 4]]

print np.vstack([a2, b])
# >> [[1 2]
#     [3 4]
#     [5 6]]

(iv) linspace

This NumPy operation returns evenly spaced numbers. These numbers are returned over a specified interval. Here’s an example:

1

2

3

import NumPy as np

a=np.linspace(1,3,10)

print(a)

Output – [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111 2.33333333 2.55555556 2.77777778 3

(v) Max/ Min

We can use this function to find the minimum, maximum, and sum of a Python NumPy matrix. Here’s an example.

1

2

3

4

5

import NumPy as np

a= np.array([1,2,3])

print(a.min())

print(a.max())

print(a.sum())

Output – 1 3 6

(vi) Square Root & Standard Deviation

Since NumPy matrices play a key role in scientific computing, various mathematical functions can be performed using NumPy. These operations include important ones like square root and standard deviation. As you can see in the example below, the square root of each element is available in the output. Moreover, the standard deviation, that is the variation of each element from the mean of the NumPy matrix, has also been printed.

1

2

3

4

import NumPy as np

a=np.array([(1,2,3),(3,4,5,)])

print(np.sqrt(a))

print(np.std(a))

Output – [[ 1. 1.41421356 1.73205081]
[ 1.73205081 2. 2.23606798]]
1.29099444874

Python Numpy Special Functions

There are many mathematical functions like sine, tan, cos, log, etc that you can use in NumPy.

For instance, we can use NumPy to plot the graph for the sine function. We import a module called Matplotlib to do this. Here’s how the graph gets plotted on NumPy.

1

2

3

4

5

6

import NumPy as np

import matplotlib.pyplot as plt

x= np.arange(0,3*np.pi,0.1)

y=np.sin(x)

plt.plot(x,y)

plt.show()

Output –

NumPy Matrix Source - CloudFront

Output

Here’s another example. This time we’re plotting the tan function using NumPy.

1

2

3

4

5

6

import NumPy as np

import matplotlib.pyplot as plt

x= np.arange(0,3*np.pi,0.1)

y=np.tan(x)

plt.plot(x,y)

plt.show()

Output –

NumPy Matrix Source - CloudFront

Output

Conclusion

NumPy matrices allow for a number of complex operations that you can get in this Numpy Tutorial.

A Python NumPy matrix is also much superior to default Python lists because it is faster, and uses lesser space. We have only discussed a limited list of operations that can be done using NumPy. There is a much broader list of operations that are possible which can be easily executed with these Python Tools. You will be able to move towards more advanced NumPy operations once you’ve mastered the basics.

Are you also inspired by the opportunities provided by Python? You may also enroll for a Python Data Science Course for more lucrative career options in Data Science using Python.





There are 4 comments


  • 9 months ago

    John Wick   /   Reply

    This blog was very very helpful and informative many of my doubts regarding NumPy Matrix are cleared.
    Thank You

    • 9 months ago

      Niharika Mahendra   /   Reply

      Hi John,

      Thanks for appreciating our efforts.

  • 9 months ago

    Aaanami Panday   /   Reply

    Nice post. Thanks for sharing.

    • 9 months ago

      Niharika Mahendra   /   Reply

      Thanks Aaanami

Your Comment

Your email address will not be published.