0% found this document useful (0 votes)
14 views3 pages

NumPy Array Operations Guide

This document provides a comprehensive guide on various NumPy operations including joining, splitting, searching, sorting, filtering, and iterating over arrays. It includes code examples for each operation to illustrate their usage. The guide highlights the flexibility and functionality of NumPy for array manipulation.

Uploaded by

daddycool0603
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)
14 views3 pages

NumPy Array Operations Guide

This document provides a comprehensive guide on various NumPy operations including joining, splitting, searching, sorting, filtering, and iterating over arrays. It includes code examples for each operation to illustrate their usage. The guide highlights the flexibility and functionality of NumPy for array manipulation.

Uploaded by

daddycool0603
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

Here’s a detailed guide with examples for joining, splitting, searching, sorting, filtering, and

iterating in NumPy.

1. Join Arrays

You can join arrays using functions like [Link], [Link], [Link], and
[Link].

Examples:

import numpy as np

# Concatenate
a = [Link]([1, 2])
b = [Link]([3, 4])
print([Link]((a, b))) # Output: [1 2 3 4]

# Stack
print([Link]((a, b), axis=0)) # Output: [[1 2]
# [3 4]]

# Horizontal Stack
print([Link]((a, b))) # Output: [1 2 3 4]

# Vertical Stack
print([Link]((a, b))) # Output: [[1 2]
# [3 4]]

2. Split Arrays

You can split arrays using functions like [Link], [Link], and [Link].

Examples:

# Split equally
arr = [Link]([1, 2, 3, 4, 5, 6])
print([Link](arr, 3)) # Output: [array([1, 2]), array([3, 4]), array([5, 6])]

# Horizontal Split
matrix = [Link]([[1, 2], [3, 4], [5, 6]])
print([Link](matrix, 2)) # Output: [array([[1],
# [3],
# [5]]), array([[2],
# [4],
# [6]])]

# Vertical Split
print([Link](matrix, 3)) # Output: [array([[1, 2]]), array([[3, 4]]), array([[5, 6]])]

3. Search in Arrays

You can search for specific values or conditions using [Link], [Link], or
[Link].

Examples:

# Where
arr = [Link]([10, 20, 30, 40])
print([Link](arr == 20)) # Output: (array([1]),)

# Searchsorted
sorted_arr = [Link]([1, 3, 5, 7])
print([Link](sorted_arr, 4)) # Output: 2

# Nonzero
print([Link](arr > 25)) # Output: (array([2, 3]),)

4. Sort Arrays

You can sort arrays using [Link].

Examples:

# Sort
arr = [Link]([3, 1, 4, 2])
print([Link](arr)) # Output: [1 2 3 4]

# Sort multidimensional array


matrix = [Link]([[3, 2], [1, 4]])
print([Link](matrix, axis=0)) # Output: [[1 2]
# [3 4]]

5. Filter Arrays

You can filter arrays using boolean indexing or [Link].

Examples:

# Boolean Indexing
arr = [Link]([10, 20, 30, 40])
filter = arr > 20
print(arr[filter]) # Output: [30 40]

# Extract
print([Link](filter, arr)) # Output: [30 40]

6. Iterate Over Arrays

You can iterate using loops or [Link].

Examples:

# Iterating over 1D array


arr = [Link]([1, 2, 3])
for x in arr:
print(x) # Output: 1 2 3

# Iterating over 2D array


matrix = [Link]([[1, 2], [3, 4]])
for x in [Link](matrix):
print(x) # Output: 1 2 3 4

# Enumerate with index


for idx, x in [Link](matrix):
print(idx, x) # Output: (0, 0) 1, (0, 1) 2, (1, 0) 3, (1, 1) 4

These examples demonstrate the versatility of NumPy operations. Let me know if you'd like
more details on any specific topic!

You might also like