[go: up one dir, main page]

0% found this document useful (0 votes)
8 views10 pages

NumPy The Foundation of Scientific Computing in Python

Uploaded by

amnakhalidgsr
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)
8 views10 pages

NumPy The Foundation of Scientific Computing in Python

Uploaded by

amnakhalidgsr
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/ 10

NumPy: The Foundation of

Scientific Computing in
Python
Unlocking the power of numerical operations for data science and machine
learning.
Agenda

What We'll Cover Today


01 02 03

NumPy Fundamentals Array Manipulation Advanced NumPy


Introduction, importance, and core Creation, operations, indexing, slicing, and Mathematical functions, broadcasting, and
features. reshaping. random number generation.

04 05

Practical Applications Summary & Next Steps


Real-world use cases, code examples, and visualizations. Key takeaways and resources for continued learning.
What is NumPy?
NumPy (Numerical Python) is the fundamental package for
scientific computing with Python. It provides:

N-dimensional array object: The powerful ndarray,


offering efficient storage and operations for large datasets.
Sophisticated (broadcasting) functions: Tools for applying
operations across arrays of different shapes.
Tools for integrating C/C++ and Fortran code: Enhancing
performance for critical sections.
Useful linear algebra, Fourier transform, and random
number capabilities: Essential for mathematical and
statistical analysis.
Why NumPy Matters: Key Features & Advantages

Performance Memory Efficiency


NumPy operations are implemented in C, making them ndarray stores data in contiguous blocks of memory,
significantly faster than pure Python equivalents, reducing overhead and improving cache utilization.
especially for large arrays.

Foundation for Ecosystem Vectorization


Many scientific libraries (SciPy, Pandas, Scikit-learn, Allows operations to be applied to entire arrays at once,
Matplotlib) are built on NumPy, leveraging its array eliminating slow Python loops and improving
object and speed. readability.
Array Creation & Basic Operations
NumPy arrays (ndarrays) are the core. Let's see how to create and manipulate them.

Creating Arrays Basic Arithmetic Operations

import numpy as np a = np.array([10, 20, 30])


b = np.array([1, 2, 3])
# From a list
arr1 = np.array([1, 2, 3]) # Element-wise addition
print(arr1) print(a + b)

# All zeros # Element-wise multiplication


zeros = np.zeros((2, 3)) print(a * b)
print(zeros)
# Scalar multiplication
# All ones print(a * 2)
ones = np.ones((2, 2))
print(ones) # Dot product
dot_prod = a.dot(b)
# Range of numbers print(dot_prod)
arange = np.arange(0, 10, 2)
print(arange)
Indexing, Slicing & Reshaping
Accessing and reorganizing data in arrays is crucial for data manipulation.

Indexing & Slicing Reshaping Arrays

arr = np.array([[1, 2, 3], data = np.arange(12) # [0, 1, ..., 11]


[4, 5, 6], print("Original:\n", data)
[7, 8, 9]])
# Reshape to 3 rows, 4 columns
# Element access reshaped = data.reshape(3, 4)
print(arr[0, 1]) # Output: 2 print("Reshaped:\n", reshaped)

# Row slice # Flatten back to 1D


print(arr[1, :]) # Output: [4 5 6] flattened = reshaped.flatten()
print("Flattened:\n", flattened)
# Column slice
print(arr[:, 2]) # Output: [3 6 9] # Transpose
transposed = reshaped.T
# Sub-array print("Transposed:\n", transposed)
print(arr[0:2, 1:3])
# Output: [[2 3]
# [5 6]]
Powerful Features: Broadcasting & Random Module

Broadcasting Random Module

NumPy's broadcasting allows arithmetic operations on arrays of Generate random numbers for simulations, initializations, and
different shapes. more.

a = np.array([1, 2, 3]) # Single random float [0.0, 1.0)


b = 5 # Scalar print(np.random.rand())
print(a + b) # Output: [6 7 8]
# Array of random floats
matrix = np.array([[1, 2, 3], [4, 5, 6]]) print(np.random.rand(2, 3))
row_vec = np.array([10, 20, 30])
print(matrix + row_vec) # Random integers
# Output: [[11 22 33] print(np.random.randint(0, 10, size=(2, 2)))
# [14 25 36]]
# Normal distribution (mean=0, std=1)
print(np.random.normal(0, 1, size=5))

# Uniform distribution
print(np.random.uniform(0, 1, size=5))

# Gamma distribution (shape=1, scale=2)


print(np.random.gamma(1, 2, size=5))
NumPy in Action: Real-World Applications
NumPy is the backbone of the Python scientific computing stack.

Data Analysis Machine Learning & AI


Efficiently handling large datasets, statistical Basis for array operations in Scikit-learn, TensorFlow,
computations, and data cleaning (e.g., in Pandas). PyTorch. Used for feature engineering, model training,
and data preparation.

Image Processing Scientific Research


Images are represented as multi-dimensional arrays, Simulations, numerical modeling, signal processing, and
enabling operations like filtering, resizing, and complex mathematical problem-solving across various
transformations. scientific domains.
Visualizing Data with NumPy & Matplotlib
NumPy provides the data, Matplotlib makes it visual.

Example: Sine Wave Plot

import matplotlib.pyplot as plt


import numpy as np

# Generate x values using NumPy


x = np.linspace(0, 2 * np.pi, 100)

# Calculate sine values using NumPy


y = np.sin(x)

plt.figure(figsize=(8, 4))
plt.plot(x, y, color='#f98ac7ff', linewidth=2)
plt.title('Sine Wave using NumPy and Matplotlib')
plt.xlabel('Angle (radians)')
plt.ylabel('Sine Value')
plt.grid(True)
plt.show()
This simple example demonstrates how effortlessly NumPy
arrays integrate with Matplotlib for powerful data visualization.
Summary & Next Steps

Key Takeaways Further Exploration


NumPy is foundational: Essential for numerical computing NumPy Official Documentation
in Python, especially for data science and ML. Coursera: Python for Data Science, AI & Development
Efficiency matters: Its ndarray and vectorized operations SciPy (Scientific Python): Extends NumPy with more
provide speed and memory efficiency. advanced scientific functions.
Versatile: From basic arithmetic to complex statistical
Practice creating and manipulating arrays. The best way to learn
distributions and linear algebra.
is by doing!
Ecosystem Integrator: Powers many other popular Python
libraries.

You might also like