Python informer

Improve your Python coding skills

Avoiding loops in numpy

Suppose we had a numpy array a, and we wanted to add 1 to each element of the array. We could do it like this: for i, x in enumerate(a): a[i] = x + 1 The problem here is that the for loop is being executed in Python. Now Python is fairly efficient, but it will usually be quite a bit slower than native code. And if the array is very large, that can be quite a performance hit. Read more →

Anatomy of a numpy array

Numpy arrays come is various types, shapes and sizes. In this article will look at different array parameters, and learn the correct terms used by numpy. Rank The rank of an array is simply the number of axes (or dimensions) it has. A simple list has rank 1: A 2 dimensional array (sometimes called a matrix) has rank 2: A 3 dimensional array has rank 3. It is shown here as a stack of matrices Read more →

Creating numpy arrays

In this section we will look at how to create numpy arrays. This article shows various ways to create arrays with default content (such as all zeros), later articles show how to create arrays with data content. zeros function The zeros function creates a new array containing zeros. You must specify the shape. For example: import numpy as np a1 = np.zeros(4) print(a1) a2 = np.zeros((2, 3)) print(a2) a2_ints = np. Read more →

Indexing and slicing numpy arrays

In this section we will look at indexing and slicing. These work in a similar way to indexing and slicing with standard Python lists, with a few differences Indexing an array Indexing is used to obtain individual elements from an array, but it can also be used to obtain entire rows, columns or planes from multi-dimensional arrays. Indexing in 1 dimension We can create 1 dimensional numpy array from a list like this: Read more →

Image processing with numpy

In this section we will learn how to use numpy to store and manipulate image data. We will use the Python Imaging library (PIL) to read and write data to standard file formats. If you want to learn more about numpy in general, try the other tutorials. Before trying these examples you will need to install the numpy and pillow packages (pillow is a fork of the PIL library). Read more →


numpy is a python library for working with large arrays of data - for example sound or image data. Data is stored as primitive int or float types, and is processed using native code for efficiency. Quick tour of numpy - the basics of using numpy. Creating numpy arrays - different ways to create intialised numpy arrays. Anatomy of a numpy array - the correct terms to describe various types of numpy array. Read more →

Numpy efficiency

When we talk about the efficiency of numpy arrays, what exactly do we mean? There are really two separate issues: Efficiency of memory usage. Processing efficiency. Memory usage Numpy is written in C, which is quite a low level language. Values are stored directly in memory. If you want to store an 8-bit integer using C, it takes exactly one byte. In Python, numbers are stored as objects. Read more →

Quick tour of numpy

Numpy is a Python package that allows you to efficiently store and process large arrays of numerical data. Obvious examples of this type of data are sound data and image data, but numpy can also be used anywhere you have large data sets to process. Part of the attraction of numpy is that it uses simple and familiar Python syntax to perform complex operations on arrays, which simplifies your code. Read more →