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

Practical Guide To NumPy For Data Science

The document provides an overview of NumPy, an open-source Python library for numerical computing and data science. It outlines key NumPy concepts like multi-dimensional arrays, broadcasting, and mathematical functions. The document is structured as a guide with 8 chapters covering NumPy fundamentals like installation, array creation and manipulation, input/output, and advanced techniques.

Uploaded by

Riddhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
256 views

Practical Guide To NumPy For Data Science

The document provides an overview of NumPy, an open-source Python library for numerical computing and data science. It outlines key NumPy concepts like multi-dimensional arrays, broadcasting, and mathematical functions. The document is structured as a guide with 8 chapters covering NumPy fundamentals like installation, array creation and manipulation, input/output, and advanced techniques.

Uploaded by

Riddhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

@RAMCHANDRAPADWAL

Practical Guide
to NumPy for
Data Science

A STEP-BY-STEP GUIDE
Table of Contents
Introduction to NumPy
What is NumPy?
Why Use NumPy in Data Science?
Installing and Importing NumPy
2.1 Installation Instructions
2.2 Importing NumPy
NumPy Arrays
3.1 Creating NumPy Arrays
3.2 Array Attributes
3.3 Indexing and Slicing Arrays
3.4 Array Operations 3.5 Broadcasting
Array Manipulation
4.1 Reshaping Arrays
4.2 Joining and Splitting Arrays
4.3 Changing Array Data Types
4.4 Sorting and Searching Arrays
Mathematical Functions
5.1 Basic Mathematical Operations
5.2 Trigonometric Functions
5.3 Exponential and Logarithmic Functions
5.4 Statistical Functions
5.5 Linear Algebra Operations
Array Input and Output
6.1 Saving and Loading Arrays
6.2 Text File Input and Output
6.3 Binary File Input and Output
6.4 Compressed File Input and Output
Advanced NumPy Features
7.1 Broadcasting in Depth
7.2 Array Manipulation Tricks
7.3 Fancy Indexing and Boolean Masking
7.4 Structured Arrays
7.5 Universal Functions
Performance Tips and Best Practices
8.1 Vectorization
8.2 Memory Efficiency
8.3 Use NumPy Functions Instead of Loops
Conclusion

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.1

Introduction to
NumPy

A Step-by-Step Guide
1.1 WHAT IS NUMPY?
NumPy (Numerical Python) is an open-source Python library
that provides powerful tools for performing numerical
computations and working with multi-dimensional arrays. It
serves as the foundation for many other scientific computing
libraries in Python and is widely used in data science, machine
learning, and scientific research.

1.2 WHY USE NUMPY IN DATA SCIENCE?


NumPy offers several advantages for data science tasks:
Efficient array operations: NumPy provides fast and efficient
implementations of mathematical operations on arrays,
making it suitable for large-scale data processing.
Multi-dimensional arrays: NumPy arrays allow for efficient
storage and manipulation of multi-dimensional data, such
as matrices or tensors.
Broadcasting: NumPy supports broadcasting, which
enables operations between arrays of different shapes,
making code concise and readable.
Integration with other libraries: NumPy seamlessly
integrates with other data science libraries such as Pandas,
Matplotlib, and scikit-learn, providing a comprehensive
ecosystem for data analysis and machine learning.

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.2

Installing and
Importing NumPy

A Step-by-Step Guide
2.1 Installation Instructions
To install NumPy, you can use the Python package manager,
pip. Open your command prompt or terminal and run the
following command:

This will download and install the latest version of NumPy.

2.2 Importing NumPy


Once NumPy is installed, you can import it into your Python
script or Jupyter Notebook using the following import
statement:

The "np" alias is commonly used for NumPy to shorten the


code.

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.3

NumPy Arrays

A Step-by-Step Guide
3.1 Creating NumPy Arrays
1. NumPy arrays are the fundamental data structure in
NumPy. They can be created in several ways:

Using the numpy.array() function:


This function takes a sequence-like object (e.g., list,
tuple) and creates a NumPy array from it.

OUTPUT:

Using the numpy.arange() function:


This function creates an array with regularly spaced
values within a specified range.

OUTPUT:

Using the numpy.zeros() and numpy.ones() functions:


These functions create arrays filled with zeros or
ones, respectively.

OUTPUT:

@RAMCHANDRAPADWAL
OUTPUT:

3.2 Array Attributes


NumPy arrays have various attributes that provide useful
information about the array's shape, size, and data type. Some
commonly used attributes include:
shape: Returns a tuple representing the size of each
dimension of the array.
dtype: Returns the data type of the array elements.
ndim: Returns the number of array dimensions.
size: Returns the total number of elements in the array.
itemsize: Returns the size in bytes of each array element.

3.3 Indexing and Slicing Arrays


NumPy arrays can be accessed and sliced using indexing and
slicing operations. The indexing starts at 0, and negative
indices can be used to access elements from the end of the
array.

@RAMCHANDRAPADWAL
3.4 Array Operations
NumPy provides a wide range of mathematical and logical
operations on arrays. These operations are applied element-
wise, and they can be performed between arrays of
compatible shapes.

3.5 Broadcasting
Broadcasting is a powerful feature of NumPy that allows
arithmetic operations between arrays of different shapes. It
eliminates the need for explicit looping and simplifies the
code.

OUTPUT:

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.4

Array Manipulation

A Step-by-Step Guide
4.1 Reshaping Arrays
NumPy provides several methods to reshape arrays, including
reshape(), flatten(), and ravel(). Reshaping allows you to
change the dimensions of an array without changing the
underlying data.

OUTPUT:

4.2 Joining and Splitting Arrays


NumPy allows you to concatenate or stack arrays together
using functions like concatenate(), vstack(), and hstack().
Similarly, you can split arrays into multiple smaller arrays using
split(), vsplit(), and hsplit().

OUTPUT:

@RAMCHANDRAPADWAL
4.3 Changing Array Data Types
NumPy provides functions to change the data type of an array,
such as astype(). This is particularly useful when you need to
convert an array to a different data type for computation or
storage.

OUTPUT:

4.4 Sorting and Searching Arrays


NumPy offers functions for sorting arrays, such as sort() and
argsort(). You can also search for elements using functions
like where() and searchsorted().

OUTPUT:

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.5

Mathematical
Functions

A Step-by-Step Guide
5.1 Basic Mathematical Operations
NumPy provides a wide range of mathematical functions for
performing basic operations on arrays. These functions
include add(), subtract(), multiply(), divide(),and more.

5.2 Trigonometric Functions


NumPy provides various trigonometric functions, such as sin(),
cos(), tan(), arcsin(), arccos(), and arctan(). These functions
operate element-wise on arrays.

5.3 Exponential and Logarithmic Functions


NumPy provides various trigonometric functions, such as sin(),
cos(), tan(), arcsin(), arccos(), and arctan(). These functions
operate element-wise on arrays.

@RAMCHANDRAPADWAL
5.4 Statistical Functions
NumPy offers a variety of statistical functions for analyzing
arrays, such as mean(), median(), std(), var(), min(), and max().

5.5 Linear Algebra Operations


NumPy provides linear algebra functions for matrix operations,
such as dot(), transpose(), inverse(), and eig().

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.6

Array Input and


Output

A Step-by-Step Guide
6.1 Saving and Loading Arrays
NumPy provides functions to save and load arrays from disk.
You can use np.save() to save an array to a binary file and
np.load() to load the array back into memory.

OUTPUT:

6.2 Text File Input and Output


NumPy allows you to save and load arrays in text file format
using np.savetxt() and np.loadtxt(). This is useful for sharing
data with other applications or for human-readable storage.

OUTPUT:

@RAMCHANDRAPADWAL
6.3 Binary File Input and Output
NumPy supports saving and loading arrays in binary format
using np.save() and np.load(). Binary files are more space-
efficient and faster to read and write compared to text files.

OUTPUT:

6.4 Compressed File Input and Output

NumPy allows you to save and load compressed arrays using


np.savez() and np.load() with the .npz file extension. This is
useful for saving memory and reducing storage space.

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.7

Advanced NumPy
Features

A Step-by-Step Guide
7.1 Broadcasting in Depth
Broadcasting is a powerful feature of NumPy that allows arrays
of different shapes to be combined in arithmetic operations.
NumPy automatically applies broadcasting rules to make the
arrays compatible for the operation.

OUTPUT:

7.2 Array Manipulation Tricks


NumPy provides various tricks for manipulating arrays
efficiently, such as swapping axes, repeating elements, and
stacking arrays.

@RAMCHANDRAPADWAL
7.3 Fancy Indexing and Boolean Masking
NumPy supports advanced indexing techniques, such as fancy
indexing and boolean masking, to select specific elements or
subsets of arrays based on conditions.

7.4 Structured Arrays


NumPy allows you to create structured arrays where each
element can have multiple fields with different data types. This
is useful when dealing with structured data or tabular data.

@RAMCHANDRAPADWAL
7.5 Universal Functions
NumPy provides universal functions (ufuncs) that operate
element-wise on arrays, such as add(), subtract(), multiply(),
and divide(). These functions are optimized for performance.

np.float64)])

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

CHAPTER N.8

Performance Tips
and Best
Practices

A Step-by-Step Guide
8.1 Vectorization
NumPy's strength lies in vectorized operations, where
functions are applied to entire arrays instead of looping over
individual elements. This results in faster execution and
cleaner code.

8.2 Memory Efficiency


NumPy arrays are memory-efficient compared to Python lists,
especially for large datasets. However, be cautious of
creating unnecessary copies of arrays, as it can consume
additional memory.

@RAMCHANDRAPADWAL
8.3 Use NumPy Functions Instead of Loops
NumPy provides a wide range of functions for common
operations, such as sum, mean, min, max, and more. Using
these functions instead of explicit loops can significantly
improve performance.

@RAMCHANDRAPADWAL
@RAMCHANDRAPADWAL

Conclusion
NumPy is a powerful library for data science, providing
efficient and flexible data structures and functions for
numerical computing. In this practical guide, we covered the
basics of NumPy, including array creation, indexing and slicing,
array operations, array manipulation, mathematical functions,
input and output, advanced features, performance tips, and
best practices. With this knowledge, you can leverage NumPy
to perform various data science tasks efficiently and
effectively. Happy coding!

You might also like