Python Matrix (2D Array, NumPy), With Examples

Python Matrix

This article will explain what a matrix is and how to use them in the Python Programming Language with the NumPy library.

What is a Matrix

In mathematics and computer programming, a matrix is a two-dimensional array of values.

It is a grid of columns and rows, with each position in the grid holding one of these values.

It is an array because it’s a collection of elements with multiple elements, and it’s two-dimensional because the values exist at two coordinates (i.e., rows and columns).

Defining a Matrix using NumPy

NumPy is a Python library that adds support for large arrays and matrices and tools to manage them.

Here’s how to define a two-dimensional array, or matrix, in Python:

# Import the NumPy library 
import numpy

# Define an array - in this case the height (cm) and age (years) of several people
people = numpy.array([['Jim', 180, 20],
            ['Bob', 189, 18],
            ['Ann', 160, 21],
            ['Amy', 172, 25]])

print(people)

The above code defined the following matrix/array – each row is contained by [] (square brackets), and each value (or value in a column) is separated by a , (comma).

Jim 180 20
Bob 189 18
Ann 160 21
Amy 172 25

Accessing Matrix Row Value

Matrix positions are indexes – numbers that give a column or row position in their order from left to right or top to bottom. Indexes always start counting from 0 for the first position.

So, to print Jim’s details (the first row), access the matrix row index 0:

print(people[0])

Which will output:

['Jim', 180, 20]

Print Ann’s data from row index 2:

print(people[2])

Which will output:

['Ann', 160, 21]

Accessing Matrix Column Value

Column data can be retrieved from each row by also specifying the column index (which also starts counting at 0).

So, to retrieve the value of the first column in the first row (the person’s name):

print(people[0][0])

Which will return:

Jim

To retrieve the value of the 2nd row 3rd column (Bob’s age):

print(people[1][2])

Which will return:

18

Add/Delete/Update Rows

Adding a Single Row to a Matrix

The NumPy append function will append a row to a given matrix:

people = numpy.append(people, [['Tim', 191, 26]], axis=0)

The axis specified (0) is the row – the first coordinate in a two-dimensional array.

The updated people array in this example will replace the existing people array.

Adding Multiple Rows to a Matrix

In the above example, the array of data for Tim is contained in a separate set of square brackets – this is because the values to be appended are supplied as an array so that multiple rows can be added at once:

people = numpy.append(people, [['Tim', 191, 26], ['Pam', 195, 30]], axis=0)

Deleting Rows

Like the NumPy append() function, the delete() function accepts an array of values (in this case, a list of the row indexes to delete) and an axis:

people = numpy.delete(people, [1, 3], axis=0)

Above, the rows at indexes 1 and 3 were deleted – leaving the following array:

[['Jim' '180' '20']
['Ann' '160' '21']
['Tim' '191' '26']
['Pam' '195' '30']]

Updating Rows

Ann grew rapidly after drinking too many energy drinks – so her height must be updated. To update a row, simply re-assign the value at the same index as the existing row:

people[2] = ['Ann', 197, 21]

Above, the record for Ann at index 2 will be replaced with the new values.

Deleting Columns

The same delete() function used to delete rows is used to delete columns – simply change the index from 0 to 1 to specify the second coordinate of the two-dimensional array (i.e., the column):

people = numpy.delete(people, [[1, 2]], axis=1)

This will delete the columns at index 1 and 2 in the matrix, leaving only each person’s name.

Adding Columns

To add a column, use the NumPy insert function.

To add a ‘favorite color’ column to the end of the example people matrix, the following would be used:

people = numpy.insert(people, [3], [['red'], ['blue'], ['green'], ['purple']], axis=1)

Again, the people array is replaced with an updated copy with the new column inserted.

The axis is specified as 1, again because we are updating the column, which is the second array dimension.

The insert() function accepts several parameters – the array to be updated, followed by the index the column should be inserted at (in this case, the third column after nameheight, and age), the values to be inserted into the column, and the previously mentioned axis.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *