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

Numpy NumPy (Numerical Python)

The document provides an overview of NumPy, a core library for scientific computing in Python, highlighting its capabilities in array creation, properties, indexing, mathematical operations, and aggregation functions. It emphasizes the importance of understanding views versus copies to avoid common bugs. The document concludes by suggesting the application of NumPy with other libraries like Pandas, Matplotlib, and Scikit-learn.

Uploaded by

adeleoci5
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)
3 views10 pages

Numpy NumPy (Numerical Python)

The document provides an overview of NumPy, a core library for scientific computing in Python, highlighting its capabilities in array creation, properties, indexing, mathematical operations, and aggregation functions. It emphasizes the importance of understanding views versus copies to avoid common bugs. The document concludes by suggesting the application of NumPy with other libraries like Pandas, Matplotlib, and Scikit-learn.

Uploaded by

adeleoci5
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

for Data Science

Essential concepts every data scientist should know

Numerical Python • Array Computing • Scientific Tools


What is NumPy?

10-100x N-dim Foundation


Faster than pure Python Arrays of any shape For Pandas, SciPy, PyTorch

NumPy (Numerical Python) is the core library for scientific computing in Python. It provides a powerful N-
dimensional array object and a collection of mathematical functions that operate efficiently on entire arrays.

import numpy as np # The standard alias used everywhere

NumPy arrays are stored as contiguous memory blocks — that's why they're so fast.

NumPy for Data Science


Creating Arrays
import numpy as np Key Functions
# From a Python list
a = [Link]([1, 2, 3, 4]) [Link] / [Link]

Fast initialization for matrices


# Special arrays
[Link]((3, 4)) # all 0s
[Link]((2, 3)) # all 1s [Link]
[Link](4) # identity
Like Python range() but returns array
# Ranges
[Link](0, 10, 2) # [0,2,4,6,8]
[Link](0,1,5) # 5 evenly spaced [Link]

For evenly spaced floats (great for plotting)


# Random
[Link](3, 3)
[Link](0, 10, (3,3)) [Link]

For simulations and neural net weights

NumPy for Data Science


Array Properties & Reshaping
Essential Properties Reshaping
Property Returns Example a = [Link](12)
# [Link] => (12,)
.shape Tuple of dims (3, 4)
b = [Link](3, 4)
.ndim # of dimensions 2 # [Link] => (3, 4)

.dtype Data type float64 c = [Link](2, 2, 3)


# 3D array!
.size Total elements 12
[Link]() # always 1D copy
[Link]() # 1D view (faster)

Rule: reshape() works as long as total elements stay the same • Use -1 to auto-calculate a dimension: [Link](3, -
1)

NumPy for Data Science


Indexing & Slicing
a = [Link]([[1,2,3],[4,5,6],[7,8,9]])
0-based indexing
# Single element
a[0, 2] # => 3 (row 0, col 2) First element is always index 0

# Row / column
a[1, :] # row 1 => [4, 5, 6]
a[:, 0] # col 0 => [1, 4, 7] Slices are views
# Slicing Editing a slice changes the original array!
a[0:2, 1:3] # sub-matrix

# Boolean indexing
a[a > 5] # => [6, 7, 8, 9]
Boolean indexing
# Fancy indexing
a[[0, 2], :] # rows 0 and 2 Most common in data cleaning & filtering

NumPy for Data Science


Math Operations & Broadcasting
Element-wise Operations Broadcasting
a = [Link]([1, 2, 3]) a = [Link]([[1,2,3],[4,5,6]])
b = [Link]([4, 5, 6]) # shape: (2, 3)

a + b # [5, 7, 9] a + 10 # adds 10 everywhere


a * b # [4, 10, 18]
a ** 2 # [1, 4, 9] b = [Link]([1, 2, 3])
[Link](a) # [1, 1.41, 1.73] a + b # adds to each row
[Link](a) # e^1, e^2, e^3 # shape (2,3) + (3,) => (2,3)

a * b is element-wise • Use a @ b or [Link](a, b) for matrix multiplication

NumPy for Data Science


Aggregation Functions
a = [Link]([[1,2,3],[4,5,6]]) Understanding axis=
[Link]() # 21 (all elements)
[Link]() # 3.5 No axis
[Link]() # 1
[Link]() # 6 Operates on ALL elements → scalar
[Link]() # standard deviation
[Link]() # variance

# Axis-wise operations axis=0


[Link](axis=0) # [5,7,9] (col sums)
[Link](axis=1) # [6, 15] (row sums) Collapses ROWS → result has shape of columns

[Link](a) # cumulative sum


[Link](a) # index of max axis=1
Collapses COLUMNS → result has shape of rows

NumPy for Data Science


Useful Utility Functions
[Link]() [Link]()
[Link](a > 5, 1, 0) [Link](a, axis=0)

Conditional replacement — like if/else on arrays Returns sorted array along an axis

[Link]() [Link]()
idx = [Link](a) [Link](a, return_counts=True)

Returns indices that would sort the array Unique values (and optionally counts)

[Link]() [Link]()
[Link](a, 0, 1) [Link]([a, b], axis=0)

Clamp values to a range — great for normalizing Join arrays together along an axis

NumPy for Data Science


Copies vs. Views
This is one of the most common sources of bugs in NumPy!

VIEW (Dangerous) COPY (Safe)

a = [Link]([1, 2, 3, 4]) a = [Link]([1, 2, 3, 4])


b = a # b is a VIEW b = [Link]() # true copy

b[0] = 99 b[0] = 99
print(a) # [99, 2, 3, 4] print(a) # [1, 2, 3, 4]
# a was changed! # a is unchanged ✓

# Slices are also views # Rule of thumb:


c = a[1:3] # Always use .copy() when you
c[0] = 0 # modifies a too! # need an independent array

NumPy for Data Science


What You've Learned

• Creating arrays: [Link], zeros, ones, arange, linspace, random


• Array properties: .shape, .ndim, .dtype, .size
• Reshaping: reshape(), flatten(), ravel()
• Indexing & slicing — including boolean and fancy indexing
• Element-wise math, matrix multiplication, and ufuncs
• Aggregations: sum, mean, min, max across axes
• Utility functions: where, sort, argsort, unique, clip
• Views vs copies — and how to avoid bugs with .copy()

Next: Apply NumPy with Pandas, Matplotlib, and Scikit-learn!

You might also like