Loading, please wait...

A to Z Full Forms and Acronyms

Introduction to NumPy-Part 1 | NumPy Python Tutorial

This article is about NumPy, Why use NumPy, how to start with NumPy, how to create ndarray , use of ndim and ndmin.

Numerical Python(NumPy) | part-1

“NumPy is a very powerful library used to perform various numerical operations or logical operations on the multidimensional array.”

Through this article, you will learn about a very powerful library named as Numpy. This article is just an introduction of NumPy
Library. So the topics that you will learn through this article are as given below:
1. What is NumPy?
2. Why use NumPy array?
3. How to start with NumPy?
4. Creating a nD array
5. Use of ndim attribute

What is NumPy?

It is a python library and is written partially in python, but most of the parts that require fast computation are written in C or C++.
it's a linear algebra library in Python.

It is used for performing mathematical and logic operations on the array.

It provides features for operation on multidimensional arrays and matrices in Python.

As we know python doesn’t have the array, it is built on the list only. So basically when we go through NumPy we need to go for indexing, slicing, and all those kind of stuff that we do with python list.

Why use NumPy ARRAY?

NumPy array again means a python list, but NumPy aims to provide an array object that is up to 50times faster than traditional python lists.

The array object in NumPy is called nD array. NumPy offers lots of features that make working with nD array very easy.
Arrays are mostly used in data science, where speed and resources are very important.
NumPy arrays are stored at a continuous place in memory unlike lists, so processes can access and manipulate them efficiently and also it's optimized to work with the latest CPU architectures.

How to start with NumPy?

installation:
syntax:       

pip install numpy

importing:
syntax:

import numpy

Now, NumPy is imported and ready to use. Let’s look at an example:

import numpy as np
print(np.__version__) #output:1.19.1

 

Creating a NumPy array | creating an nD array object:
NumPy is employed to work with arrays & the array object in NumPy is termed nD array.
To create an nD array we can use the array() function.
To create an array we can pass a list as well as a tuple or an array-like object into an array(), it will be converted into an nD array.
example:

import numpy as np
ar1 = np.array([9 ,8 ,7, 6])
ar2 = np.array((9 ,8 ,7, 6))
print(ar1) 
#output:[9 8 7 6]

print(type(ar1))
#output:<class 'numpy.ndarray'>

print(ar2) 
#output:[9 8 7 6]

print(type(ar2)) 
#output:<class 'numpy.ndarray'>

Dimensions in Arrays
One level of array depth is known as the dimension in arrays
a)  "0-D arrays, or scalars, are the elements in an array."
   

#create 0-D array with value 10
ar1= np.array(10)
print(ar1)
#output: 10

b) "1-D array, has 0-D arrays as its elements. It is also known as a uni-dimensional array "

#create 1-D array containing the values 10,20,30,40,50
ar1= np.array([10,20,30,40,50])

print(ar1)
#output:[10 20 30 40 50]

c) "2-D array has 1-D arrays as its elements. 2-D arrays are mostly used to represent matrix or 2nd order tensors."
      note: for matrix operation NumPy has a whole submodule called numpy.mat

#create 2-D array containing the values 10,20,30,40,50 and 60,70,80,90,100

ar1= np.array([[10,20,30,40,50], [60,70,80,90,100]])

print(ar1)
#output:
#[[ 10 20 30 40 50]
# [ 60 70 80 90 100]]

d) "3-D array, has 2-D arrays(matrices) as its elements. 3-D arrays are mostly used to represent 3rd order tensors."

#create 3-D array containing two 2-D array

ar1= np.array([[[10,20,30,40,50], [60,70,80,90,100]], [[11,21,31,41,51], [61,71,81,91,101]]])

print(ar1)
[[[ 10 20 30 40 50]
[ 60 70 80 90 100]]

[[ 11 21 31 41 51]
[ 61 71 81 91 101]]]

5. What is the ndim attribute?
"NumPy provides an attribute called ndim which returns an integer value that tells the dimension of the array."
for example:

a0= np.array(10)
a1= np.array([10,20,30,40,50])
a2= np.array([[10,20,30,40,50], [60,70,80,90,100]])
a3= np.array([[[10,20,30,40,50], [60,70,80,90,100]], [[11,21,31,41,51], [61,71,81,91,101]]])

print(a0.ndim)
#output: 0

print(a1.ndim)
#output: 1

print(a2.ndim)
#output: 2

print(a3.ndim)
#output: 3

#Note: Numpy array can be of any order we can specify this by passing ndmin attribute with an integer value in the array() function.
#example:

array1 =np.array(56, ndmin=5)

print(array1)
#output: [[[[[56]]]]]

print('no. of dimensions : ', array1.ndim)
#output: no. of dimensions : 5

Thanks for reading this article, in the upcoming article you will learn NumPy Indexing and Slicing. Hope you like this article.....

A to Z Full Forms and Acronyms

Related Article