Professional Documents
Culture Documents
Introduction To Numpy: by Adnan Amin Lecturer
Introduction To Numpy: by Adnan Amin Lecturer
By
Adnan Amin
Lecturer
Data-driven science (Data Science)
• Effective data-driven science and computation requires understanding
how data is stored and manipulated.
• Datasets can come from a wide range of sources and a wide range of
formats, including collections of documents, collections of images,
collections of sound clips, collections of numerical measurements.
• Remember:-
• All data fundamentally as arrays of numbers.
Images:
• It can be thought of as simply two dimensional narrays of numbers
representing pixel brightness across the area.
Source
Lexalytics
No matter what the data are:
The first step in making them
analyzable will be to transform them
into arrays of numbers.
Efficient storage and manipulation of
numerical arrays is absolutely fundamental
to the process of doing data science.
For example, Python has the NumPy package and the Pandas package
Numerical Python Python data analysis
numerical
computation
Scientific Python
Visualization
Collection of algorithms
and functions
Machine learning
library
Visualization
Source: TechVidvan
• NumPy (short for Numerical Python) provides an efficient interface to
store and operate on dense data buffers (in memory).
• NumPy arrays form the core of nearly the entire ecosystem of data
science tools in Python.
import numpy as np
The Basics of NumPy Arrays
Rule 1:
Rule 2:
If the two arrays
If the shape of the
differ in their Rule 3:
two arrays does not
number of
match in any If in any dimension
dimensions, the
dimension, the the sizes disagree
shape of the one
array with shape and neither is equal
with fewer
equal to 1 in that to 1, an error is
dimensions is
dimension is raised.
padded with ones
stretched to match
on its leading (left)
the other shape.
side.
Broadcasting example 1
• Let’s look at adding a two-dimensional array to a one-dimensional array:
• Let’s consider an operation on these two arrays. The shapes of the arrays are:
• m.shape = (2, 3)
• a.shape = (3,)
• Rule 1 says that the array a has fewer dimensions, so we pad it on the left with
ones:
• m.shape -> (2, 3)
• a.shape -> (1, 3)
Broadcasting example 1…
• By rule 2, we now see that the first dimension disagrees, so we
stretch this dimension to match:
• m.shape -> (2, 3)
• a.shape -> (2, 3)
• The shapes match, and we see that the final shape will be (2, 3):
Broadcasting example 2
• Let’s take a look at an example where both arrays need to be
broadcast: