Mastering the Basics of NumPy: A Step-by-Step Guide for Beginners

IEEE_TEMS BLOGS
4 min readJun 15, 2023

--

“You can have data without information, but you cannot have information without data.” — Daniel Keys Moran

NumPy Essentials-The introduction:

NumPy offers an array of functionalities, including support for matrices, Fourier transforms, and a rich set of functions for working in the field of linear algebra. Numerical Python is referred to as NumPy.

In the year 2005, Travis Oliphant developed NumPy. You can use it for free because it is an open-source project.

NumPy’s Mathematical Powerhouse-use of numpy:

The goal of NumPy is to offer array objects that are up to 50 times faster than conventional Python lists. Numerous mathematical operations can be carried out on arrays with NumPy.

It provides a vast library of high-level mathematical functions that work on these arrays and matrices and strong data structures that ensure efficient calculations with arrays and matrices.

Installing Python: Setting Up the Programming Environment

Let’s begin by installing NumPy before getting into the specifics. Using pip, the Python package manager, run the following command to install it:

pip install numpy

Importing NumPy: Syntax and Best Practices

Import it in your applications by adding the import keyword:

import numpy
arr=numpy.array([1,2,3,4])
print(arr)

0D ARRAYS

The components of an array are called 0-D arrays or Scalars. An array’s values are all 0-D arrays.

import numpy as np
arr=np.array(14)
print(arr)

1D ARRAYS

1-D or a one-dimensional array.

import numpy as np
arr=np.array([1,2,3,4])
print(arr)

2D ARRAYS

These are frequently utilized to represent second-order or matrix tensors.

import numpy as np
arr=np.array([[1,2,3,4],[4,5,6,7]])
print(arr)

DIMENSIONS

The ndim attribute in NumPy Arrays gives us an integer value that indicates how many dimensions the array has.

import numpy as np
arr=np.array([1,2,3],ndmin=5)
print(arr)
print('no of dimensions:',arr.ndim)

ARRAY INDEXING

NumPy array’s elements can be accessed by indexing. An operation called indexing selects a subset of values from an array.

import numpy as np
arr=np.array([1,2,3])
print(arr[1]+arr[2])

ARRAY SLICING

Python’s term for getting elements from one given index and adding them to another is “slicing.”

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])

DATA TYPES

Various additional data types in NumPy only have one character, such as int for integers and uint for unsigned integers.

Data Types in NumPy

EXAMPLE:

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.dtype)

ARRAY SHAPE

The number of items in each dimension determines an array’s form.
It gives back a tuple with the number of corresponding elements for each index.

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)

ARRAY RESHAPING

By reshaping, we can adjust the number of pieces in each dimension or add or remove sizes.

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)

ARRAY ITERATING

This may be done using a normal Python for a loop since Numpy works with multi-dimensional arrays.

import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
print(x)

ARRAY CONCATENATION

Putting the contents of two or more arrays into one array is known as concatenation.

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)

ARRAY SPLITTING

Splitting separates one array into several while joining combines several arrays into one.

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)

ARRAY SORTING

Placing items in an orderly sequence is the act of sorting.

import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))

ARRAY FILTERING

Filtering is extracting a subset of components from an existing array and using those elements to build a new array.

import numpy as np
arr = np.array([41, 42, 43, 44])
x = [True, False, True, False]
newarr = arr[x]
print(newarr)

GETTING A RANDOM NUMBER

A random number DOES NOT always represent a different value. Random refers to something that cannot be logically expected.

from numpy import random
x = random.randint(100)
print(x)

Conclusion for Mastering NumPy: Unlocking the Power of Scientific Computing

NumPy, a vital module in Python, enables rapid and efficient complex scientific computations. Access to effective tools for data analysis, machine learning, and numerical computing is made possible by mastering its principles. Python programmers can work with enormous datasets, carry out complex calculations, and enhance performance using NumPy. Popular libraries like Pandas and SciPy are built on top of it. Python’s ability to handle challenging scientific problems is made possible via NumPy.

Its perfect compatibility with the Python environment enables seamless interchange with other libraries and frameworks, providing thorough solutions for complex scientific issues that arise in the real world.

With this knowledge, you are prepared to study more complex issues and utilize NumPy’s extensive capabilities in your work.

Coding is fun!

--

--

IEEE_TEMS BLOGS
IEEE_TEMS BLOGS

Written by IEEE_TEMS BLOGS

IEEE_TEMS, a chapter of VIT UNIV, before we tell about us STOP! Make your fingers join forces with your mind to work hard.Let’s go to the glassy world of techs

No responses yet