DEV Community

Aziza Afrin
Aziza Afrin

Posted on

New to NumPy?

In python, the library which is used to create and manipulates matrices is NumPy. Basically, NumPy works with arrays. For instances array is an ordered series or arrangement of numbers/objects. NumPy stands for "Numerical Python".

Let's see how can we use Numpy to work with matrices.

First import NumPy module with running the following code.

import numpy as np
Enter fullscreen mode Exit fullscreen mode

1. NumPy Arrays

Let us create a list.

my_list=[1,2,3]
Enter fullscreen mode Exit fullscreen mode

Now call np.array to craete a NumPy array with the list.

arr=np.array(my_list)
Enter fullscreen mode Exit fullscreen mode

And type arr to see the array.

arr
Enter fullscreen mode Exit fullscreen mode

We can create this array by directly putting the values like:

arr=np.array([1,2,3])
Enter fullscreen mode Exit fullscreen mode

The array we have created was an one dimensional array. We can create two dimensional array. Let's create a three by three array.

mt=[[1,2,3],[4,5,6],[7,8,9]]
np.array(mt)
Enter fullscreen mode Exit fullscreen mode

Let's create a sequence starting at zero, step size or difference is 2, to 11

np.arange(0,11,2)
Enter fullscreen mode Exit fullscreen mode

Here, we are creating a five by five dimensional array containing only zeros:

np.zeros((5,5))
Enter fullscreen mode Exit fullscreen mode

We can also create matrics having only value 1, identity matrics etc (example are given in the notebook).

We can create array with values from a specific distribution such as standard normal distribution, uniform distribution, poisson distribution, binomial distribution, chi square distribution etc. For example we can create a two dimensional array with sample from uniform distribution(0 to 1).

np.random.rand(5,5)
Enter fullscreen mode Exit fullscreen mode

We can find the maximum or minimum values is the array.

#array returning random integers
ranarr=np.random.randint(0,50,10)
ranarr.max()
Enter fullscreen mode Exit fullscreen mode

While returning the maximum value of the array we can also find the location for the maximum value.

ranarr.argmax()
Enter fullscreen mode Exit fullscreen mode

2.NumPy Indexing and Selection

Let us create a two dimensional array first.

arr=np.array([[5,10,15],[20,25,30],[35,40,45]])
arr
Enter fullscreen mode Exit fullscreen mode

and we get
array([[ 5, 10, 15],
[20, 25, 30],
[35, 40, 45]])

If we want to get the value 10 we type

arr_2d[0][1]
Enter fullscreen mode Exit fullscreen mode

In python, indexing starts from 0, so if we want to grab except the third row we type, because third row indexed as 2.

arr_2d[:2]
Enter fullscreen mode Exit fullscreen mode

3. NumPy Operations

Let's do some arithmatic operations with numpy

ar=np.arange(1,11)
Enter fullscreen mode Exit fullscreen mode

Addition:

ar+ar
Enter fullscreen mode Exit fullscreen mode

Substraction: Substract array from a array and substract a scaler from array

ar-ar
ar-4
Enter fullscreen mode Exit fullscreen mode

Multiplication:

ar*3
Enter fullscreen mode Exit fullscreen mode

4. Universal Array Function

Universal Functions used to implement vectorization in NumPy which is more faster than iterating over elements. For example:
To find a array containing square root of each values in array we type

np.sqrt(ar)
Enter fullscreen mode Exit fullscreen mode

Taking exponential of each values of the array:

np.exp(ar)
Enter fullscreen mode Exit fullscreen mode

To find a product of arrays:

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

np.prod([a, b])

Enter fullscreen mode Exit fullscreen mode

You can practice more example at your own. The notebook link is given below. Go to the link and practice.
Notebook Link: [https://www.kaggle.com/code/azizaafrin/python-numpy]

Happy Learning!<3

Top comments (4)

Collapse
 
dendihandian profile image
Dendi Handian • Edited

Keep up for your writing.

A suggestion: You can highlight the code blocks in the article markdownguide.org/extended-syntax/...

for python code, just add py after the opening triple quote

Collapse
 
aziafrin profile image
Aziza Afrin

Thank you so much. I would try this.

Collapse
 
imrulhasan profile image
Md. Imrul Hasan

great... keep it up apu.

Collapse
 
aziafrin profile image
Aziza Afrin

Thanks for your support!