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.

  • Tourism Trend Prediction

    After tourism was established as a motivator of local economies (country, state), many governments stepped up to the plate.

  • Sentiment Analysis Polarity Detection using pos tag

    Sentiment analysis can determine the polarity of sentiments from given sentences. We can classify them into certain categories.

  • For loop with Dictionary

    Traverse a dictionary with for loop Accessing keys and values in dictionary. Use Dict.values() and Dict.keys() to generate keys and values as iterable. Nested Dictionaries with for loop Access Nested values of Nested Dictionaries How useful was this post? Click on a star to rate it! Submit Rating

  • For Loops with python

    For loop is one of the most useful methods to reuse a code for repetitive execution.

  • Metrics and terminologies of digital analytics

    These all metrics are revolving around visits and hits which we are getting on websites. Single page visits, Bounce, Cart Additions, Bounce Rate, Exit rate,

  • Hypothesis Testing

    Hypothesis testing is a statistical method for determining whether or not a given hypothesis is true. A hypothesis can be any assumption based on data.

  • A/B testing

    A/B tests are randomly controlled experiments. In A/B testing, you get user response on various versions of the product, and users are split within multiple versions of the product to figure out the “winner” of the version.

  • For Loop With Tuples

    This article covers ‘for’ loops and how they are used with tuples. Even if the tuples are immutable, the accessibility of the tuples is similar to that of the list.

  • Multivariate ANOVA (MANOVA) with python

    MANOVA is an update of ANOVA, where we use a minimum of two dependent variables.

  • Two-Way ANOVA

    You only need to understand two or three concepts if you have read the one-way ANOVA article. We use two factors instead of one in a two-way ANOVA.

Instagram
WhatsApp
error: Content is protected !!