Array Manipulation Using NumPy
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!
In this article, we will learn ways to perform array manipulation using NumPy. We will be covering the following sections:
- Installing and Importing NumPy
- Creating NumPy Arrays
- Basic Array Operations
- NumPy Aggregate Functions
- NumPy Array Manipulation
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:
<pre class="wp-block-code py">
<pre class="python" style="font-family:monospace">pip install numpy</pre class="python" style="font-family:monospace">
<code>
<pre class="python" style="font-family:monospace">pip install numpy</pre class="python" style="font-family:monospace">
Now let’s import the NumPy library:
<code>
<pre class="python" style="font-family:monospace">pip install numpy</pre class="python" style="font-family:monospace">
<code>
Explore Online Python Courses
<code>
Best-suited Python courses for you
Learn Python with these high-rated online courses
Creating NumPy Arrays
<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>
You can create NumPy arrays in the following ways:
<code>
Creating arrays from existing lists/tuples – using np.array()
<code>
<code>
<pre class="python" style="font-family:monospace">import numpy as np</pre class="python" style="font-family:monospace">
<code>
<pre class="python" style="font-family:monospace">import numpy as np</pre class="python" style="font-family:monospace">
<code>
<code>
Creating an array of zeros – using np.zeros()
<code>
<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">
<code>
Creating an array of ones – using np.ones()
<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">
<code>
<code>
Creating fixed-length arrays – using random numbers between 0-1
<code>
<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">
<code>
Creating fixed-length arrays – using np.arange()
<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">
<code>
<code>
Creating fixed-length arrays – using np.linespace()
<code>
<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">
<code>
Basic Array Operations
<code>
Let’s start by performing basic arithmetic operations such as addition, subtraction, multiplication, and division on two arrays using NumPy:
<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">
<code>
Addition
<code>
To add two or more arrays, you can simply use np.add(array1, array2) or the ‘+’ sign, as shown:
<code>
<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">
<code>
Subtraction
<code>
To subtract an array from another, use np.subtract(array1, array2) or the ‘-‘ sign, as shown:
<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">
<code>
<code>
Multiplication
<code>
To find the product of two arrays, use np.multiply(array1, array2) or the ‘*’ sign, as shown:
<code>
<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">
<code>
Divide
<code>
To divide one array with another, use np.divide(array1, array2) or the ‘/ ‘ sign, as shown:
<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">
<code>
<code>
NumPy Aggregate Functions
<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>
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>
<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">
<code>
NumPy Array Manipulation
<code>
Reshaping an Array
<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>
<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>
<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">
<code>
<code>
Let’s set the above array to 5 rows and 2 columns that can accommodate all 10 elements of the array:
<code>
<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">
<code>
Note that, the new array shape must always be compatible with the original shape.
<code>
Transposing an Array
<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>
Let’s transpose the array we just created above. To do so, you can use transpose() or just .T , as shown:
<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">
<code>
<code>
Indexing Arrays
<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>
<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>
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>
<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 arrayb <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">
<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 arrayb <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">
<code>
<code>
As you can see, the value in the 4th row (index [3]) and the 2nd column (index [1]) is displayed.
<code>
Slicing an Array
<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>
- start: specifies where to start slicing from
- stop: specifies where to stop slicing
- step: determines the increment between each index for slicing
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Additionnp.<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">#ora+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">
<code>
<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>
Another way of slicing without the use of parameters:
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Subtractionnp.<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">#ora-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">
<code>
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Multiplicationnp.<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">#ora*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">
<code>
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#NumPy Array Divisionnp.<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">#ora/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">
<code>
<code>
What about 2D arrays? How do we slice them?
<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">
<code>
<code>
As you can see, we have sliced the first two rows and the first two columns of the 2D array.
<code>
Concatenating Arrays
<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>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating another array within a given intervalnp.<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">
<code>
<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>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Reshaping the above 1D arraynp.<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">
<code>
<code>
Joining Arrays
<code>
There are various methods you can use to join arrays, most common of which are given below:
<code>
- np.stack() method: joins a sequence of arrays along a new axis
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Transposing a 2D arrayarr <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">#orarr.<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">
<code>
<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>
- np.column_stack()method: stacks 1-D arrays as columns into a 2-D array
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D array of random numbersarr <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">
<code>
<code>
- np.hstack()method: adds the second array to the columns of the first array
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Locating an element in the arrayarr<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">
<code>
<code>
- np.vstack()method: combines the second array as new rows in the first array
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating an arrayarr <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 arrayarr_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">
<code>
<code>
Splitting Arrays
<code>
There are various methods you can use to split arrays as well, the most common of which are given below:
<code>
- np.split() method: splits an array into multiple sub-arrays
<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">
<code>
<code>
- np.hsplit() method: splits an array horizontally
<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">
<code>
<code>
- np.vsplit() method: splits an array vertically
<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">
<code>
<code>
Flattening an Array
<code>
This operation converts a 2D array to a 1D array. This can be done in two ways:
<code>
- np.ravel() method: flattens the original array
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating a 2D arraysarr <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">
<code>
<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>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Creating two arraysarr1 <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 arraysnp.<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">
<code>
<code>
- flatten() method: returns the flattened copy of the original array
<code>
<pre class="python" style="font-family:monospace"><span style="color: #808080;font-style: italic">#Concatenating both arrays on columnsnp.<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">
<code>
<code>
Endnotes
<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.
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