0% found this document useful (0 votes)
19 views18 pages

Numpy

Python

Uploaded by

Bgautam Mudaliar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

Numpy

Python

Uploaded by

Bgautam Mudaliar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Numpy

NumPy, short for Numerical Python, has long been a cornerstone of numerical computing in
Python. It provides the data structures, algorithms, and library glue needed for most scientific
applications involving numerical data in Python. NumPy contains, among other things:

● A fast and efficient multidimensional array object ndarray


● Functions for performing element-wise computations with arrays or mathematical
operations between arrays
● Tools for reading and writing array-based datasets to disk
● Linear algebra operations, Fourier transform, and random number generation
● A mature C API to enable Python extensions and native C or C++ code to access
NumPy’s data structures and computational facilities.

Beyond the fast array-processing capabilities that NumPy adds to Python, one of its primary
uses in data analysis is as a container for data to be passed between algorithms and libraries.
For numerical data, NumPy arrays are more efficient for storing and manipulating data than the
other built-in Python data structures. Also, libraries written in a lower-level language, such as C
or Fortran, can operate on the data stored in a NumPy array without copying data into some
other memory representation. Thus, many numerical computing tools for Python either assume
NumPy arrays as a primary data structure or else target seamless interoperability with NumPy.

Why Numpy?
One of the reasons NumPy is so important for numerical computations in Python is because it is
designed for efficiency on large arrays of data. There are a number of reasons for this:

● NumPy internally stores data in a contiguous block of memory, independent of other


built-in Python objects. NumPy’s library of algorithms written in the C language can
operate on this memory without any type checking or other overhead. NumPy arrays
also use much less memory than built-in Python sequences.
● NumPy operations perform complex computations on entire arrays without the need for
Python for loops. To give you an idea of the performance difference, consider a NumPy
array of one million integers, and the equivalent Python list:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Now let’s multiply each sequence by 2:

NumPy-based algorithms are generally 10 to 100 times faster (or more) than their
pure Python counterparts and use significantly less memory.

The NumPy ndarray


A Multidimensional Array Object One of the key features of NumPy is its N-dimensional array
object, or ndarray, which is a fast, flexible container for large datasets in Python. Arrays enable
you to perform mathematical operations on whole blocks of data using similar syntax to the
equivalent operations between scalar elements.

To give you a flavor of how NumPy enables batch computations with similar syntax to scalar
values on built-in Python objects, we first import NumPy and generate a small array of random
data:

Then write mathematical operations with data:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
In the first example, all of the elements have been multiplied by 10. In the second, the
corresponding values in each “cell” in the array have been added to each other.

An ndarray is a generic multidimensional container for homogeneous data; that is, all of the
elements must be the same type. Every array has a shape, a tuple indicating the size of each
dimension, and a dtype, an object describing the data type of the array:

Creating ndarrays The easiest way to create an array is to use the array function. This accepts
any sequence-like object (including other arrays) and produces a new NumPy array containing
the passed data. For example, a list is a good candidate for conversion:

Nested sequences, like a list of equal-length lists, will be converted into a multidimen‐ sional
array:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Since data2 was a list of lists, the NumPy array arr2 has two dimensions with shape inferred
from the data. We can confirm this by inspecting the ndim and shape attributes:

Unless explicitly specified, np.array tries to infer a good data type for the array that it creates.
The data type is stored in a special dtype metadata object; for example, in the previous two
examples we have:

Homogeneous n-dimensional vectors:


In addition to np.array, there are a number of other functions for creating new arrays. As
examples, zeros and ones create arrays of 0s or 1s, respectively, with a given length or shape.
empty creates an array without initializing its values to any particular value. To create a higher
dimensional array with these methods, pass a tuple for the shape:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Note:It’s not safe to assume that np.empty will return an array of all zeros. In some cases, it may
return uninitialized “garbage” values.

Matrix functions
1. The identity matrix:
In linear algebra, the identity matrix of size n is the n × n square matrix with ones on the
main diagonal and zeros elsewhere.

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
2. Function ‘eye’:
It is used to return a 2-D array with ones on the diagonal and zeros elsewhere.

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
3. Function ‘diag’:
The diag function takes two arguments:
● An ndarray v.
● An integer k (default = 0). If ‘v’ is dimension is 1 then the function constructs a
matrix where its diagonal number k is formed by the elements of the vector ‘v’. If
a is a matrix (dimension 2) then the function extracts the elements of the kth
diagonal in a one-dimensional vector.

4. Function ‘fromfunction’:
Construct an array by executing a function over each coordinate.
Let’s create a vector-based on its indices.

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
5. Function ‘vectorize’
The purpose of numpy.vectorize is to transform functions which are not numpy-aware
into functions that can operate on (and return) numpy arrays
Let’s build a function sign, allowing us to calculate the sign of a value.

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Array creation functions
Function Description

array Convert input data (list, tuple, array, or other sequence type) to an
ndarray either by inferring a dtype or explicitly specifying a dtype; copies
the input data by default

asarray Convert input to ndarray, but do not copy if the input is already an
ndarray arange Like the built-in range but returns an ndarray instead of
a list

ones, ones_like Produce an array of all 1s with the given shape and dtype; ones_like
takes another array and produces a ones array of the same shape and
dtype

zeros, zeros_like Like ones and ones_like but producing arrays of 0s instead

empty, empty_like Create new arrays by allocating new memory, but do not populate with
any values like ones and zeros

full, full_like Produce an array of the given shape and dtype with all values set to the
indicated “fill value” full_like takes another array and produces a filled
array of the same shape and dtype

eye, identity Create a square N × N identity matrix (1s on the diagonal and 0s
elsewhere)

Aggregation methods:
● ndarray.sum: Return the sum of the array elements over the given axis.
● ndarray.sum: Return the product of the array elements over the given axis.
● ndarray.max: Return the maximum along the given axis.
● ndarray.min: Return the minimum along the given axis.
● ndarray.mean: Returns the average of the array elements along the given axis.
● ndarray.cumsum: Return the cumulative sum of the elements along the given axis.
● ndarray.cumprod: Return the cumulative product of the elements along the given axis.
● ndarray.var: Returns the variance of the array elements, along the given axis.
● ndarray.std: Returns the standard deviation of the array elements along the given axis.
Learnvista Pvt Ltd.
2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
● ndarray.argmin: Return indices of the minimum values along the given axis.
● ndarray.argmax: Return indices of the maximum values along the given axis.

Data Types for ndarrays


The data type or dtype is a special object containing the information (or metadata, data about
data) the ndarray needs to interpret a chunk of memory as a particular type of data:

dtypes are a source of NumPy’s flexibility for interacting with data coming from other systems. In
most cases they provide a mapping directly onto an underlying disk or memory representation,
which makes it easy to read and write binary streams of data to disk and also to connect to code
written in a low-level language like C or Fortran. The numerical dtypes are named the same
way: a type name, like float or int, followed by a number indicating the number of bits per
element. A standard double precision floating-point value (what’s used under the hood in
Python’s float object) takes up 8 bytes or 64 bits. Thus, this type is known in NumPy as float64.

Type Type code Description

int8, uint8 i1, u1 Signed and unsigned 8-bit (1


byte) integer types

int16, uint16 i2, u2 Signed and unsigned 16-bit


integer types

int32, uint32 i4, u4 Signed and unsigned 32-bit


integer types

int64, uint64 i8, u8 Signed and unsigned 64-bit


integer types

float16 f2 Half-precision floating point

float32 f4 or f Standard single-precision floating


point; compatible with C float

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
float64 f8 or d Standard double-precision floating
point; compatible with C double
and Python float object

float128 f16 or g Extended-precision floating point

complex64, complex128, c8, c16, c32 Complex numbers represented by


complex256 two 32, 64, or 128 floats,
respectively

bool ? Boolean type storing True and


False values

object O Python object type; a value can


be any Python object

string_ S Fixed-length ASCII string type (1


byte per character); for example,
to create a string dtype with
length 10, use 'S10'

unicode_ U Fixed-length Unicode type


(number of bytes platform
specific); same specification
semantics as string_ (e.g., 'U10')

Note: It’s important to be cautious when using the numpy.string_ type, as string data in NumPy
is fixed size and may truncate input without warning. pandas has more intuitive out-of-the-box
behavior on non-numeric data.

Arithmetic with NumPy


Arrays Arrays are important because they enable you to express batch operations on data
without writing any for loops. NumPy users call this vectorization. Any arithmetic operations
between equal-size arrays applies the operation element-wise:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Arithmetic operations with scalars propagate the scalar argument to each element in the array:

Comparisons between arrays of the same size yield boolean arrays:

Operations between differently sized arrays is called broadcasting

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Basic Indexing and Slicing
NumPy array indexing is a rich topic, as there are many ways you may want to select a subset
of your data or individual elements. One-dimensional arrays are simple; on the surface they act
similarly to Python lists:

As you can see, if you assign a scalar value to a slice, as in arr[5:8] = 12, the value is
propagated (or broadcasted henceforth) to the entire selection. An important first distinction
from Python’s built-in lists is that array slices are views on the original array. This means that the
data is not copied, and any modifications to the view will be reflected in the source array. To give
an example of this, first create a slice of arr:

Now, when we change values in arr_slice, the mutations are reflected in the original array arr:

The “bare” slice [:] will assign to all values in an array:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Note: If you want a copy of a slice of an ndarray instead of a view, you will need to explicitly
copy the array—for example, arr[5:8].copy().

With higher dimensional arrays, you have many more options. In a two-dimensional array, the
elements at each index are no longer scalars but rather one-dimensional arrays:

Thus, individual elements can be accessed recursively. But that is a bit too much work, so you
can pass a comma-separated list of indices to select individual elements. So these are
equivalent:

Indexing with slices


Like one-dimensional objects such as Python lists, ndarrays can be sliced with the familiar
syntax:

Consider the two-dimensional array from before, arr2d. Slicing this array is a bit different:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
As you can see, it has sliced along axis 0, the first axis. A slice, therefore, selects a range of
elements along an axis. It can be helpful to read the expression arr2d[:2] as “select the first two
rows of arr2d.”

You can pass multiple slices just like you can pass multiple indexes:

When slicing like this, you always obtain array views of the same number of dimensions. By
mixing integer indexes and slices, you get lower dimensional slices. For example, we can select
the second row but only the first two columns like so:

Similarly, I can select the third column but only the first two rows like so:

Boolean Indexing
Let’s consider an example where we have some data in an array and an array of names
with duplicates. I’m going to use here the randn function in numpy.random to generate
some random normally distributed data:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Suppose each name corresponds to a row in the data array and we wanted to select all the
rows with corresponding name 'Bob'. Like arithmetic operations, compari‐ sons (such as ==)
with arrays are also vectorized. Thus, comparing names with the string 'Bob' yields a boolean
array:

This boolean array can be passed when indexing the array:

The boolean array must be of the same length as the array axis it’s indexing. You can even mix
and match boolean arrays with slices or integers.

What’s the difference between numpy.arange and the range


python built-in function?
● Python range function
Python built-in function ‘range’ takes only integers as arguments.
range(0, 1, 0.2) #Error

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
● Function: ‘linspace’:
Return evenly spaced numbers over a specified interval.

● Function: ‘logspace’:
Return numbers spaced evenly on a log scale (by default in base 10).

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Learnvista Pvt Ltd.
2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]

You might also like