0% found this document useful (0 votes)
8 views

Numpy

NumPy is a Python package created for efficient computation and processing of multidimensional arrays, providing high-performance tools for scientific computing and data analysis. It offers features like broadcasting, which allows for arithmetic operations on arrays of different shapes, and supports efficient memory usage and performance compared to Python lists. NumPy is widely used in various applications, including machine learning, data manipulation, and scientific computing, and can be easily installed using pip.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Numpy

NumPy is a Python package created for efficient computation and processing of multidimensional arrays, providing high-performance tools for scientific computing and data analysis. It offers features like broadcasting, which allows for arithmetic operations on arrays of different shapes, and supports efficient memory usage and performance compared to Python lists. NumPy is widely used in various applications, including machine learning, data manipulation, and scientific computing, and can be easily installed using pip.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Ques 1. What is NumPy, and what is its primary purpose in Python?

Ans- Travis Oliphant created NumPy package in 2005 by injecting the features of the ancestor
module Numeric into another module Numarray.

NumPy stands for numeric python which is a python package for the computation and processing of
the multidimensional and single dimensional array elements.

Numpy is a general-purpose array-processing package. It provides a high-performance


multidimensional array object, and tools for working with these arrays. It is the fundamental
package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be
used as an efficient multi-dimensional container of generic data.

NumPy stands for numeric python which is a python package for the computation and processing of
the multidimensional and single dimensional array elements.

It is a Python library that provides a multidimensional array object, various derived objects (such as
masked arrays and matrices), and an assortment of routines for fast operations on arrays, including
mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic
linear algebra, basic statistical operations, random simulation and much more.

Who else uses NumPy? :-

NumPy fully supports an object-oriented approach, starting, once again, with ndarray. For example,
ndarray is a class, possessing numerous methods and attributes. Many of its methods are mirrored
by functions in the outer-most NumPy namespace, allowing the programmer to code in whichever
paradigm they prefer. This flexibility has allowed the NumPy array dialect and NumPy ndarray class
to become the de-facto language of multi-dimensional data interchange used in Python.

The need of NumPy :-

With the revolution of data science, data analysis libraries like NumPy, SciPy, Pandas, etc. have seen
a lot of growth. With a much easier syntax than other programming languages, python is the first
choice language for the data scientist.

NumPy provides a convenient and efficient way to handle the vast amount of data. NumPy is also
very convenient with Matrix multiplication and data reshaping. NumPy is fast which makes it
reasonable to work with a large set of data.

1. NumPy performs array-oriented computing.


2. It efficiently implements the multidimensional arrays.
3. It performs scientific computations.
4. It is capable of performing Fourier Transform and reshaping the data stored in
multidimensional arrays.
5. NumPy provides the in-built functions for linear algebra and random number generation.

How are NumPy arrays different from Python lists? :-

Python list is most suitable for data storage and not ideally intended for math tasks, while a NumPy
list can easily support math-intensive tasks.
Python provides a basic data structure called a list. A Python list supports a changeable -- or mutable
-- ordered sequence of data elements or values called items. A single list can also contain many
different data types. This makes lists handy for storing multiple data items as a single variable -- such
as customer contact information and account numbers. However, lists are potentially inefficient,
using significant amounts of memory and posing problems attempting to process mathematical
operations on varied item types.

By comparison, NumPy is built around the idea of a homogeneous data array. Although a NumPy
array can specify and support various data types, any array created in NumPy should use only one
desired data type -- a different array can be made for a different data type. This approach requires
less memory and allows more efficient system performance when processing mathematical
operations on array elements.

Common NumPy applications and uses :-

The NumPy mathematical library can be used by any software developer (at any experience level)
seeking to integrate complex numerical computing functions into their Python codebase. NumPy is
also routinely used in many different data science, machine learning (ML) and scientific Python
software packages including the following:

 Matplotlib.
 Pandas.
 scikit-image.
 scikit-learn.
 SciPy.

NumPy is regularly applied in a wide range of use cases including the following:

Data manipulation and analysis :- NumPy can be used for data cleaning, transformation and
aggregation. The data can then be readily processed through varied NumPy mathematical
operations such as statistical analysis

Scientific computing:- NumPy handles advanced mathematical operations such as matrix


multiplication, eigenvalue calculation and differential equations. This makes NumPy particularly
valuable across a vast range of simulation, modeling, visualization, computational processing and
other scientific computing tasks.

Machine learning:-ML tasks are math-intensive, and ML libraries -- such as TensorFlow and scikit-
learn -- readily use NumPy for mathematical computations needed to support ML algorithms and
model training.

Signal and image processing:- Signals and images can readily be represented as data arrays, and
NumPy can provide tools needed to perform important processing tasks on that data for purposes
such as enhancement and filtering.

Ques 2. How do you install NumPy on your Python environment?

Ans:- How to install NumPy in Python :-

Step 1: Run this command to see if pip3 and Python3 are correctly installed:
You will get the version of Python and pip installed in the output window. If getting any error, you
need to install Python and pip.

Step 2: Now install NumPy with the help of the pip command:

Step 3: After this, you need to check if the NumPy library is installed successfully in the system or not
by the following command.

Ques 3. What is an ndarray in NumPy, and how does it differ from a regular Python list or array?

Ans:- Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array.A
tuple of integers giving the size of the array along each dimension is known as shape of the array. An
array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square
brackets and can be initialized by using nested Python Lists.
The ndarray object can be accessed by using the 0 based indexing. Each element of the Array object
contains the same size in the memory.

Creating a ndarray object :-

The ndarray object can be created by using the array routine of the numpy module. For this purpose,
we need to import the numpy.

>>> a = numpy.array

Consider the below image.

We can also pass a collection object into the array routine to create the equivalent n-dimensional
array. The syntax is given below.

>>> numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

Comparison between Numpy array and Python List :-


Lists in Python store additional information about each element, such as its type and reference
count. This overhead can be significant when dealing with a large number of elements.

NumPy arrays store elements of the same data type, making them more compact and memory-
efficient than lists.

Datatype: -

Lists can hold different data types, but this can decrease memory efficiency and slow numerical
operations.

NumPy arrays have a fixed data type, reducing memory overhead by eliminating the need to store
type information for each element.

Memory : -

Lists may not store elements in contiguous memory locations, causing memory fragmentation and
inefficiency.

NumPy arrays store elements in adjacent memory locations, reducing fragmentation and allowing
for efficient access.

Performance :-

Lists can store any data type, but lack specialized NumPy functions for numerical operations

NumPy arrays are optimized for numerical computations, with efficient element-wise operations and
mathematical functions. These operations are implemented in C, resulting in faster performance
than equivalent operations on lists.

Memory consumption between Numpy array and lists :-

In Python, a list is a built-in data structure that can hold elements of varying data types. However,
the flexibility of lists comes at the cost of memory efficiency.

Python’s NumPy library supports optimized numerical array and matrix operations.

Time comparison between Numpy array and Python lists :-

here two Python lists and two Numpy arrays will be created and each container has 1000000
elements. Multiplication of elements in both the lists and Numpy arrays respectively will be carried
out and the difference in time needed for the execution for both the containers will be analyzed to
determine which one takes less time to perform the operation.

Effect of operations on Numpy array and Python Lists :-


the incapability of the Python list to carry out a basic operation is demonstrated. A Python list and a
Numpy array having the same elements will be declared and an integer will be added to increment
each element of the container by that integer value without looping statements.

Advantages of using Numpy Arrays Over Python Lists: -

 Consumes less memory.


 Fast as compared to the python List.
 Convenient to use.

Ques 4. Explain the concept of broadcasting in NumPy. How does it work, and when is it useful?

Ans- The term broadcasting refers to the ability of NumPy to treat arrays with different dimensions
during arithmetic operations. This process involves certain rules that allow the smaller array to be
‘broadcast’ across the larger one, ensuring that they have compatible shapes for these operations.

Broadcasting is not limited to two arrays; it can be applied over multiple arrays as well.

Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of
Python. It does this without making needless copies of data and usually leads to efficient algorithm
implementations. There are, however, cases where broadcasting is a bad idea because it leads to
inefficient use of memory that slows computation

Broadcasting provides a means of vectorizing array operations, therefore eliminating the need for
Python loops. This is because NumPy is implemented in C Programming, which is a very efficient
language. It does this without making needless copies of data which leads to efficient algorithm
implementations. But broadcasting over multiple arrays in NumPy extension can raise cases where
broadcasting is a bad idea because it leads to inefficient use of memory that slows down the
computation. The resulting array returned after broadcasting will have the same number of
dimensions as the array with the greatest number of dimensions.

General broadcasting rules :-

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the
trailing (i.e. rightmost) dimension and works its way left. Two dimensions are compatible when

they are equal, or

one of them is 1.

If these conditions are not met, a ValueError: operands could not be broadcast together exception is
thrown, indicating that the arrays have incompatible shapes.

Input arrays do not need to have the same number of dimensions. The resulting array will have the
same number of dimensions as the input array with the greatest number of dimensions, where the
size of each dimension is the largest size of the corresponding dimension among the input arrays.
Note that missing dimensions are assumed to have size one.

For example, if you have a 256x256x3 array of RGB values, and you want to scale each color in the
image by a different value, you can multiply the image by a one-dimensional array with 3 values.
Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that
they are compatible:
When either of the dimensions compared is one, the other is used. In other words, dimensions with
size 1 are stretched or “copied” to match the other.In the following example, both the A and B arrays
have axes with length one that are expanded to a larger size during the broadcast operation:

Array Broadcasting Algorithm :-

The NumPy array broadcasting algorithm determines how NumPy will treat arrays with different
shapes during arithmetic operations.

Input: Array ‘A’ with ‘m’ dimensions and array ‘B’ with ‘n’ dimensions

p = max(m, n)
if m < p:
left-pad A's shape with 1s until it also has p dimensions
else if n < p:
left-pad B's shape with 1s until it also has p dimensions
result_dims = new list with p elements
for i in p-1 ... 0:
A_dim_i = A.shape[i]
B_dim_i = B.shape[i]
if A_dim_i != 1 and B_dim_i != 1 and A_dim_i != B_dim_i:
raise ValueError("could not broadcast")
else:
# Pick the Array which is having maximum Dimension
result_dims[i] = max(A_dim_i, B_dim_i)

What makes an array Broadcastable? :-

An array can be considered broadcastable if it satisfies the following rules:

The arrays have the same shapes


The arrays have the same number of dimensions and the length of each dimension is either of
common length or 1.

If an array has a different number of dimensions then the array with fewer dimensions can have its
shape prepended.

NumPy Broadcasting Operations :-

 Computing the outer product of vectors


 Broadcasting a vector to a matrix,
 Broadcasting a vector to the transposed matrix,
 Reshaping and broadcasting a vector to a matrix, and
 Performing scalar multiplication on a matrix.

Ques 5. How can you create a NumPy array filled with zeros of a specific shape and data type?

Ans- numpy.zeros() function is the primary method for creating an array of zeros in NumPy. It
requires the shape of the array as an argument, which can be a single integer for a one-dimensional
array or a tuple for multi-dimensional arrays. This method is significant because it provides a fast and
memory-efficient way to initialize arrays, which is crucial in large-scale computations.

import numpy as np

# Create a 3x3 array of zeros

zero_array = np.zeros((3, 3))

print(zero_array)

How to Specify Data Types for Arrays :-


The numpy.zeros() function allows specifying the data type of the elements using the dtype
parameter. This feature is significant when you need arrays with specific data types for compatibility
or performance reasons.

import numpy as np

# Creating array of 2 rows 3 column

# as Datatype integer

arr = np.zeros((2, 3), dtype=int)

print(arr)

Ques 6. Describe the difference between np.dot() and np.matmul() in NumPy and when you might
use each one.

Ans- Matrix multiplication, often denoted as matmul, and dot product are fundamental operations
in linear algebra. They are pivotal in various applications, from machine learning to physics.
Understanding when to use matmul versus dot can greatly impact the efficiency of your
computations.
The Difference between Matmul and Dot in Numpy :

Definition and Explanation :-

Matmul stands for matrix multiplication, a mathematical operation that combines two matrices to
produce a new one. It’s a versatile tool used in a wide range of applications, including solving
systems of linear equations, image processing, and deep learning.

The dot product, also known as the inner product or scalar product, is a simpler operation that
calculates the sum of the products of corresponding entries in two vectors. It’s widely used in
geometry and physics, often for finding angles and projections.

Comparison of Matmul and Dot:

Matmul and dot in Numpy serve distinct purposes. While matmul combines matrices, the dot
product operates on vectors.

Factors to Consider When Choosing between Matmul and Dot :-


1- Dimensions of the Matrices:- The dimensions of your data play a crucial role in determining
whether to use matmul or dot. Matmul requires that the number of columns in the first
matrix matches the number of rows in the second. The dot product, on the other hand, can
be performed on vectors of any length.

2- Computational Efficiency:- Matmul can be computationally intensive, especially for large


matrices. The dot product is computationally simpler and faster. If performance is a primary
concern, dot may be the better choice.

3- Precision and Accuracy: -Matmul allows for more complex operations, making it suitable for
applications where precision is essential, such as scientific simulations and neural networks.
Dot is simpler but may need to be more accurate in certain scenarios.

4- Available Hardware and Software: - Consider the hardware and software at your disposal.
Matmul might be optimized for certain hardware or software libraries, offering better
performance. Dot, being a simpler operation, may be more universally applicable.

Examples of When to Use Matmul or Dot in Numpy :-

Matrices with Matching Dimensions: When you need to multiply two matrices, matmul is your go-to
choice. Ensure that the number of columns in the first matrix matches the number of rows in the
second.

Large Matrices: For large datasets or matrices, where computational efficiency is crucial, the dot
product is often a faster option. It reduces the computational load while providing satisfactory
results.

Small Matrices: In cases where precision and accuracy are paramount, and the matrices are
relatively small, matmul is a better choice. It handles complex mathematical operations effectively.

Matrices with Different Dimensions: When you have matrices with dimensions that don’t align for
matmul, opt for the dot product. It can operate on vectors of varying lengths without issues.
Ques7. What is slicing in NumPy, and how does it work with multidimensional arrays?

Ans- Array Slicing is the process of extracting a portion of an array. With slicing, we can easily access
elements in the array. It can be done on one or more dimensions of a NumPy array.

Here,

start - index of the first element to be included in the slice

stop - index of the last element (exclusive)

step - step size between each element in the slice


NumPy Array Slicing :-

Modify Array Elements Using Slicing :-

With slicing, we can also modify array elements using:

start parameter

stop parameter

start and stop parameter

start, stop, and step parameter

NumPy Array Negative Slicing :-


Ques 8. :- How can you perform element-wise operations (addition, subtraction, multiplication, etc.)
on two NumPy arrays?

Ans:- NumPy provides a wide range of operations that can perform on arrays, including arithmetic
operations.NumPy's arithmetic operations are widely used due to their ability to perform simple and
efficient calculations on arrays.

List of Arithmetic Operations :-

NumPy Array Element-Wise Addition :- As mentioned earlier, we can use the both + operator and
the built-in function add() to perform element-wise addition between two NumPy arrays. For
example,
NumPy Array Element-Wise Subtraction:- In NumPy, we can either use the - operator or the
subtract() function to perform element-wise subtraction between two NumPy arrays. For example,

NumPy Array Element-Wise Multiplication :-

For element-wise multiplication, we can use the * operator or the multiply() function. For example,
NumPy Array Element-Wise Division :-

We can use either the / operator or the divide() function to perform element-wise division between
two numpy arrays. For example,

NumPy Array Element-Wise Exponentiation :-Array exponentiation refers to raising each element of
an array to a given power. In NumPy, we can use either the ** operator or the power() function to
perform the element-wise exponentiation operation. For example,
NumPy Array Element-Wise Modulus :-

We can perform a modulus operation in NumPy arrays using the % operator or the mod() function.

This operation calculates the remainder of element-wise division between two arrays.

Let's see an example.


Ques 9. What are universal functions (ufuncs) in NumPy, and why are they important for array
operations?

Ans- NumPy ufuncs | Universal functions :-

NumPy Universal functions (ufuncs in short) are simple mathematical functions that operate on
ndarray (N-dimensional array) in an element-wise fashion.

It supports array broadcasting, type casting, and several other standard features. NumPy provides
various universal functions like standard trigonometric functions, functions for arithmetic
operations, handling complex numbers, statistical functions, etc.

Characteristics of NumPy ufuncs :-

 These functions operate on ndarray (N-dimensional array) i.e. NumPy’s array class.
 It performs fast element-wise array operations.
 It supports various features like array broadcasting, type casting, etc.
 Numpy universal functions are objects that belong to numpy.ufunc class.
 Python functions can also be created as a universal function using the frompyfunc library
function.
 Some ufuncs are called automatically when the corresponding arithmetic operator is used
on arrays. For example, when the addition of two arrays is performed element-wise using
the ‘+’ operator then np.add() is called internally.

Why use ufuncs?

ufunc, or universal functions offer various advantages in NumPy. Some benefits of using ufuncs
are:

Vectorized Operations :-

 ufuncs are applied element-wise to all the elements in the ndarray.


 ufuncs are more efficient than loops as they are applied simultaneously to all elements.
Vectorization is very useful on large data sets.

2. Type Casting :-

 Type casting means converting the data type of a variable to perform the necessary
operation.
 ufuncs automatically handle type casting and ensure compatible datatypes for
calculations.
 This allows code to be concise and reduces the chances of error.

3- Broadcasting :-

 Broadcasting means to perform arithmetic operations on arrays of different size.


 ufuncs automatically handle broadcasting and avoids the need for manual array shape
manipulation.

Basic Universal Functions (ufunc) in NumPy :-

Here are some of the universal functions (ufunc) in the NumPy Python library:
Trigonometric functions :-These functions work on radians, so angles need to be converted to
radians by multiplying by pi/180. Only then we can call trigonometric functions. They take an array
as input arguments.

It includes functions like:

Statistical functions :-

These functions calculate the mean, median, variance, minimum, etc. of array elements.

They are used to perform statistical analysis of array elements.

It includes functions like:


Bit-twiddling functions :-

These functions accept integer values as input arguments and perform bitwise operations on binary
representations of those integers.

It includes functions like:


Ques 10. Explain how you can reshape a NumPy array and provide an example of when reshaping
might be necessary in data manipulation or analysis.

Ans- Reshaping numpy array simply means changing the shape of the given array, shape basically
tells the number of elements and dimension of array, by reshaping an array we can add or remove
dimensions or change number of elements in each dimension. In order to reshape a numpy array we
use reshape method with the given array.

Note : We can also use np.reshape(array, shape) command to reshape the array

Reshaping : 1-D to 2D :-

In this example we will reshape the 1-D array of shape (1, n) to 2-D array of shape (N, M) here M
should be equal to the n/N there for N should be factor of n.

Code-

# importing numpy

import numpy as np
# creating a numpy array

array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

# printing array

print("Array : " + str(array))

# length of array

n = array.size

# N-D array N dimension

N=4

# calculating M

M = n//N

# reshaping numpy array

# converting it to 2-D from 1-D array

reshaped1 = array.reshape((N, M))

# printing reshaped array

print("First Reshaped Array : ")

print(reshaped1)

# creating another reshaped array

reshaped2 = np.reshape(array, (2, 8))

# printing reshaped array

print("Second Reshaped Array : ")

print(reshaped2)
Reshaping : 1-D to 3-D :-

In this we will see how we can reshape a 1-D array to 3-D dimension array. A 3-D array is the 1-D
array of 2-D arrays.
Code-

# importing numpy

import numpy as np

# creating a numpy array

array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

# printing array

print("Array : " + str(array))

# reshaping numpy array

# converting it to 3-D from 1-D array

reshaped = array.reshape((2, 2, 4))

# printing reshaped array

print("Reshaped 3-D Array : ")

print(reshaped)

Reshaping N-D to 1-D array :-

In this example we will see how we can reshape a 2-D or 3-D array to form a 1-D array. We can also
use reshape(-1) to do this, here -1 is the unknown dimension.

# importing numpy

import numpy as np

# creating a numpy array


array = np.array([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

# printing array

print(" 2-D Array : ")

print(array)

# reshaping numpy array

# converting it to 1-D from 2-D array

reshaped = array.reshape((9))

# or we can use unknown dimension

# reshaped = array.reshape((-1))

# printing reshaped array

print("Reshaped 1-D Array : ")

print(reshaped)

Reshaping using unknown dimension :-


We can reshape a array although we don’t know all the new dimensions by using -1 as one of the
dimension, but we should know all the other dimension to use unknown dimension

# importing numpy

import numpy as np

# creating a numpy array

array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

# printing array
print("Array : " + str(array))

# reshaping numpy array

# converting it to 3-D from 1-D array

reshaped1 = array.reshape((2, 2, -1))

# printing reshaped array

print("First Reshaped Array : ")

print(reshaped1)

# converting it to 2-D array

reshaped2 = array.reshape((4, -1))

# printing reshaped array

print("Second Reshaped Array : ")

print(reshaped2)

You might also like