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

Introduction to NumPy

NumPy is an open-source Python library designed for numerical and scientific computing, providing support for large, multi-dimensional arrays and matrices along with high-level mathematical functions. Key features include the N-dimensional array object (ndarray), broadcasting, linear algebra support, and efficient memory usage, making it essential for data science and machine learning. NumPy is faster than traditional Python lists due to its memory efficiency and locality of reference, and it serves as the foundation for other data science libraries.

Uploaded by

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

Introduction to NumPy

NumPy is an open-source Python library designed for numerical and scientific computing, providing support for large, multi-dimensional arrays and matrices along with high-level mathematical functions. Key features include the N-dimensional array object (ndarray), broadcasting, linear algebra support, and efficient memory usage, making it essential for data science and machine learning. NumPy is faster than traditional Python lists due to its memory efficiency and locality of reference, and it serves as the foundation for other data science libraries.

Uploaded by

Ayush Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to NumPy

NumPy (short for Numerical Python) is a powerful open-source library in Python


that is used for numerical and scientific computing. It provides support for large,
multi-dimensional arrays and matrices, along with a collection of high-level
mathematical functions to operate on these arrays efficiently.
It is widely used in data science, machine learning, and scientific computing due
to its speed and ease of use.

🔹 Key Features of NumPy


1. N-Dimensional Array Object (ndarray)
o At the core of NumPy is the ndarray, a fast and flexible container
for large datasets.
o Unlike Python lists, NumPy arrays are homogeneous (all elements
must be of the same type) and more memory efficient.
2. Broadcasting
o Allows arithmetic operations on arrays of different shapes without
writing complex code.
o For example, you can add a scalar to an array or perform operations
between differently-shaped arrays.
3. Mathematical Functions
o NumPy provides a wide range of vectorized mathematical
operations like mean(), sum(), std(), dot(), exp(), sin(), and many
more.
o These are implemented in C and are faster than native Python
loops.
4. Linear Algebra Support
o Includes functions for matrix multiplication, determinants,
eigenvalues, solving linear equations, etc.
5. Random Number Generation
o Built-in module for generating random numbers (uniform, normal,
binomial, etc.), which is useful for simulations and machine
learning.
6. Efficient Memory Usage
o NumPy arrays consume less memory and are more efficient for
large data operations compared to native Python data structures
like lists.
7. Integration with Other Libraries
o NumPy is the foundation for other data science libraries like
Pandas, SciPy, Matplotlib, Scikit-learn, and more.
8. Tools for Reading/Writing Array Data
o Functions like np.loadtxt(), np.genfromtxt(), and np.save() make it
easy to read/write data from files.
9. Indexing, Slicing, and Iterating
o Powerful and flexible ways to access and manipulate array data.

🔸 Example
python
CopyEdit
import numpy as np

# Create a 2D array
a = np.array([[1, 2], [3, 4]])

# Perform matrix operations


b = np.array([[5, 6], [7, 8]])
c=a+b
print(c)

What is NumPy?
NumPy is a Python library used for working with arrays.
It also has functions for working in domain of linear algebra, fourier transform,
and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open source project and
you can use it freely.
NumPy stands for Numerical Python.

Why Use NumPy?


In Python we have lists that serve the purpose of arrays, but they are slow to
process.
NumPy aims to provide an array object that is up to 50x faster than traditional
Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting
functions that make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are
very important.
Data Science: is a branch of computer science where we study how to store,
use and analyze data for deriving information from it.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also it is optimized to
work with latest CPU architectures.
Which Language is NumPy written in?
NumPy is a Python library and is written partially in Python, but most of the parts
that require fast computation are written in C or C++.
Where is the NumPy Codebase?
The source code for NumPy is located at this github
repository https://github.com/numpy/numpy
github: enables many people to work on the same codebase.
Create a NumPy ndarray Object
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first element has
index 0, and the second has index 1 etc.
To access elements from 2-D arrays we can use comma separated integers
representing the dimension and the index of the element.
Think of 2-D arrays like a table with rows and columns, where the dimension
represents the row and the index represents the column.
import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st row: ', arr[0, 1])


To access elements from 3-D arrays we can use comma separated integers
representing the dimensions and the index of the element.

Example
Access the third element of the second array of the first array:
import numpy as np

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

print(arr[0, 1, 2])
Negative Indexing
Use negative indexing to access an array from the end.
Example
Print the last element from the 2nd dim:
import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('Last element from 2nd dim: ', arr[1, -1])


Data Types in NumPy
NumPy has some extra data types, and refer to data types with one character,
like i for integers, u for unsigned integers etc.
Below is a list of all data types in NumPy and the characters used to represent
them.
 i - integer
 b - boolean
 u - unsigned integer
 f - float
 c - complex float
 m - timedelta
 M - datetime
 O - object
 S - string
 U - unicode string
 V - fixed chunk of memory for other type ( void )
 The Difference Between Copy and
View
 The main difference between a copy and a view of an array is that the
copy is a new array, and the view is just a view of the original array.
 The copy owns the data and any changes made to the copy will not
affect original array, and any changes made to the original array will
not affect the copy.
 The view does not own the data and any changes made to the view will
affect the original array, and any changes made to the original array
will affect the view.

You might also like