MediumA Comprehensive Guide to Array Manipulation in Numpy | by Let's Decode | Medium
Sitemap
Let's Decode

Let's Decode

From Complexity to Crystal Clear.......

A Comprehensive Guide to Array Manipulation in Numpy

Array manipulation in Python Numpy

5 min readOct 8, 2023

--

Hello friends, In previous articles we talked about numpy basics, and how to install Numpy on your computer then we discussed how to create numpy arrays, slicing, indexing, and much more. In today's article, we will discuss different array manipulation techniques, element-wise operations, broadcasting, and more methods in Numpy.

Numpy array manipulation
Image source

Table of Contents

Array Manipulation In Numpy

You may easily reshape, concatenate, and split arrays with NumPy’s extensive array manipulation features.

Image source: https://shorturl.at/bDG46
  1. Reshaping Arrays: Using the reshape() function you can modify the array’s shape. Make sure that the new form has the same number of components as the old array. Let's see one example:
Numpy array reshape
Image source
import numpy as np

my_array = np.array([1, 2, 3, 4, 5, 6])

# Reshape the array to a 2x3 matrix
reshaped_array = my_array.reshape(2, 3)

print("Original Array:\n", my_array)
print("Reshaped Array:\n", reshaped_array)

The output will be:

Original Array:
[1 2 3 4 5 6]

Reshaped Array:
[[1 2 3]
[4 5 6]]

2. Concatenating Arrays: You can concatenate (combine) two or more arrays using functions like numpy.concatenate(). Ensure that the dimensions along the specified axis match. Let's see one example:

Press enter or click to view image in full size
Concatenating array in numpy
Image source
import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Concatenate the arrays vertically
vertical_concatenation = np.concatenate((array1, array2))

# Concatenate the arrays horizontally
horizontal_concatenation = np.concatenate((array1, array2), axis=0)

print("Vertical Concatenation:", vertical_concatenation)
print("Horizontal Concatenation:", horizontal_concatenation)

The output will be:

Vertical Concatenation: [1 2 3 4 5 6]
Horizontal Concatenation: [1 2 3 4 5 6]

To learn more about concatenation Click here

Get Let's Decode’s stories in your inbox

Join Medium for free to get updates from this writer.

3. Splitting Numpy Arrays: You can split an array into multiple smaller arrays using functions like numpy.split() or numpy.hsplit() for horizontal splitting. Let’s see one example:

Split array in Numpy
Image source
import numpy as np

my_array = np.array([1, 2, 3, 4, 5, 6])

# Split the array into three equal parts
split_arrays = np.split(my_array, 3)

print("Split Arrays:", split_arrays)

The output will be:

Split Arrays: [array([1, 2]), array([3, 4]), array([5, 6])]

To learn more about splitting Check the docs.

Element Wise Operation

One of the key features of NumPy is its ability to perform element-wise operations on arrays. These operations apply mathematical operations like addition, subtraction, multiplication, and division to each element of an array.

import numpy as np

array1 = np.array([4, 6, 9])
array2 = np.array([4, 5, 6])

add= array1 + array2 # addition '+'
sub= array1 - array2 # subtraction '-'
mul= array1 * array2 # multiplication '*'
div= array1 / array2 # division '/'

print("Array1:", array1)
print("Array2:", array2)
print("addition:", add)
print("subtraction:", sub)
print("multiplication:", mul)
print("division:", div)

The output will be:

Array1: [4 6 9]
Array2: [4 5 6]
add: [8 11 15]
sub: [0 1 3]
mul: [16 30 54]
div: [1 1 1]

You can explore different Numpy’s methods like insert, delete, append, resize, and more.

Broadcasting in Numpy

NumPy introduces the concept of broadcasting, which allows operations on arrays of different shapes without the need for explicit loops. Broadcasting is especially useful when performing element-wise operations on arrays with mismatched dimensions. Let's see one example:

Press enter or click to view image in full size
Image source
import numpy as np

array = np.array([1, 2, 3, 4, 5])
scalar = 10

result = array + scalar

print("Array:", array)
print("Scalar:", scalar)
print("Result:", result)

The output will be:

Array: [1 2 3 4 5]
Scalar: 10
Result: [11 12 13 14 15]
import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([[2], [4], [6]])

result = array1 * array2 # array multiplication

print("Array1:", array1)
print("Array2:\n", array2)
print("Result:\n", result)

The output will be:

Array1: [1 2 3]
Array2:
[[2]
[4]
[6]]
Result:
[[ 2 4 6]
[ 8 16 24]
[12 24 36]]

To learn more about broadcasting visit the documentation.

Aggregation Functions in Numpy

Numerous aggregation functions are available in NumPy for reducing data in arrays. You can use these functions to calculate statistics like sum, mean, median, and more. Let's see one example:

import numpy as np

array1 = np.array([1, 2, 3, 4, 5])

total = np.sum(array1)

print("Array:", array1)
print("Sum:", total)

array2= np.array([3, 1, 2, 4, 5])

mean = np.mean(array2)
median = np.median(array2)

print("Array:", array2)
print("Mean:", mean)
print("Median:", median)

The output will be:

Array: [1 2 3 4 5]
Sum: 15
Array: [3 1 2 4 5]
Mean: 3.0
Median: 3.0

To learn more about aggregation functions see docs.

Conclusion

We explored NumPy in more detail in this article, expanding on our prior understanding of array creation, slicing, and indexing. We looked at key array manipulation strategies that let us easily reshape, concatenate, and split arrays. We customized arrays using the reshape() function, and concatenate() allowed us to merge arrays both vertically and horizontally.

We showed how NumPy makes it easy to do math with numbers in arrays. NumPy can add, subtract, multiply, or divide these numbers, and it’s good at it!

We were able to conduct operations on arrays of various shapes without costly loops thanks to the concept of broadcasting, which opened up new possibilities. As a result, NumPy is a reliable tool for managing a variety of datasets. Finally, we looked at aggregation functions like sum, mean, and median that help with data reduction and statistical analysis.

“If you learn something new from this article, please show your support by giving it a clap. Your appreciation motivates me to create more articles for you. Thank you for your encouragement!”

--

--

Written by Let's Decode

From Complexity to Crystal Clear.......