Cyberithub

Learn NumPy in Python: ndarray or N-dimensional array with Best Examples

Advertisements

In this article, we will discuss about another important module called NumPy in Python. If you are a programmer or developer then you might be aware of concepts of array and its uses in different programming languages. Python has a powerful array library package called NumPy which can be used by Scientists and Researchers in the field of Linear Algebra, Fourier Transform, matrices and in many other places. It was created by Travis Oliphant in 2005 as an open source project. We will the uses of NumPy in Python in great detail with best working examples.

Learn NumPy in Python: ndarray or N-dimensional array with Best Examples

Learn NumPy in Python

Also Read: Python Pandas Tutorial: Series and Data Frame Explained with Best Examples

Numpy in python is used to create and work with multi-dimensional arrays(An array that contains 1-D arrays as it's elements are called multi-dimensional array). Containers like list, set, tuple also servers the purpose of creating arrays. But they are slow in processing when dealing with large data set. Hence Numpy is used to create array objects like list does. Numpy array object is called as 'ndarray'.

Below example demonstrate on how to create numpy array objects. We have used list, tuple and dictionary to convert them into numpy arrays(i.e ndarray). Also, One can refer to the source code of Numpy available in Git Repo.

Example

import numpy as np

print(f"Ndarray created from list: {type(np.array([1,2,'S']))}")
print(f"Ndarray created from tuple: {type(np.array(('R', 3,4)))}")
print(f"Ndarray created from dictionary: {type(np.array({1:'S', 2:'R'}))}")

Output

Numpy list created: <class 'numpy.ndarray'>
Numpy tuple created: <class 'numpy.ndarray'>
Numpy dictionary created: <class 'numpy.ndarray'>

array() -> this function is used to create Numpy ndarray objects.

type() -> this function tells about the type of object that has been passed as an argument to the array() function.

In example above, type of array passed is 'numpy.ndarray'.

1. Install Numpy in PyCharm

Follow below steps in Pycharm IDE to install the module:-

  • Go to File -> settings
  • select project where you want to install Pandas library
  • select project interpreter
  • click on '+' symbol at extreme right side
  • search for the library
  • click on 'Install Package'

2. Import Numpy

Numpy library can be imported using keyword 'import'. It can be imported in 2 ways.

import numpy

or

import numpy as np(alias)

3. Check  Numpy Version Installed

import numpy as np
print(f"Numpy Version Installed is: {np.__version__}")

Output

Numpy Version Installed is: 1.20.2

4. Create Multi-dimensional Numpy Arrays

Numpy allows to create n dimensional arrays which helps while working with large data sets. Below example shows the creation of 1D, 2D and 3D array. Similarly any dimension of array can be created according to the requirement.

import numpy as np

scalar_array = np.array(20)
one_array = np.array([2,4,6,8])
two_array = np.array([[1,4,5], [2,3,8]])

print("Scalar array created:", scalar_array)
print("1D array created:", one_array)
print("2D array created", two_array)

Output

Scalar array created: 20
1D array created: [2 4 6 8]
2D array created [[1 4 5]
[2 3 8]]

5. Numpy Functions

arange() This function returns numpy array. It takes start, stop index and step size as user defined parameter. By default element starts from 0 with step size 1 if not specified.

Syntax :

arange([start,] stop[, step,][, dtype])

Example

import numpy

print(f"Array created of size 10: {numpy.arange(10)}")
print(f"Array created with start and stop interval: {numpy.arange(2, 8)}")
print(f"Array created with start, stop and step size passed: {numpy.arange(2, 15, 3)}")

Output

Array created of size 10: [0 1 2 3 4 5 6 7 8 9]
Array created with start and stop interval: [2 3 4 5 6 7]
Array created with start, stop and step size passed: [ 2  5  8 11 14]

slice() :  This function returns elements of an array based on different user inputs provided as parameter. Similar to like arange() function, slice() also takes start, stop index and step size as parameter to slice the array and create slice object. Below example demonstrate the function usage.

Syntax:

slice(start, end, step)

Example

import numpy

array = numpy.arange(1,10,1)
a = slice(2)
b = slice(2, 6)
c = slice(2, 10, 2)

print(f"Array Created: {array}")
print(f"Sliced Array: {array[a]}")
print(f"Sliced Array with start stop index: {array[b]}")
print(f"Sliced Array with start, stop and step size: {array[c]}")

Output

Array Created: [1 2 3 4 5 6 7 8 9]
Sliced Array: [1 2]
Sliced Array with start stop index: [3 4 5 6]
Sliced Array with start, stop and step size: [3 5 7 9]

Leave a Comment