Python informer

Improve your Python coding skills

Creating random data in numpy

This article is part of a series on numpy. In this section we will look at how to create numpy arrays initialised with random data. There are various ways to create an array of random numbers in numpy. If you read the numpy documentation, you will find that most of the random functions have several variants that do more or less the same thing. They might vary in minor ways - parameter order, whether the value range is inclusive or exclusive etc. Read more →

Creating data series in numpy

This article is part of a series on numpy. In this section we will look at how to create numpy arrays initialised with data series. arange arange works in a similar way to the built in range function, except that it creates a numpy array. The other difference is that it can work with floating point values: r1 = np.arange(4.9) print(r1) r2 = np.arange(.5, 4.9) print(r2) r3 = np.arange(.5, 4. Read more →

Creating numpy arrays from existing data

This article is part of a series on numpy. In this section we will look at how to create numpy arrays from existing data. From an existing sequence An easy way to create an array based on data is to use thearray function: import numpy as np k1 = [1, 3, 5, 7] d1 = np.array(k1) print(d1) This creates a numpy array d1 based on the list k1: Read more →

Creating numpy arrays with fixed values

This article is part of a series on numpy. In this section we will look at how to create numpy arrays with fixed content (such as all zeros). We will first look at the zeros function, that creates an array full of zeros. We will use that to see how to: Create arrays of different shapes. Create arrays using different data types (such as floats and ints). Create like arrays (arrays that copy the shape and type of another array). Read more →

Data types

This article is part of a series on numpy. numpy supports five main data types - ints, unsigned ints, floats, complex numbers, and booleans. Integers Integers in Python can represent positive or negative numbers of any size. That is because Python integers are objects, and the implementation automatically grabs more memory if necessary to store very large values. Integers in numpy are very different. An integer occupies a fixed number of bytes. Read more →

Numpy - contents

This section covers numpy, a library for performing efficient calculations on large numerical arrays. Introduction Introduction to numpy - an overview of the numpy library. Anatomy of a numpy array - arrays of different shapes and sizes. Numpy efficiency - how numpy arrays acheive efficiency. Creating arrays Creating numpy arrays Fixed value arrays - creating arrays that are filled with a fixed value (eg all zeros). Data series - creating arrays that are filled with a data series. Read more →

Advanced vectorisation in in numpy

This article is part of a series on numpy. One of the key benefits of numpy is its ability to perform operations on an entire array with a single operator or function call. This means that you avoid executing a relatively slow Python loop, and instead use numpy to execute the loop in optimised C code. There are various ways to do this that cover most of the common looping scenarios you might meet when processing large arrays. Read more →

Anatomy of a numpy array

This article is part of a series on numpy. 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. Read more →

Creating numpy arrays

This article is part of a series on numpy. There are various ways to create numpy arrays: Filled with a fixed value such as all zeros. Filled with a series of values such as all 1, 2, 3…. Filled with existing data. Filled with random data. This section also covers the different data types that arrays can use. Visit the PythonInformer Discussion Forum for numeric Python. Read more →

Indexing and slicing numpy arrays

This article is part of a series on numpy. 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 →