NumPy: Python’s Mathematical Backbone

Numpy has created a vast ecosystem spanning numerous fields of science.

Welcome to the fascinating world of NumPy! If you’re new to data science or just starting to dip your toes into coding, you’ve landed in the perfect place.

NumPy, short for Numerical Python, is a fundamental package for scientific computing in Python. It’s the backbone that supports a wide array of data science operations, from basic mathematical operations to complex machine learning algorithms. In this guide, we’ll break down the basics of NumPy in a simple, engaging manner, peppered with code examples to ensure you grasp the concepts thoroughly.

What is NumPy?

NumPy is an open-source Python library that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. It’s designed for scientific computation, enabling complex mathematical operations to be expressed in a few lines of code. At its core, the library’s main object is the powerful ndarray (n-dimensional array), which is a grid of values, all of the same type, indexed by a tuple of non-negative integers.

Main Features

  • NumPy Library is used to work with arrays.
  • It also has functions for matrices, linear algebra, and Fourier Transform.
  • Python has lists which do the work of an array, but NumPy will provide 50x more speed than a Python list.
  • NumPy is written in C, C++ and Python.
  • Numpy integrates c and C++ codes in Python.
  • Where fast computations are required c, C++ is used.
  • It works efficiently with the latest CPU architecture.

Why Use NumPy?

Before NumPy, Python was capable of handling lists. However, lists are slow to process and not suitable for mathematical operations, especially when dealing with large datasets. NumPy arrays are stored at one continuous place in memory, unlike lists, making it efficient for:

  • Operations involving mathematical computations on arrays
  • Handling large data sets
  • Implementing machine learning algorithms

Numpy Installation

Using this command will ensure the installation of the latest version of NumPy that is compatible with your system.

pip install numpy

Importing Numpy into your code

  • import numpy
  • import numpy as np
Python
Python
Python
import numpy

num_arr=numpy.array([42,541,234,4,45,454,5])

print(num_arr)

print(type(num_arr))
Output
[ 42 541 234   4  45 454  5]
<class 'numpy.ndarray'>

NumPy Arrays: The Basics

Creating a NumPy array is straightforward. Let’s explore a few ways to do this.

From a Python List

Python
Python
Python
# Create a 1D NumPy array 
my_array = np.array([1, 2, 3, 4, 5]) 
print(my_array) 
# Create a 2D NumPy array (also known as a matrix) 
my_2d_array = np.array([[1, 2, 3], [4, 5, 6]]) 
print(my_2d_array)

Special Arrays in NumPy

NumPy also provides functions to create arrays filled with zeros, ones, or a range of numbers – handy for initializing large arrays.

Python
Python
Python
import numpy

num_arr=numpy.array([42,541,234,4,45,454,5])

print(num_arr)

print(type(num_arr))
Output
[ 42 541 234   4  45 454  5]
<class 'numpy.ndarray'>
# Array of zeros 
zeros = np.zeros((3, 4)) 
print(zeros) 
# Array of ones 
ones = np.ones((2, 2)) 
print(ones) 
# Array with a sequence of numbers 
sequence = np.arange(10, 20) 
print(sequence) 
# Evenly spaced numbers over a specified interval 
linspace = np.linspace(0, 1, 5) 
print(linspace)

Operations with NumPy Arrays

One of the strengths of NumPy is its ability to perform operations on arrays with ease.

Arithmetic Operations

You can easily perform arithmetic operations on arrays element-wise.

Python
Python
Python
a = np.array([1, 2, 3, 4]) 
b = np.array([10, 20, 30, 40]) 
# Addition 
print(a + b) 
# Subtraction 
print(b - a) 
# Multiplication 
print(a * b) 
# Division 
print(b / a)

Mathematical Functions

NumPy offers a vast library of mathematical functions to perform operations such as trigonometric calculations, logarithms, and more.

Python
Python
Python
import numpy

num_arr=numpy.array([42,541,234,4,45,454,5])

print(num_arr)

print(type(num_arr))
Output
[ 42 541 234   4  45 454  5]
<class 'numpy.ndarray'>
# Square roots 
print(np.sqrt(a)) 
# Exponential 
print(np.exp(a)) 
# Sine function 
print(np.sin(a))

Aggregations

Performing statistical calculations is straightforward with NumPy’s aggregation functions.

Python
Python
Python
# Sum 
print(np.sum(a)) 
# Maximum value 
print(np.max(a)) 
# Mean 
print(np.mean(a))

Numpy array Functions

FunctionsExplanation
copy()Copy functions create an identical, independent array from the original.
view()The view reflects the original array, and any change to the view or the array is similarly affected.
shape()Returns the shape of the array with respect to its dimensions.
reshape()Changes the dimensions or changes the number of elements in the array.
concatnate()Concatenate joins two or more arrays by using its 0th axis default and the axis is changeable.
stack()Stack joins two 1D arrays into one using axis parameters.
hstack()H Stack helps to stack arrays with the rows.
vstack()V stack helps to stack arrays with the columns.
dstack()D stack helps to stack arrays with height.
array_split()split an array into a specific number of arrays.
hsplit()split 2D arrays among their rows into a specific number of arrays.
where()searches an element in the array and returns the indexes of matches.
searchsorted()SearchSorted() is used for inserting elements according to order in the array and Returns an index for the element to be inserted.
sort()Returns a sorted array into a specified order.
numpy.broadcastnumpy. broadcast

Python list vs Numpy array

Numpy arrayPython List
A numpy array is limited to a single datatype in single array.A numpy array is limited to a single datatype in a single array.
The Numpy array occupies less memory.Python list calculations are slower than Numpy array
Numpy arrays are at the heart of all Numpy systems.Python lists occupy more memory while containing similar items.
Numpy array calculations are faster because of the above fact.Python has multiple sequenced data storage techniques that have different functionalities.
you can use ndarray to create multidimensional arrays.
etc matrices , data tables.
Python has nested data set capabilities.

Numpy Ecosystem

Numpy has created a vast ecosystem spanning numerous fields of science.

It empowers the users with the computational powers of the C and FORTRAN languages. All these functionalities have been brought over to Python. Since the codes are easily readable, they can be used to make complex calculations. Numpy provides the ability to exploit highly functional machines’ powers for calculations. So many libraries exploit these capabilities to provide their functions. Such as Tensorflow, xtensor, Dask, CuPy, JAX, and Xarray.

In the field of data science, Numpy is capable of assisting at all steps of analytics.

  • ETL analysis
  • Exploratory analysis
  • Model creation and Evaluation
  • Dashboard reports

This article only introduces the basic, fundamental features of numpy. In the following articles, you will be introduced to a plethora of numpy features.

Numpy Arrays

Numpy Library is used to work with arrays. It also has functions for matrices, linear algebra, and Fourier Transform. Python has lists which do the work of an array, but numpy will provide 50x more speed than a Python list.

How useful was this post?

Click on a star to rate it!

  • ANCOVA: Analysis of Covariance with python

    ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.

  • Learn Python The Fun Way

    What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!

  • Meet the most efficient and intelligent AI assistant : NotebookLM

    Start using NotebookLM today and embark on a smarter, more efficient learning journey!

  • Break the ice

    This can be a super guide for you to start and excel in your data science career.

  • ANOVA (Analysis of Variance ) part 1

    A method to find a statistical relationship between two variables in a dataset where one variable is used to group data.

  • Basic plots with Seaborn

    Seaborn library has matplotlib at its core for data point visualizations. This library gives highly statistical informative graphics functionality to Seaborn.

  • Matplotlib in python

    The Matplotlib library helps you create static and dynamic visualisations. Dynamic visualizations that are animated and interactive. This library makes it easy to plot data and create graphs.

  • Plotly with Python and R

    This library is named Plotly after the company of the same name. Plotly provides visualization libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST.

  • Numpy Array

    Numpy array have functions for matrices ,linear algebra ,Fourier Transform. Numpy arrays provide 50x more speed than a python list.

  • NumPy: Python’s Mathematical Backbone

    Numpy has created a vast ecosystem spanning numerous fields of science.

  • Introduction to Pandas: A Guide

    Pandas is a easy to use data analysis and manipulation tool. Pandas provides functionality for categorical,ordinal, and time series data . Panda provides fast and powerful calculations for data analysis.

  • Pandas Dataframe in brief

    In this tutorial, you will learn How to Access The Data in Various Ways From the dataframe.

  • Exploring the World of Sets in Python

    Understand one of the important data types in Python. Each item in a set is distinct. Sets can store multiple items of various types of data.

  • A Beginner’s Guide to Immutable Tuples

    Tuples are a sequence of Python objects. A tuple is created by separating items with a comma. They are put inside the parenthesis “”(“” , “”)””.

Instagram
WhatsApp
error: Content is protected !!