Array Manipulation Using NumPy

Array Manipulation Using NumPy

9 mins read8.4K Views Comment
Updated on Aug 27, 2024 13:43 IST

NumPy is a foundation library for scientific computations in Python, literally standing for Numerical Python. It contains sophisticated functions and tools for integrating with other programming languages as well. During data analysis, it is widely used to handle arrays as it offers a powerful n-dimensional array object – as much as 50x faster than a traditional list in Python!

2022_04_Feature-DiagramsE1.jpg

In this article, we will learn ways to perform array manipulation using NumPy. We will be covering the following sections:

Must read: What is Python?

Installing and Importing NumPy

Let’s start with installing the library in your working environment first. Execute the following command in your terminal:


 
Copy code
| <pre class="wp-block-code py">
<pre class="python" style="font-family:monospace">pip install numpy</pre class="python" style="font-family:monospace">
Copy code

Copy code

 
Copy code

 
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace">pip install numpy</pre class="python" style="font-family:monospace">
Copy code

Now let’s import the NumPy library:

<code>
Copy code

 
<pre class="python" style="font-family:monospace">pip install numpy</pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
<code>
Copy code
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
2 weeks
– / –
16 weeks
1.7 K
3 months
– / –
– / –
4.24 K
2 weeks
3 K
3 weeks
– / –
4 months

Creating NumPy Arrays

<code>
Copy code

Arrays are a grid of values containing information in the form of data elements, their location, and type. An array can be a vector (1D) with a single column or a matrix (2D) with multiple columns.

<code>
Copy code

You can create NumPy arrays in the following ways:

<code>
Copy code

Creating arrays from existing lists/tuples – using np.array()

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace">import numpy as np</pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

 
<pre class="python" style="font-family:monospace">import numpy as np</pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
Text

Description automatically generated

<code>
Copy code

Creating an array of zeros – using np.zeros()

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a one-dimensional array
x <span style="color: #66cc66">= <span style="color: black">[<span style="color: #ff4500">2<span style="color: #66cc66">, <span style="color: #ff4500">4<span style="color: #66cc66">, <span style="color: #ff4500">6<span style="color: #66cc66">, <span style="color: #ff4500">8<span style="color: #66cc66">, <span style="color: #ff4500">10<span style="color: black">]
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(x<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code
A close-up of a calculator

Description automatically generated with low confidence

<code>
Copy code

Creating an array of ones – using np.ones()

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a one-dimensional array
x <span style="color: #66cc66">= <span style="color: black">[<span style="color: #ff4500">2<span style="color: #66cc66">, <span style="color: #ff4500">4<span style="color: #66cc66">, <span style="color: #ff4500">6<span style="color: #66cc66">, <span style="color: #ff4500">8<span style="color: #66cc66">, <span style="color: #ff4500">10<span style="color: black">]
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(x<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
A picture containing text

Description automatically generated

<code>
Copy code

Creating fixed-length arrays – using random numbers between 0-1

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a two-dimensional array
y <span style="color: #66cc66">= <span style="color: black">[<span style="color: black">[<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">4<span style="color: #66cc66">,<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">7<span style="color: #66cc66">,<span style="color: #ff4500">8<span style="color: #66cc66">,<span style="color: #ff4500">9<span style="color: black">]<span style="color: black">]
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(y<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code
Text

Description automatically generated

<code>
Copy code

Creating fixed-length arrays – using np.arange()

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a two-dimensional array
y <span style="color: #66cc66">= <span style="color: black">[<span style="color: black">[<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">4<span style="color: #66cc66">,<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">7<span style="color: #66cc66">,<span style="color: #ff4500">8<span style="color: #66cc66">,<span style="color: #ff4500">9<span style="color: black">]<span style="color: black">]
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(y<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

Creating fixed-length arrays – using np.linespace()

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of zeros
arr <span style="color: #66cc66">= np.<span style="color: black">zeros<span style="color: black">(<span style="color: black">(<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: black">)<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

Basic Array Operations

<code>
Copy code

Let’s start by performing basic arithmetic operations such as addition, subtraction, multiplication, and division on two arrays using NumPy:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of zeros
arr <span style="color: #66cc66">= np.<span style="color: black">zeros<span style="color: black">(<span style="color: black">(<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: black">)<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

Addition

<code>
Copy code

To add two or more arrays, you can simply use np.add(array1, array2) or the ‘+’ sign, as shown:

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of ones
arr <span style="color: #66cc66">= np.<span style="color: black">ones<span style="color: black">(<span style="color: black">(<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: black">)<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

Subtraction

<code>
Copy code

To subtract an array from another, use np.subtract(array1, array2) or the ‘-‘ sign, as shown:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of ones
arr <span style="color: #66cc66">= np.<span style="color: black">ones<span style="color: black">(<span style="color: black">(<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: black">)<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
Text

Description automatically generated

<code>
Copy code

Multiplication

<code>
Copy code

To find the product of two arrays, use np.multiply(array1, array2) or the ‘*’ sign, as shown:

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of random numbers
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">random.<span style="color: #dc143c">random<span style="color: black">(<span style="color: black">[<span style="color: #ff4500">3<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: black">]<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code
Text

Description automatically generated

<code>
Copy code

Divide

<code>
Copy code

To divide one array with another, use np.divide(array1, array2) or the ‘/ ‘ sign, as shown:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of random numbers
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">random.<span style="color: #dc143c">random<span style="color: black">(<span style="color: black">[<span style="color: #ff4500">3<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: black">]<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

NumPy Aggregate Functions

<code>
Copy code

Aggregate functions perform an operation on a set of values and produce a single result. The most useful aggregate functions are listed below:

<code>
Copy code
Functions Description
np.sum() Returns the sum of array elements over a given axis.
np.prod() Returns the product of array elements over a given axis.
np.mean() Computes the arithmetic mean along the specified axis.
np.std() Computes the standard deviation along the specified axis.
np.var() Computes the variance along the specified axis.
np.min() Returns the indices of the minimum values along an axis.
np.max() Returns the indices of the maximum values along an axis.
np.all() Checks if all array elements along a given axis evaluate to True.
np.any() Checks if any array element along a given axis evaluates to True.
np.cumsum() Returns the cumulative sum of the elements along a given axis.
np.cumprod() Returns the cumulative product of the elements along a given axis.

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating an array within a given interval
arr <span style="color: #66cc66">= np.<span style="color: black">arange<span style="color: black">(<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">30<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: black">)
arr</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code
Text

Description automatically generated

<code>
Copy code

NumPy Array Manipulation

<code>
Copy code

Reshaping an Array

<code>
Copy code

NumPy offers flexible tools to change the dimension of an array. But what is meant by array dimension? It is how you specify the direction in which you can vary the array elements, as shown:

<code>
Copy code
A picture containing shape

Description automatically generated

<code>
Copy code

One of the most common methods to change the dimension of an array is the reshape() function – commonly used to modify the shape and hence, the dimension of an array. Let’s see how:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating an array within a given interval
arr <span style="color: #66cc66">= np.<span style="color: black">arange<span style="color: black">(<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">30<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: black">)
arr</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

Let’s set the above array to 5 rows and 2 columns that can accommodate all 10 elements of the array: 

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating an array of a given length
arr <span style="color: #66cc66">= np.<span style="color: black">linspace<span style="color: black">(<span style="color: #ff4500">0<span style="color: #66cc66">,<span style="color: #ff4500">20<span style="color: #66cc66">,<span style="color: #ff4500">5<span style="color: black">)
arr</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code
Text

Description automatically generated

<code>
Copy code

Note that, the new array shape must always be compatible with the original shape.

<code>
Copy code

Transposing an Array

<code>
Copy code

The transpose of a matrix, aka a 2D array, is obtained by changing the rows to columns and vice versa. So, if we have an array of shape (x, y), then the transpose of the array will have the shape (y, x).

<code>
Copy code

Let’s transpose the array we just created above. To do so, you can use transpose() or just .T , as shown:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating an array of a given length
arr <span style="color: #66cc66">= np.<span style="color: black">linspace<span style="color: black">(<span style="color: #ff4500">0<span style="color: #66cc66">,<span style="color: #ff4500">20<span style="color: #66cc66">,<span style="color: #ff4500">5<span style="color: black">)
arr</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
A picture containing circle

Description automatically generated

<code>
Copy code

Indexing Arrays

<code>
Copy code

Array indexing uses a square bracket “[ ]”  to get a specific element of an array. Below is the indexing of a 1D and 2D array of ones:

<code>
Copy code
A screenshot of a computer

Description automatically generated with medium confidence

<code>
Copy code

In 2D arrays, indexing is represented by a pair of values – the first value is the row index and the second is the column index.

<code>
Copy code

Let’s say you want to find the value of an element at a particular position. This can be done through the help of indices as shown:

<code>
Copy code

 
Copy code

<code>
Copy code
|
<pre class="python" style="font-family:monospace">a <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: #ff4500">100<span style="color: #66cc66">,<span style="color: #ff4500">200<span style="color: #66cc66">,<span style="color: #ff4500">300<span style="color: black">]<span style="color: black">) <span style="color: #808080;font-style: italic">#1D array
b <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: black">[<span style="color: #ff4500">20<span style="color: #66cc66">,<span style="color: #ff4500">25<span style="color: #66cc66">,<span style="color: #ff4500">30<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">40<span style="color: #66cc66">,<span style="color: #ff4500">50<span style="color: #66cc66">,<span style="color: #ff4500">60<span style="color: black">]<span style="color: black">]<span style="color: black">) <span style="color: #808080;font-style: italic">#2D array</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></pre class="python" style="font-family:monospace">
Copy code
Text

Description automatically generated

<code>
Copy code

 
<pre class="python" style="font-family:monospace">a <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: #ff4500">100<span style="color: #66cc66">,<span style="color: #ff4500">200<span style="color: #66cc66">,<span style="color: #ff4500">300<span style="color: black">]<span style="color: black">) <span style="color: #808080;font-style: italic">#1D array
b <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: black">[<span style="color: #ff4500">20<span style="color: #66cc66">,<span style="color: #ff4500">25<span style="color: #66cc66">,<span style="color: #ff4500">30<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">40<span style="color: #66cc66">,<span style="color: #ff4500">50<span style="color: #66cc66">,<span style="color: #ff4500">60<span style="color: black">]<span style="color: black">]<span style="color: black">) <span style="color: #808080;font-style: italic">#2D array</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

As you can see, the value in the 4th row (index [3]) and the 2nd column (index [1]) is displayed.

<code>
Copy code

Slicing an Array

<code>
Copy code

Array slicing allows you to extract a portion of an array and generate a new array. The slice object is constructed with the following integer parameters in the slice() function:

<code>
Copy code
  • start: specifies where to start slicing from
  • stop: specifies where to stop slicing
  • step: determines the increment between each index for slicing

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Addition
np.<span style="color: black">add<span style="color: black">(a<span style="color: #66cc66">,b<span style="color: black">)
<span style="color: #808080;font-style: italic">#or
a+b</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

As you can see, the sliced array starts from 12, ends at 42 (the last element is excluded), and the values displayed are incremented by 2.

<code>
Copy code

Another way of slicing without the use of parameters:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Subtraction
np.<span style="color: black">subtract<span style="color: black">(a<span style="color: #66cc66">,b<span style="color: black">)
<span style="color: #808080;font-style: italic">#or
a-b</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Multiplication
np.<span style="color: black">multiply<span style="color: black">(a<span style="color: #66cc66">,b<span style="color: black">)
<span style="color: #808080;font-style: italic">#or
a*b</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Division
np.<span style="color: black">divide<span style="color: black">(a<span style="color: #66cc66">,b<span style="color: black">)
<span style="color: #808080;font-style: italic">#or
a/b</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

What about 2D arrays? How do we slice them?

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Mean of array 'a' elements: "<span style="color: #66cc66">, np.<span style="color: black">mean<span style="color: black">(a<span style="color: black">)<span style="color: black">)
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Standard Deviation of array 'a' elements: "<span style="color: #66cc66">, np.<span style="color: black">std<span style="color: black">(a<span style="color: black">)<span style="color: black">)
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Variance of array 'a' elements: "<span style="color: #66cc66">, np.<span style="color: black">var<span style="color: black">(a<span style="color: black">)<span style="color: black">)
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Sum of array 'b' elements: "<span style="color: #66cc66">, np.<span style="color: #008000">sum<span style="color: black">(b<span style="color: black">)<span style="color: black">)
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(<span style="color: #483d8b">"Product of array 'b' elements: "<span style="color: #66cc66">, np.<span style="color: black">prod<span style="color: black">(b<span style="color: black">)<span style="color: black">)</span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #008000"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #483d8b"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

As you can see, we have sliced the first two rows and the first two columns of the 2D array.

<code>
Copy code

Concatenating Arrays

<code>
Copy code

Through concatenate you can join a sequence of arrays along an existing axis. To concatenate two arrays, use np.concatenate(array1, array2), as shown:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating another array within a given interval
np.<span style="color: black">arange<span style="color: black">(<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">40<span style="color: #66cc66">,<span style="color: #ff4500">4<span style="color: black">)</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
A picture containing text

Description automatically generated

<code>
Copy code

Note that by default, axis=0, meaning the arrays are joined on rows. If you want to concatenate on columns, set axis=1:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Reshaping the above 1D array
np.<span style="color: black">arange<span style="color: black">(<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">40<span style="color: #66cc66">,<span style="color: #ff4500">4<span style="color: black">).<span style="color: black">reshape<span style="color: black">(<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: black">)</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
A picture containing watch, clock

Description automatically generated

<code>
Copy code

Joining Arrays

<code>
Copy code

There are various methods you can use to join arrays, most common of which are given below:

<code>
Copy code
  • np.stack() method: joins a sequence of arrays along a new axis 

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Transposing a 2D array
arr <span style="color: #66cc66">= np.<span style="color: black">arange<span style="color: black">(<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">40<span style="color: #66cc66">,<span style="color: #ff4500">4<span style="color: black">).<span style="color: black">reshape<span style="color: black">(<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: black">)
arr.<span style="color: black">transpose<span style="color: black">(<span style="color: black">)
<span style="color: #808080;font-style: italic">#or
arr.<span style="color: black">T</span style="color: black"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
Text

Description automatically generated

<code>
Copy code

Note that by default, the arrays are joined on columns as we have specified axis=1. Alternatively, you can use column_stack() method as shown below.

<code>
Copy code
  • np.column_stack()method: stacks 1-D arrays as columns into a 2-D array

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of random numbers
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">random.<span style="color: #dc143c">random<span style="color: black">(<span style="color: black">(<span style="color: #ff4500">4<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: black">)<span style="color: black">)
arr</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
Text

Description automatically generated with medium confidence

<code>
Copy code
  • np.hstack()method: adds the second array to the columns of the first array

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Locating an element in the array
arr<span style="color: black">[<span style="color: #ff4500">3<span style="color: #66cc66">,<span style="color: #ff4500">1<span style="color: black">]</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code
  • np.vstack()method: combines the second array as new rows in the first array

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating an array
arr <span style="color: #66cc66">= np.<span style="color: black">arange<span style="color: black">(<span style="color: #ff4500">50<span style="color: black">)
<span style="color: #808080;font-style: italic">#Slicing the array
arr_slice <span style="color: #66cc66">= <span style="color: #008000">slice<span style="color: black">(<span style="color: #ff4500">12<span style="color: #66cc66">,<span style="color: #ff4500">42<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: black">) <span style="color: #808080;font-style: italic">#(start,stop,step)
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(arr<span style="color: black">[arr_slice<span style="color: black">]<span style="color: black">)</span style="color: black"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #008000"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
A picture containing text, clock

Description automatically generated

<code>
Copy code

Splitting Arrays

<code>
Copy code

There are various methods you can use to split arrays as well, the most common of which are given below:

<code>
Copy code
  • np.split() method: splits an array into multiple sub-arrays 

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #ff7700;font-weight:bold">print<span style="color: black">(arr<span style="color: black">[<span style="color: #ff4500">3:<span style="color: #ff4500">13<span style="color: black">]<span style="color: black">) <span style="color: #808080;font-style: italic">#Array sliced from index[3] until index[13]</span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
Text

Description automatically generated

<code>
Copy code
  • np.hsplit() method: splits an array horizontally 

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Array sliced from a given index
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(arr<span style="color: black">[<span style="color: #ff4500">32:<span style="color: black">]<span style="color: black">)</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
Table

Description automatically generated

<code>
Copy code
  • np.vsplit() method: splits an array vertically 

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Array sliced until a given index
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(arr<span style="color: black">[:<span style="color: #ff4500">15<span style="color: black">]<span style="color: black">)</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code
A picture containing text

Description automatically generated

<code>
Copy code

Flattening an Array

<code>
Copy code

This operation converts a 2D array to a 1D array. This can be done in two ways:

<code>
Copy code
  • np.ravel() method: flattens the original array 

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D arrays
arr <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: black">[<span style="color: #ff4500">20<span style="color: #66cc66">,<span style="color: #ff4500">25<span style="color: #66cc66">,<span style="color: #ff4500">30<span style="color: #66cc66">,<span style="color: #ff4500">35<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">40<span style="color: #66cc66">,<span style="color: #ff4500">50<span style="color: #66cc66">,<span style="color: #ff4500">60<span style="color: #66cc66">,<span style="color: #ff4500">70<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">45<span style="color: #66cc66">,<span style="color: #ff4500">48<span style="color: #66cc66">,<span style="color: #ff4500">51<span style="color: #66cc66">,<span style="color: #ff4500">54<span style="color: black">]<span style="color: black">]<span style="color: black">)
<span style="color: #808080;font-style: italic">#Extracting specific rows and columns through slicing
<span style="color: #ff7700;font-weight:bold">print<span style="color: black">(arr<span style="color: black">[<span style="color: #ff4500">0:<span style="color: #ff4500">2<span style="color: #66cc66">,<span style="color: #ff4500">0:<span style="color: #ff4500">2<span style="color: black">]<span style="color: black">)</span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: #ff7700;font-weight:bold"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

As you can see, the second and the third rows are concatenated to the first row, thus flattening the array. We can also perform this operation column-wise through the order parameter:

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating two arrays
arr1 <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: black">[<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">2<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: #66cc66">,<span style="color: #ff4500">4<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">2<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: #66cc66">,<span style="color: #ff4500">7<span style="color: #66cc66">,<span style="color: #ff4500">11<span style="color: black">]<span style="color: black">]<span style="color: black">)
arr2 <span style="color: #66cc66">= np.<span style="color: #dc143c">array<span style="color: black">(<span style="color: black">[<span style="color: black">[<span style="color: #ff4500">1<span style="color: #66cc66">,<span style="color: #ff4500">3<span style="color: #66cc66">,<span style="color: #ff4500">5<span style="color: #66cc66">,<span style="color: #ff4500">7<span style="color: black">]<span style="color: #66cc66">, <span style="color: black">[<span style="color: #ff4500">2<span style="color: #66cc66">,<span style="color: #ff4500">4<span style="color: #66cc66">,<span style="color: #ff4500">6<span style="color: #66cc66">,<span style="color: #ff4500">8<span style="color: black">]<span style="color: black">]<span style="color: black">)
<span style="color: #808080;font-style: italic">#Concatenating both arrays
np.<span style="color: black">concatenate<span style="color: black">(<span style="color: black">(arr1<span style="color: #66cc66">, arr2<span style="color: black">)<span style="color: black">)</span style="color: black"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #ff4500"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #dc143c"></span style="color: #66cc66"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code
  • flatten() method: returns the flattened copy of the original array 

<code>
Copy code

 
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Concatenating both arrays on columns
np.<span style="color: black">concatenate<span style="color: black">(<span style="color: black">(arr1<span style="color: #66cc66">, arr2<span style="color: black">)<span style="color: #66cc66">, axis<span style="color: #66cc66">=<span style="color: #ff4500">1<span style="color: black">)</span style="color: black"></span style="color: #ff4500"></span style="color: #66cc66"></span style="color: #66cc66"></span style="color: black"></span style="color: #66cc66"></span style="color: black"></span style="color: black"></span style="color: black"></span style="color: #808080;font-style: italic"></pre class="python" style="font-family:monospace">
Copy code

<code>
Copy code

<code>
Copy code

Endnotes

<code>
Copy code

NumPy is a powerful foundational library in Python and can be used to perform a wide variety of mathematical operations on arrays. It guarantees efficient calculations and offers high-level functions that operate on arrays and matrices.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio