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!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

  • 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.

  • Hypothesis Testing: A Comprehensive Overview

    This article delves into the application of hypothesis testing across diverse domains

  • Versions of ANCOVA (Analysis Of Covariance) with python

    To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…

  • Python Variables

    How useful was this post? Click on a star to rate it! Submit Rating Average rating 0 / 5. Vote count: 0 No votes so far! Be the first to rate this post.

  • A/B Testing Quiz

    Complete the code by dragging and dropping the correct functions

  • Python Functions

    Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…

  • Python Indexing: A Guide for Data Science Beginners

    Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.

  • Diffusion Models: Making AI Creativity

    Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…

  • Quiz Challenge: Basics with Python [Questions]

    Solve These Questions in Following Challange

  • Introducing Plethora of Stable Diffusion models: Part 1

    Generate AI images as good as DALL-E completely offline.

Instagram
WhatsApp
error: Content is protected !!