Nick McCullum Headshot

Nick McCullum

Software Developer & Professional Explainer

NumPy Indexing and Assignment

Hey - Nick here! This page is a free excerpt from my $199 course Python for Finance, which is 50% off for the next 50 students.

If you want the full course, click here to sign up.

In this lesson, we will explore indexing and assignment in NumPy arrays.

The Array I'll Be Using In This Lesson

As before, I will be using a specific array through this lesson. This time it will be generated using the np.random.rand method. Here's how I generated the array:

Here is the actual array:

To make this array easier to look at, I will round every element of the array to 2 decimal places using NumPy's round method:

Here's the new array:

How To Return A Specific Element From A NumPy Array

We can select (and return) a specific element from a NumPy array in the same way that we could using a normal Python list: using square brackets.

An example is below:

We can also reference multiple elements of a NumPy array using the colon operator. For example, the index [2:] selects every element from index 2 onwards. The index [:3] selects every element up to and excluding index 3. The index [2:4] returns every element from index 2 to index 4, excluding index 4. The higher endpoint is always excluded.

A few example of indexing using the colon operator are below.

Element Assignment in NumPy Arrays

We can assign new values to an element of a NumPy array using the = operator, just like regular python lists. A few examples are below (note that this is all one code block, which means that the element assignments are carried forward from step to step).

arr[2:5] = 0.5

Returns array([0. , 0. , 0.5, 0.5, 0.5])

As you can see, modifying second_new_array also changed the value of new_array .

Why is this?

By default, NumPy does not create a copy of an array when you reference the original array variable using the = assignment operator. Instead, it simply points the new variable to the old variable, which allows the second variable to make modification to the original variable - even if this is not your intention.

This may seem bizarre, but it does have a logical explanation. The purpose of array referencing is to conserve computing power. When working with large data sets, you would quickly run out of RAM if you created a new array every time you wanted to work with a slice of the array.

Fortunately, there is a workaround to array referencing. You can use the copy method to explicitly copy a NumPy array.

An example of this is below.

As you can see below, making modifications to the copied array does not alter the original.

So far in the lesson, we have only explored how to reference one-dimensional NumPy arrays. We will now explore the indexing of two-dimensional arrays.

Indexing Two-Dimensional NumPy Arrays

To start, let's create a two-dimensional NumPy array named mat :

There are two ways to index a two-dimensional NumPy array:

  • mat[row, col]
  • mat[row][col]

I personally prefer to index using the mat[row][col] nomenclature because it is easier to visualize in a step-by-step fashion. For example:

You can also generate sub-matrices from a two-dimensional NumPy array using this notation:

Array referencing also applies to two-dimensional arrays in NumPy, so be sure to use the copy method if you want to avoid inadvertently modifying an original array after saving a slice of it into a new variable name.

Conditional Selection Using NumPy Arrays

NumPy arrays support a feature called conditional selection , which allows you to generate a new array of boolean values that state whether each element within the array satisfies a particular if statement.

An example of this is below (I also re-created our original arr variable since its been awhile since we've seen it):

You can also generate a new array of values that satisfy this condition by passing the condition into the square brackets (just like we do for indexing).

An example of this is below:

Conditional selection can become significantly more complex than this. We will explore more examples in this section's associated practice problems.

In this lesson, we explored NumPy array indexing and assignment in thorough detail. We will solidify your knowledge of these concepts further by working through a batch of practice problems in the next section.

NumPy: Get and set values in an array using various indexing

This article explains how to get and set values, such as individual elements or subarrays (e.g., rows or columns), in a NumPy array ( ndarray ) using various indexing.

  • Indexing on ndarrays — NumPy v1.26 Manual

Basics of selecting values in an ndarray

Specify with integers, specify with slices, specify with a list of boolean values: boolean indexing, specify with a list of integers: fancy indexing, combine different specification formats, assign new values to selected ranges, views and copies.

See the following articles for information on deleting, concatenating, and adding to ndarray .

  • NumPy: Delete rows/columns from an array with np.delete()
  • NumPy: Concatenate arrays with np.concatenate, np.stack, etc.
  • NumPy: Insert elements, rows, and columns into an array with np.insert()

The NumPy version used in this article is as follows. Note that functionality may vary between versions.

Individual elements or subarrays (such as rows or columns) in an ndarray can be selected by specifying their positions or ranges in each dimension with commas, as in [○, ○, ○, ...] . The trailing : can be omitted, making [○, ○, :, :] equivalent to [○, ○] .

For a 2D array, [i] selects the ith row, and [:, i] selects the ith column (indexes start from 0 ). More details will be provided later.

Positions in each dimension can be specified not only as integers but also in other formats such as lists or slices, allowing for the selection of any subarray.

Positions (indexes) are specified as integers ( int ).

Indexes start from 0 , and negative values can be used to specify positions from the end ( -1 represents the last). Specifying a non-existent position results in an error.

The same applies to multi-dimensional arrays. Positions are specified for each dimension.

You can omit the specification of later dimensions.

In a 2D array, [i] , equivalent to [i, :] , selects the ith row as a 1D array, and [:, i] selects the ith column.

Ranges can be selected with slices ( start:end:step ).

  • NumPy: Slicing ndarray

Example with a 1D array:

Example with a 2D array:

The trailing : can be omitted.

Using a slice i:i+1 selects a single row or column, preserving the array's dimensions, unlike selection with an integer ( int ), which reduces the dimensions.

  • NumPy: Get the number of dimensions, shape, and size of ndarray

Slices preserve the original array's dimensions, while integers reduce them. This difference can affect outcomes or cause errors in operations like concatenation, even with the same range selected.

Specifying a list or ndarray of Boolean values ( True or False ) matching the dimensions' sizes selects True positions, similar to masking.

An error occurs if the sizes do not match.

Rows or columns can be extracted using a slice : .

Note that specifying a list of Boolean values for multiple dimensions simultaneously does not yield the expected result. Using np.ix_() is necessary.

  • numpy.ix_ — NumPy v1.26 Manual

As with slices, selecting a range of width 1 (a single row or column) preserves the original array's number of dimensions.

A comparison on an ndarray yields a Boolean ndarray . Using this for indexing with [] selects True values, producing a flattened 1D array.

  • NumPy: Compare two arrays element-wise

Specify multiple conditions using & (AND), | (OR), and ~ (NOT) with parentheses () . Using and , or , not , or omitting parentheses results in an error.

  • How to fix "ValueError: The truth value ... is ambiguous" in NumPy, pandas

For methods of extracting rows or columns that meet certain conditions using Boolean indexing, refer to the following article.

  • NumPy: Extract or delete elements, rows, and columns that satisfy the conditions

It is also possible to select ranges with a list or ndarray of integers.

Order can be inverted or repeated, and using negative values is allowed. Essentially, it involves creating a new array by selecting specific positions from the original array.

As with Boolean indexing, specifying lists for multiple dimensions simultaneously does not yield the expected result. Using np.ix_() is necessary.

As in the 1D example, the order can be inverted or repeated, and negative values are also permissible.

When selecting with a list of one element, the original array's number of dimensions is preserved, in contrast to specifying with an integer.

Different formats can be used to specify each dimension.

A combination of a list of Boolean values and a list of integers requires the use of np.ix_() .

Note that np.ix_() can only accept 1D lists or arrays.

For example, when specifying multiple lists for arrays of three dimensions or more, np.ix_() is required. However, it cannot be combined with integers or slices in the same selection operation.

Integers can be specified as lists containing a single element. In this case, the resulting array retains the same number of dimensions as the original ndarray .

You can use range() to achieve similar functionality as slices. For example, to simulate slicing, retrieve the size of the target dimension using the shape attribute and pass it to range() as in range(a.shape[n])[::] .

  • How to use range() in Python

You can assign new values to selected ranges in an ndarray .

Specifying a scalar value on the right side assigns that value to all elements in the selected range on the left side.

Arrays can also be specified on the right side.

If the shape of the selected range on the left side matches that of the array on the right side, it is directly assigned. Non-contiguous locations pose no problem.

If the shape of the selected range on the left side does not match that of the array on the right side, it is assigned through broadcasting.

An error occurs if the shapes cannot be broadcast.

For more information on broadcasting, refer to the following article.

  • NumPy: Broadcasting rules and examples

The specification format used for each dimension when selecting subarrays determines whether a view or a copy of the original array is returned.

For example, using slices returns a view.

Whether two arrays refer to the same memory can be checked using np.shares_memory() .

  • NumPy: Views and copies of arrays

In the case of a view, changing the value in the selected subarray also changes the value in the original array, and vice versa.

Boolean indexing or fancy indexing returns a copy.

In this case, changing the value in the selected subarray does not affect the original array, and vice versa.

To create a copy of a subarray selected with a slice and process it separately from the original ndarray , use the copy() method.

When combining different specification formats, using Boolean or fancy indexing returns a copy.

Using integers and slices returns a view.

Related Categories

Related articles.

  • NumPy: Functions ignoring NaN (np.nansum, np.nanmean, etc.)
  • NumPy: Generate random numbers with np.random
  • NumPy: Ellipsis (...) for ndarray
  • List of NumPy articles
  • NumPy: arange() and linspace() to generate evenly spaced values
  • NumPy: Trigonometric functions (sin, cos, tan, arcsin, arccos, arctan)
  • NumPy: np.sign(), np.signbit(), np.copysign()
  • NumPy: Set the display format for ndarray
  • NumPy: Meaning of the axis parameter (0, 1, -1)
  • NumPy: Read and write CSV files (np.loadtxt, np.genfromtxt, np.savetxt)
  • OpenCV, NumPy: Rotate and flip image
  • NumPy: Set whether to print full or truncated ndarray
  • NumPy: Calculate cumulative sum and product (np.cumsum, np.cumprod)

logo

Python Numerical Methods

../_images/book_cover.jpg

This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists , the content is also available at Berkeley Python Numerical Methods .

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license . If you find this content useful, please consider supporting the work on Elsevier or Amazon !

< 2.6 Data Structure - Dictionaries | Contents | 2.8 Summary and Problems >

Introducing Numpy Arrays ¶

In the 2nd part of this book, we will study the numerical methods by using Python. We will use array/matrix a lot later in the book. Therefore, here we are going to introduce the most common way to handle arrays in Python using the Numpy module . Numpy is probably the most fundamental numerical computing module in Python.

NumPy is important in scientific computing, it is coded both in Python and C (for speed). On its website, a few important features for Numpy is listed:

a powerful N-dimensional array object

sophisticated (broadcasting) functions

tools for integrating C/C++ and Fortran code

useful linear algebra, Fourier transform, and random number capabilities

Here we will only introduce you the Numpy array which is related to the data structure, but we will gradually touch on other aspects of Numpy in the following chapters.

In order to use Numpy module, we need to import it first. A conventional way to import it is to use “np” as a shortened name.

WARNING! Of course, you could call it any name, but conventionally, “np” is accepted by the whole community and it is a good practice to use it for obvious purposes.

To define an array in Python, you could use the np.array function to convert a list.

TRY IT! Create the following arrays:

\(x = \begin{pmatrix} 1 & 4 & 3 \\ \end{pmatrix}\)

\(y = \begin{pmatrix} 1 & 4 & 3 \\ 9 & 2 & 7 \\ \end{pmatrix}\)

NOTE! A 2-D array could use a nested lists to represent, with the inner list represent each row.

Many times we would like to know the size or length of an array. The array shape attribute is called on an array M and returns a 2 × 3 array where the first element is the number of rows in the matrix M and the second element is the number of columns in M. Note that the output of the shape attribute is a tuple. The size attribute is called on an array M and returns the total number of elements in matrix M.

TRY IT! Find the rows, columns and the total size for array y.

NOTE! You may notice the difference that we only use y.shape instead of y.shape() , this is because shape is an attribute rather than a method in this array object. We will introduce more of the object-oriented programming in a later chapter. For now, you need to remember that when we call a method in an object, we need to use the parentheses, while the attribute don’t.

Very often we would like to generate arrays that have a structure or pattern. For instance, we may wish to create the array z = [1 2 3 … 2000]. It would be very cumbersome to type the entire description of z into Python. For generating arrays that are in order and evenly spaced, it is useful to use the arange function in Numpy.

TRY IT! Create an array z from 1 to 2000 with an increment 1.

Using the np.arange , we could create z easily. The first two numbers are the start and end of the sequence, and the last one is the increment. Since it is very common to have an increment of 1, if an increment is not specified, Python will use a default value of 1. Therefore np.arange(1, 2000) will have the same result as np.arange(1, 2000, 1) . Negative or noninteger increments can also be used. If the increment “misses” the last value, it will only extend until the value just before the ending value. For example, x = np.arange(1,8,2) would be [1, 3, 5, 7].

TRY IT! Generate an array with [0.5, 1, 1.5, 2, 2.5].

Sometimes we want to guarantee a start and end point for an array but still have evenly spaced elements. For instance, we may want an array that starts at 1, ends at 8, and has exactly 10 elements. For this purpose you can use the function np.linspace . linspace takes three input values separated by commas. So A = linspace(a,b,n) generates an array of n equally spaced elements starting from a and ending at b.

TRY IT! Use linspace to generate an array starting at 3, ending at 9, and containing 10 elements.

Getting access to the 1D numpy array is similar to what we described for lists or tuples, it has an index to indicate the location. For example:

For 2D arrays, it is slightly different, since we have rows and columns. To get access to the data in a 2D array M, we need to use M[r, c], that the row r and column c are separated by comma. This is referred to as array indexing. The r and c could be single number, a list and so on. If you only think about the row index or the column index, than it is similar to the 1D array. Let’s use the \(y = \begin{pmatrix} 1 & 4 & 3 \\ 9 & 2 & 7 \\ \end{pmatrix}\) as an example.

TRY IT! Get the element at first row and 2nd column of array y .

TRY IT! Get the first row of array y .

TRY IT! Get the last column of array y .

TRY IT! Get the first and third column of array y .

There are some predefined arrays that are really useful. For example, the np.zeros , np.ones , and np.empty are 3 useful functions. Let’s see the examples.

TRY IT! Generate a 3 by 5 array with all the as 0.

TRY IT! Generate a 5 by 3 array with all the element as 1.

NOTE! The shape of the array is defined in a tuple with row as the first item, and column as the second. If you only need a 1D array, then it could be only one number as the input: np.ones(5) .

TRY IT! Generate a 1D empty array with 3 elements.

NOTE! The empty array is not really empty, it is filled with random very small numbers.

You can reassign a value of an array by using array indexing and the assignment operator. You can reassign multiple elements to a single number using array indexing on the left side. You can also reassign multiple elements of an array as long as both the number of elements being assigned and the number of elements assigned is the same. You can create an array using array indexing.

TRY IT! Let a = [1, 2, 3, 4, 5, 6]. Reassign the fourth element of A to 7. Reassign the first, second, and thrid elements to 1. Reassign the second, third, and fourth elements to 9, 8, and 7.

TRY IT! Create a zero array b with shape 2 by 2, and set \(b = \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ \end{pmatrix}\) using array indexing.

WARNING! Although you can create an array from scratch using indexing, we do not advise it. It can confuse you and errors will be harder to find in your code later. For example, b[1, 1] = 1 will give the result \(b = \begin{pmatrix} 0 & 0 \\ 0 & 1 \\ \end{pmatrix}\) , which is strange because b[0, 0], b[0, 1], and b[1, 0] were never specified.

Basic arithmetic is defined for arrays. However, there are operations between a scalar (a single number) and an array and operations between two arrays. We will start with operations between a scalar and an array. To illustrate, let c be a scalar, and b be a matrix.

b + c , b − c , b * c and b / c adds a to every element of b, subtracts c from every element of b, multiplies every element of b by c, and divides every element of b by c, respectively.

TRY IT! Let \(b = \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ \end{pmatrix}\) . Add and substract 2 from b. Multiply and divide b by 2. Square every element of b. Let c be a scalar. On your own, verify the reflexivity of scalar addition and multiplication: b + c = c + b and cb = bc.

Describing operations between two matrices is more complicated. Let b and d be two matrices of the same size. b − d takes every element of b and subtracts the corresponding element of d. Similarly, b + d adds every element of d to the corresponding element of b.

TRY IT! Let \(b = \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ \end{pmatrix}\) and \(d = \begin{pmatrix} 3 & 4 \\ 5 & 6 \\ \end{pmatrix}\) . Compute b + d and b - d.

There are two different kinds of matrix multiplication (and division). There is element-by-element matrix multiplication and standard matrix multiplication. For this section, we will only show how element-by-element matrix multiplication and division work. Standard matrix multiplication will be described in later chapter on Linear Algebra. Python takes the * symbol to mean element-by-element multiplication. For matrices b and d of the same size, b * d takes every element of b and multiplies it by the corresponding element of d. The same is true for / and **.

TRY IT! Compute b * d, b / d, and b**d.

The transpose of an array, b, is an array, d, where b[i, j] = d[j, i]. In other words, the transpose switches the rows and the columns of b. You can transpose an array in Python using the array method T .

TRY IT! Compute the transpose of array b.

Numpy has many arithmetic functions, such as sin, cos, etc., can take arrays as input arguments. The output is the function evaluated for every element of the input array. A function that takes an array as input and performs the function on it is said to be vectorized .

TRY IT! Compute np.sqrt for x = [1, 4, 9, 16].

Logical operations are only defined between a scalar and an array and between two arrays of the same size. Between a scalar and an array, the logical operation is conducted between the scalar and each element of the array. Between two arrays, the logical operation is conducted element-by-element.

TRY IT! Check which elements of the array x = [1, 2, 4, 5, 9, 3] are larger than 3. Check which elements in x are larger than the corresponding element in y = [0, 2, 3, 1, 2, 3].

Python can index elements of an array that satisfy a logical expression.

TRY IT! Let x be the same array as in the previous example. Create a variable y that contains all the elements of x that are strictly bigger than 3. Assign all the values of x that are bigger than 3, the value 0.

Python Data Science Artwork

Look Ma, No for Loops: Array Programming With NumPy

Table of Contents

Getting into Shape: Intro to NumPy Arrays

Counting: easy as 1, 2, 3…, buy low, sell high, intermezzo: understanding axes notation.

  • Broadcasting

Clustering Algorithms

Amortization tables, image feature extraction, a parting thought: don’t over-optimize, more resources.

It is sometimes said that Python, compared to low-level languages such as C++ , improves development time at the expense of runtime. Fortunately, there are a handful of ways to speed up operation runtime in Python without sacrificing ease of use. One option suited for fast numerical operations is NumPy, which deservedly bills itself as the fundamental package for scientific computing with Python.

Granted, few people would categorize something that takes 50 microseconds (fifty millionths of a second) as “slow.” However, computers might beg to differ. The runtime of an operation taking 50 microseconds (50 μs) falls under the realm of microperformance , which can loosely be defined as operations with a runtime between 1 microsecond and 1 millisecond.

Why does speed matter? The reason that microperformance is worth monitoring is that small differences in runtime become amplified with repeated function calls: an incremental 50 μs of overhead, repeated over 1 million function calls, translates to 50 seconds of incremental runtime.

When it comes to computation, there are really three concepts that lend NumPy its power:

  • Vectorization

In this tutorial, you’ll see step by step how to take advantage of vectorization and broadcasting , so that you can use NumPy to its full capacity. While you will use some indexing in practice here, NumPy’s complete indexing schematics, which extend Python’s slicing syntax , are their own beast. If you’re looking to read more on NumPy indexing, grab some coffee and head to the Indexing section in the NumPy docs.

Free Bonus: Click here to get access to a free NumPy Resources Guide that points you to the best tutorials, videos, and books for improving your NumPy skills.

The fundamental object of NumPy is its ndarray (or numpy.array ), an n-dimensional array that is also present in some form in array-oriented languages such as Fortran 90, R, and MATLAB , as well as predecessors APL and J.

Let’s start things off by forming a 3-dimensional array with 36 elements:

Picturing high-dimensional arrays in two dimensions can be difficult. One intuitive way to think about an array’s shape is to simply “read it from left to right.” arr is a 3 by 4 by 3 array:

Visually, arr could be thought of as a container of three 4x3 grids (or a rectangular prism) and would look like this:

NumPy 3 dimensional array

Higher dimensional arrays can be tougher to picture, but they will still follow this “arrays within an array” pattern.

Where might you see data with greater than two dimensions?

  • Panel data can be represented in three dimensions. Data that tracks attributes of a cohort (group) of individuals over time could be structured as (respondents, dates, attributes) . The 1979 National Longitudinal Survey of Youth follows 12,686 respondents over 27 years. Assuming that you have ~500 directly asked or derived data points per individual, per year, this data would have shape (12686, 27, 500) for a total of 177,604,000 data points.
  • Color-image data for multiple images is typically stored in four dimensions. Each image is a three-dimensional array of (height, width, channels) , where the channels are usually red, green, and blue (RGB) values. A collection of images is then just (image_number, height, width, channels) . One thousand 256x256 RGB images would have shape (1000, 256, 256, 3) . (An extended representation is RGBA, where the A–alpha–denotes the level of opacity.)

For more detail on real-world examples of high-dimensional data, see Chapter 2 of François Chollet’s Deep Learning with Python .

What is Vectorization?

Vectorization is a powerful ability within NumPy to express operations as occurring on entire arrays rather than their individual elements. Here’s a concise definition from Wes McKinney:

This practice of replacing explicit loops with array expressions is commonly referred to as vectorization. In general, vectorized array operations will often be one or two (or more) orders of magnitude faster than their pure Python equivalents, with the biggest impact [seen] in any kind of numerical computations. [ source ]

When looping over an array or any data structure in Python, there’s a lot of overhead involved. Vectorized operations in NumPy delegate the looping internally to highly optimized C and Fortran functions, making for cleaner and faster Python code.

As an illustration, consider a 1-dimensional vector of True and False for which you want to count the number of “False to True” transitions in the sequence:

With a Python for loop , one way to do this would be to evaluate, in pairs, the truth value of each element in the sequence along with the element that comes right after it:

In vectorized form, there’s no explicit for loop or direct reference to the individual elements:

How do these two equivalent functions compare in terms of performance? In this particular case, the vectorized NumPy call wins out by a factor of about 70 times:

Technical Detail : Another term is vector processor , which is related to a computer’s hardware. When I speak about vectorization here, I’m referring to concept of replacing explicit for loops with array expressions, which in this case can then be computed internally with a low-level language.

Here’s another example to whet your appetite. Consider the following classic technical interview problem:

Given a stock’s price history as a sequence, and assuming that you are only allowed to make one purchase and one sale, what is the maximum profit that can be obtained? For example, given prices = (20, 18, 14, 17, 20, 21, 15) , the max profit would be 7, from buying at 14 and selling at 21.

(To all of you finance people: no, short-selling is not allowed.)

There is a solution with n-squared time complexity that consists of taking every combination of two prices where the second price “comes after” the first and determining the maximum difference.

However, there is also an O(n) solution that consists of iterating through the sequence just once and finding the difference between each price and a running minimum . It goes something like this:

Can this be done in NumPy? You bet. But first, let’s build a quasi-realistic example:

Here’s what this looks like with matplotlib . The adage is to buy low (green) and sell high (red):

An illustration showing stock’s price history as a sequence

What does the NumPy implementation look like? While there is no np.cummin() “directly,” NumPy’s universal functions (ufuncs) all have an accumulate() method that does what its name implies:

Extending the logic from the pure-Python example, you can find the difference between each price and a running minimum (element-wise) , and then take the max of this sequence:

How do these two operations, which have the same theoretical time complexity , compare in actual runtime? First, let’s take a longer sequence. (This doesn’t necessarily need to be a time series of stock prices at this point.)

Now, for a somewhat unfair comparison:

Above, treating profit_with_numpy() as pseudocode (without considering NumPy’s underlying mechanics), there are actually three passes through a sequence:

  • cummin(prices) has O(n) time complexity
  • prices - cummin(prices) is O(n)
  • max(...) is O(n)

This reduces to O(n), because O(3n) reduces to just O(n)–the n “dominates” as n approaches infinity.

Therefore, these two functions have equivalent worst-case time complexity . (Although, as a side note, the NumPy function comes with significantly more space complexity.) But that is probably the least important takeaway here. One lesson is that, while theoretical time complexity is an important consideration, runtime mechanics can also play a big role. Not only can NumPy delegate to C, but with some element-wise operations and linear algebra , it can also take advantage of computing within multiple threads. But there are a lot of factors at play here, including the underlying library used (BLAS/LAPACK/Atlas), and those details are for a whole ‘nother article entirely.

In NumPy, an axis refers to a single dimension of a multidimensional array:

The terminology around axes and the way in which they are described can be a bit unintuitive. In the documentation for Pandas (a library built on top of NumPy), you may frequently see something like:

axis : {'index' (0), 'columns' (1)}

You could argue that, based on this description, the results above should be “reversed.” However, the key is that axis refers to the axis along which a function gets called. This is well articulated by Jake VanderPlas:

The way the axis is specified here can be confusing to users coming from other languages. The axis keyword specifies the dimension of the array that will be collapsed, rather than the dimension that will be returned. So, specifying axis=0 means that the first axis will be collapsed: for two-dimensional arrays, this means that values within each column will be aggregated. [ source ]

In other words, summing an array for axis=0 collapses the rows of the array with a column-wise computation.

With this distinction in mind, let’s move on to explore the concept of broadcasting.

Broadcasting is another important NumPy abstraction. You’ve already seen that operations between two NumPy arrays (of equal size) operate element-wise :

But, what about unequally sized arrays? This is where broadcasting comes in:

The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. [ source ]

The way in which broadcasting is implemented can become tedious when working with more than two arrays. However, if there are just two arrays, then their ability to be broadcasted can be described with two short rules:

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions and works its way forward. Two dimensions are compatible when: they are equal, or one of them is 1

That’s all there is to it.

Let’s take a case where we want to subtract each column-wise mean of an array, element-wise:

In statistical jargon, sample consists of two samples (the columns) drawn independently from two populations with means of 2 and 20, respectively. The column-wise means should approximate the population means (albeit roughly, because the sample is small):

Now, subtracting the column-wise means is straightforward because broadcasting rules check out:

Here’s an illustration of subtracting out column-wise means, where a smaller array is “stretched” so that it is subtracted from each row of the larger array:

NumPy array broadcasting

Technical Detail : The smaller-sized array or scalar is not literally stretched in memory: it is the computation itself that is repeated.

This extends to standardizing each column as well, making each cell a z-score relative to its respective column:

However, what if you want to subtract out, for some reason, the row-wise minimums? You’ll run into a bit of trouble:

The problem here is that the smaller array, in its current form, cannot be “stretched” to be shape-compatible with sample . You actually need to expand its dimensionality to meet the broadcasting rules above:

Note : [:, None] is a means by which to expand the dimensionality of an array, to create an axis of length one. np.newaxis is an alias for None .

There are some significantly more complex cases, too. Here’s a more rigorous definition of when any arbitrary number of arrays of any NumPy shape can be broadcast together:

A set of arrays is called “broadcastable” to the same NumPy shape if the following rules produce a valid result, meaning one of the following is true : The arrays all have exactly the same shape. The arrays all have the same number of dimensions, and the length of each dimension is either a common length or 1. The arrays that have too few dimensions can have their NumPy shapes prepended with a dimension of length 1 to satisfy property #2. [ source ]

This is easier to walk through step by step. Let’s say you have the following four arrays:

Before checking shapes, NumPy first converts scalars to arrays with one element:

Now we can check criterion #1. If all of the arrays have the same shape, a set of their shapes will condense down to one element, because the set() constructor effectively drops duplicate items from its input. This criterion is clearly not met:

The first part of criterion #2 also fails, meaning the entire criterion fails:

The final criterion is a bit more involved:

The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy property #2.

To codify this, you can first determine the dimensionality of the highest-dimension array and then prepend ones to each NumPy shape tuple until all are of equal dimension:

Finally, you need to test that the length of each dimension is either (drawn from) a common length, or 1 . A trick for doing this is to first mask the array of NumPy “shape-tuples” in places where it equals one. Then, you can check if the peak-to-peak ( np.ptp() ) column-wise differences are all zero:

Encapsulated in a single function, this logic looks like this:

Luckily, you can take a shortcut and use np.broadcast() for this sanity-check, although it’s not explicitly designed for this purpose:

For those interested in digging a little deeper, PyArray_Broadcast is the underlying C function that encapsulates broadcasting rules.

Array Programming in Action: Examples

In the following 3 examples, you’ll put vectorization and broadcasting to work with some real-world applications.

Machine learning is one domain that can frequently take advantage of vectorization and broadcasting. Let’s say that you have the vertices of a triangle (each row is an x, y coordinate):

The centroid of this “cluster” is an (x, y) coordinate that is the arithmetic mean of each column:

It’s helpful to visualize this:

Image of a triangle

Many clustering algorithms make use of Euclidean distances of a collection of points, either to the origin or relative to their centroids.

In Cartesian coordinates, the Euclidean distance between points p and q is:

Formula for calculating Euclidean distance between points

[ source: Wikipedia ]

So for the set of coordinates in tri from above, the Euclidean distance of each point from the origin (0, 0) would be:

You may recognize that we are really just finding Euclidean norms:

Instead of referencing the origin, you could also find the norm of each point relative to the triangle’s centroid:

Finally, let’s take this one step further: let’s say that you have a 2d array X and a 2d array of multiple (x, y) “proposed” centroids. Algorithms such as K-Means clustering work by randomly assigning initial “proposed” centroids, then reassigning each data point to its closest centroid. From there, new centroids are computed, with the algorithm converging on a solution once the re-generated labels (an encoding of the centroids) are unchanged between iterations. A part of this iterative process requires computing the Euclidean distance of each point from each centroid :

In other words, we want to answer the question, to which centroid does each point within X belong ? We need to do some reshaping to enable broadcasting here, in order to calculate the Euclidean distance between each point in X and each point in centroids :

This enables us to cleanly subtract one array from another using a combinatoric product of their rows :

In other words, the NumPy shape of X - centroids[:, None] is (2, 10, 2) , essentially representing two stacked arrays that are each the size of X . Next, we want the label (index number) of each closest centroid, finding the minimum distance on the 0th axis from the array above:

You can put all this together in functional form:

Let’s inspect this visually, plotting both the two clusters and their assigned labels with a color-mapping:

Predicted classes color mapping

Vectorization has applications in finance as well.

Given an annualized interest rate, payment frequency (times per year), initial loan balance, and loan term, you can create an amortization table with monthly loan balances and payments, in a vectorized fashion. Let’s set some scalar constants first:

NumPy comes preloaded with a handful of financial functions that, unlike their Excel cousins , are capable of producing vector outputs.

The debtor (or lessee) pays a constant monthly amount that is composed of a principal and interest component. As the outstanding loan balance declines, the interest portion of the total payment declines with it.

Next, you’ll need to calculate a monthly balance, both before and after that month’s payment, which can be defined as the future value of the original balance minus the future value of an annuity (a stream of payments), using a discount factor d :

Diagram of financial formula for calculating future value of original balance

Functionally, this looks like:

Finally, you can drop this into a tabular format with a Pandas DataFrame . Be careful with signs here. PMT is an outflow from the perspective of the debtor.

At the end of year 30, the loan is paid off:

Note : While using floats to represent money can be useful for concept illustration in a scripting environment, using Python floats for financial calculations in a production environment might cause your calculation to be a penny or two off in some cases.

In one final example, we’ll work with an October 1941 image of the USS Lexington (CV-2), the wreck of which was discovered off the coast of Australia in March 2018. First, we can map the image into a NumPy array of its pixel values:

Image of the USS Lexington

For simplicity’s sake, the image is loaded in grayscale, resulting in a 2d array of 64-bit floats rather than a 3-dimensional MxNx4 RGBA array, with lower values denoting darker spots:

One technique commonly employed as an intermediary step in image analysis is patch extraction . As the name implies, this consists of extracting smaller overlapping sub-arrays from a larger array and can be used in cases where it is advantageous to “denoise” or blur an image.

This concept extends to other fields, too. For example, you’d be doing something similar by taking “rolling” windows of a time series with multiple features (variables). It’s even useful for building Conway’s Game of Life . (Although, convolution with a 3x3 kernel is a more direct approach.)

Here, we will find the mean of each overlapping 10x10 patch within img . Taking a miniature example, the first 3x3 patch array in the top-left corner of img would be:

The pure-Python approach to creating sliding patches would involve a nested for loop. You’d need to consider that the starting index of the right-most patches will be at index n - 3 + 1 , where n is the width of the array. In other words, if you were extracting 3x3 patches from a 10x10 array called arr , the last patch taken would be from arr[7:10, 7:10] . Also keep in mind that Python’s range() does not include its stop parameter:

Blurred image of the USS Lexington

With this loop, you’re performing a lot of Python calls.

An alternative that will be scalable to larger RGB or RGBA images is NumPy’s stride_tricks .

An instructive first step is to visualize, given the patch size and image shape, what a higher-dimensional array of patches would look like. We have a 2d array img with shape (254, 319) and a (10, 10) 2d patch. This means our output shape (before taking the mean of each “inner” 10x10 array) would be:

You also need to specify the strides of the new array. An array’s strides is a tuple of bytes to jump in each dimension when moving along the array. Each pixel in img is a 64-bit (8-byte) float, meaning the total image size is 254 x 319 x 8 = 648,208 bytes.

Internally, img is kept in memory as one contiguous block of 648,208 bytes. strides is hence a sort of “metadata”-like attribute that tells us how many bytes we need to jump ahead to move to the next position along each axis . We move in blocks of 8 bytes along the rows but need to traverse 8 x 319 = 2,552 bytes to move “down” from one row to another.

In our case, the strides of the resulting patches will just repeat the strides of img twice:

Now, let’s put these pieces together with NumPy’s stride_tricks :

Here’s the first 10x10 patch:

The last step is tricky. To get a vectorized mean of each inner 10x10 array, we need to think carefully about the dimensionality of what we have now. The result should collapse the last two dimensions so that we’re left with a single 245x310 array.

One (suboptimal) way would be to reshape patches first, flattening the inner 2d arrays to length-100 vectors, and then computing the mean on the final axis:

However, you can also specify axis as a tuple, computing a mean over the last two axes, which should be more efficient than reshaping:

Let’s make sure this checks out by comparing equality to our looped version. It does:

If the concept of strides has you drooling, don’t worry: Scikit-Learn has already embedded this entire process nicely within its feature_extraction module.

In this article, we discussed optimizing runtime by taking advantage of array programming in NumPy. When you are working with large datasets, it’s important to be mindful of microperformance.

However, there is a subset of cases where avoiding a native Python for loop isn’t possible. As Donald Knuth advised , “Premature optimization is the root of all evil.” Programmers may incorrectly predict where in their code a bottleneck will appear, spending hours trying to fully vectorize an operation that would result in a relatively insignificant improvement in runtime.

There’s nothing wrong with for loops sprinkled here and there. Often, it can be more productive to think instead about optimizing the flow and structure of the entire script at a higher level of abstraction.

NumPy Documentation:

  • What is NumPy?
  • Universal functions
  • NumPy for MATLAB Users
  • The complete NumPy Reference index
  • Travis Oliphant’s Guide to NumPy, 2nd ed. (Travis is the primary creator of NumPy)
  • Chapter 2 (“Introduction to NumPy”) of Jake VanderPlas’ Python Data Science Handbook
  • Chapter 4 (“NumPy Basics”) and Chapter 12 (“Advanced NumPy”) of Wes McKinney’s Python for Data Analysis 2nd ed.
  • Chapter 2 (“The Mathematical Building Blocks of Neural Networks”) from François Chollet’s Deep Learning with Python
  • Robert Johansson’s Numerical Python
  • Ivan Idris: Numpy Beginner’s Guide, 3rd ed.

Other Resources:

  • Wikipedia: Array Programming
  • SciPy Lecture Notes: Basic and Advanced NumPy
  • EricsBroadcastingDoc: Array Broadcasting in NumPy
  • SciPy Cookbook: Views versus copies in NumPy
  • Nicolas Rougier: From Python to Numpy and 100 NumPy Exercises
  • TensorFlow docs: Broadcasting Semantics
  • Theano docs: Broadcasting
  • Eli Bendersky: Broadcasting Arrays in Numpy

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Python Tricks Dictionary Merge

About Brad Solomon

Brad Solomon

Brad is a software engineer and a member of the Real Python Tutorial Team.

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Aldren Santos

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal . Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session . Happy Pythoning!

Keep Learning

Related Topics: intermediate data-science numpy

Keep reading Real Python by creating a free account or signing in:

Already have an account? Sign-In

Almost there! Complete this form and click the button below to gain instant access:

NumPy Learning Resources Guide

NumPy: The Best Learning Resources (A Free PDF Guide)

🔒 No spam. We take your privacy seriously.

assignment a numpy array

NumPy Tutorial

Numpy random, numpy ufunc, quiz/exercises, numpy creating arrays, create a numpy ndarray object.

NumPy is used to work with arrays. The array object in NumPy is called ndarray .

We can create a NumPy ndarray object by using the array() function.

type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is numpy.ndarray type.

To create an ndarray , we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray :

Use a tuple to create a NumPy array:

Dimensions in Arrays

A dimension in arrays is one level of array depth (nested arrays).

nested array: are arrays that have arrays as their elements.

Advertisement

0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.

Create a 0-D array with value 42

An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.

These are the most common and basic arrays.

Create a 1-D array containing the values 1,2,3,4,5:

An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:

Check Number of Dimensions?

NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.

Check how many dimensions the arrays have:

Higher Dimensional Arrays

An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by using the ndmin argument.

Create an array with 5 dimensions and verify that it has 5 dimensions:

In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D array.

Test Yourself With Exercises

Insert the correct method for creating a NumPy array.

Start the Exercise

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

  • Python Course
  • Python Basics
  • Interview Questions
  • Python Quiz
  • Popular Packages
  • Python Projects
  • Practice Python
  • AI With Python
  • Learn Python3
  • Python Automation
  • Python Web Dev
  • DSA with Python
  • Python OOPs
  • Dictionaries

Python NumPy – Practice Exercises, Questions, and Solutions

Python NumPy is a general-purpose array processing package. It provides fast and versatile n-dimensional arrays and tools for working with these arrays. It provides various computing tools such as comprehensive mathematical functions, random number generator and it’s easy to use syntax makes it highly accessible and productive for programmers from any background.

Python NumPy - Practice Exercises, Questions, and Solutions

This NumPy exercise will help the learners to get a better understanding of NumPy arrays. This practice page consists of a huge set of NumPy programs like NumPy array, Matrix, handling indexing in NumPy, working with Mathematics. Statistics and all sort of frequently encountered problems.

Questions on NumPy Array

  • How to create an empty and a full NumPy array?
  • Create a Numpy array filled with all zeros
  • Create a Numpy array filled with all ones
  • Check whether a Numpy array contains a specified row
  • How to Remove rows in Numpy array that contains non-numeric values?
  • Remove single-dimensional entries from the shape of an array
  • Find the number of occurrences of a sequence in a NumPy array
  • Find the most frequent value in a NumPy array
  • Combining a one and a two-dimensional NumPy Array
  • How to build an array of all combinations of two NumPy arrays?
  • How to add a border around a NumPy array?
  • How to compare two NumPy arrays?
  • How to check whether specified values are present in NumPy array?
  • How to get all 2D diagonals of a 3D NumPy array?
  • Flatten a Matrix in Python using NumPy
  • Flatten a 2d numpy array into 1d array
  • Move axes of an array to new positions
  • Interchange two axes of an array
  • NumPy – Fibonacci Series using Binet Formula
  • Counts the number of non-zero values in the array
  • Count the number of elements along a given axis
  • Trim the leading and/or trailing zeros from a 1-D array
  • Change data type of given numpy array
  • Reverse a numpy array
  • How to make a NumPy array read-only?

Questions on NumPy Matrix

  • Get the maximum value from given matrix
  • Get the minimum value from given matrix
  • Find the number of rows and columns of a given matrix using NumPy
  • Select the elements from a given matrix
  • Find the sum of values in a matrix
  • Calculate the sum of the diagonal elements of a NumPy array
  • Adding and Subtracting Matrices in Python
  • Ways to add row/columns in numpy array
  • Matrix Multiplication in NumPy
  • Get the eigen values of a matrix
  • How to Calculate the determinant of a matrix using NumPy?
  • How to inverse a matrix using NumPy
  • How to count the frequency of unique values in NumPy array?
  • Multiply matrices of complex numbers using NumPy in Python
  • Compute the outer product of two given vectors using NumPy in Python
  • Calculate inner, outer, and cross products of matrices and vectors using NumPy
  • Compute the covariance matrix of two given NumPy arrays
  • Convert covariance matrix to correlation matrix using Python
  • Compute the Kronecker product of two mulitdimension NumPy arrays
  • Convert the matrix into a list

Questions on NumPy Indexing

  • Replace NumPy array elements that doesn’t satisfy the given condition
  • Return the indices of elements where the given condition is satisfied
  • Replace NaN values with average of columns
  • Replace negative value with zero in numpy array
  • How to get values of an NumPy array at certain index positions?
  • Find indices of elements equal to zero in a NumPy array
  • How to Remove columns in Numpy array that contains non-numeric values?
  • How to access different rows of a multidimensional NumPy array?
  • Get row numbers of NumPy array having element larger than X
  • Get filled the diagonals of NumPy array
  • Check elements present in the NumPy array
  • Combined array index by index

Questions on NumPy Linear Algebra

  • Find a matrix or vector norm using NumPy
  • Calculate the QR decomposition of a given matrix using NumPy
  • Compute the condition number of a given matrix using NumPy
  • Compute the eigenvalues and right eigenvectors of a given square array using NumPy?
  • Calculate the Euclidean distance using NumPy

Questions on NumPy Random

  • Create a Numpy array with random values
  • How to choose elements from the list with different probability using NumPy?
  • How to get weighted random choice in Python?
  • Generate Random Numbers From The Uniform Distribution using NumPy
  • Get Random Elements form geometric distribution
  • Get Random elements from Laplace distribution
  • Return a Matrix of random values from a uniform distribution
  • Return a Matrix of random values from a Gaussian distribution

Questions on NumPy Sorting and Searching

  • How to get the indices of the sorted array using NumPy in Python?
  • Finding the k smallest values of a NumPy array
  • How to get the n-largest values of an array using NumPy?
  • Sort the values in a matrix
  • Filter out integers from float numpy array
  • Find the indices into a sorted array

Questions on NumPy Mathematics

  • How to get element-wise true division of an array using Numpy?
  • How to calculate the element-wise absolute value of NumPy array?
  • Compute the negative of the NumPy array
  • Multiply 2d numpy array corresponding to 1d array
  • Computes the inner product of two arrays
  • Compute the nth percentile of the NumPy array
  • Calculate the n-th order discrete difference along the given axis
  • Calculate the sum of all columns in a 2D NumPy array
  • Calculate average values of two given NumPy arrays
  • How to compute numerical negative value for all elements in a given NumPy array?
  • How to get the floor, ceiling and truncated values of the elements of a numpy array?
  • How to round elements of the NumPy array to the nearest integer?
  • Find the round off the values of the given matrix
  • Determine the positive square-root of an array
  • Evaluate Einstein’s summation convention of two multidimensional NumPy arrays

Questions on NumPy Statistics

  • Compute the median of the flattened NumPy array
  • Find Mean of a List of Numpy Array
  • Calculate the mean of array ignoring the NaN value
  • Get the mean value from given matrix
  • Compute the variance of the NumPy array
  • Compute the standard deviation of the NumPy array
  • Compute pearson product-moment correlation coefficients of two given NumPy arrays
  • Calculate the mean across dimension in a 2D NumPy array
  • Calculate the average, variance and standard deviation in Python using NumPy
  • Describe a NumPy Array in Python

Questions on Polynomial

  • Define a polynomial function
  • How to add one polynomial to another using NumPy in Python?
  • How to subtract one polynomial to another using NumPy in Python?
  • How to multiply a polynomial to another using NumPy in Python?
  • How to divide a polynomial to another using NumPy in Python?
  • Find the roots of the polynomials using NumPy
  • Evaluate a 2-D polynomial series on the Cartesian product
  • Evaluate a 3-D polynomial series on the Cartesian product

Questions on NumPy Strings

  • Repeat all the elements of a NumPy array of strings
  • How to split the element of a given NumPy array with spaces?
  • How to insert a space between characters of all the elements of a given NumPy array?
  • Find the length of each string element in the Numpy array
  • Swap the case of an array of string
  • Change the case to uppercase of elements of an array
  • Change the case to lowercase of elements of an array
  • Join String by a separator
  • Check if two same shaped string arrayss one by one
  • Count the number of substrings in an array
  • Find the lowest index of the substring in an array
  • Get the boolean array when values end with a particular character

More Questions on NumPy

  • Different ways to convert a Python dictionary to a NumPy array
  • How to convert a list and tuple into NumPy arrays?
  • Ways to convert array of strings to array of floats
  • Convert a NumPy array into a csv file
  • How to Convert an image to NumPy array and save it to CSV file using Python?
  • How to save a NumPy array to a text file?
  • Load data from a text file
  • Plot line graph from NumPy array
  • Create Histogram using NumPy

Please Login to comment...

Similar reads, improve your coding skills with practice.

 alt=

What kind of Experience do you want to share?

Structured arrays #

Introduction #.

Structured arrays are ndarrays whose datatype is a composition of simpler datatypes organized as a sequence of named fields . For example,

Here x is a one-dimensional array of length two whose datatype is a structure with three fields: 1. A string of length 10 or less named ‘name’, 2. a 32-bit integer named ‘age’, and 3. a 32-bit float named ‘weight’.

If you index x at position 1 you get a structure:

You can access and modify individual fields of a structured array by indexing with the field name:

Structured datatypes are designed to be able to mimic ‘structs’ in the C language, and share a similar memory layout. They are meant for interfacing with C code and for low-level manipulation of structured buffers, for example for interpreting binary blobs. For these purposes they support specialized features such as subarrays, nested datatypes, and unions, and allow control over the memory layout of the structure.

Users looking to manipulate tabular data, such as stored in csv files, may find other pydata projects more suitable, such as xarray, pandas, or DataArray. These provide a high-level interface for tabular data analysis and are better optimized for that use. For instance, the C-struct-like memory layout of structured arrays in numpy can lead to poor cache behavior in comparison.

Structured datatypes #

A structured datatype can be thought of as a sequence of bytes of a certain length (the structure’s itemsize ) which is interpreted as a collection of fields. Each field has a name, a datatype, and a byte offset within the structure. The datatype of a field may be any numpy datatype including other structured datatypes, and it may also be a subarray data type which behaves like an ndarray of a specified shape. The offsets of the fields are arbitrary, and fields may even overlap. These offsets are usually determined automatically by numpy, but can also be specified.

Structured datatype creation #

Structured datatypes may be created using the function numpy.dtype . There are 4 alternative forms of specification which vary in flexibility and conciseness. These are further documented in the Data Type Objects reference page, and in summary they are:

A list of tuples, one tuple per field

Each tuple has the form (fieldname, datatype, shape) where shape is optional. fieldname is a string (or tuple if titles are used, see Field Titles below), datatype may be any object convertible to a datatype, and shape is a tuple of integers specifying subarray shape.

If fieldname is the empty string '' , the field will be given a default name of the form f# , where # is the integer index of the field, counting from 0 from the left:

The byte offsets of the fields within the structure and the total structure itemsize are determined automatically.

A string of comma-separated dtype specifications

In this shorthand notation any of the string dtype specifications may be used in a string and separated by commas. The itemsize and byte offsets of the fields are determined automatically, and the field names are given the default names f0 , f1 , etc.

A dictionary of field parameter arrays

This is the most flexible form of specification since it allows control over the byte-offsets of the fields and the itemsize of the structure.

The dictionary has two required keys, ‘names’ and ‘formats’, and four optional keys, ‘offsets’, ‘itemsize’, ‘aligned’ and ‘titles’. The values for ‘names’ and ‘formats’ should respectively be a list of field names and a list of dtype specifications, of the same length. The optional ‘offsets’ value should be a list of integer byte-offsets, one for each field within the structure. If ‘offsets’ is not given the offsets are determined automatically. The optional ‘itemsize’ value should be an integer describing the total size in bytes of the dtype, which must be large enough to contain all the fields.

Offsets may be chosen such that the fields overlap, though this will mean that assigning to one field may clobber any overlapping field’s data. As an exception, fields of numpy.object_ type cannot overlap with other fields, because of the risk of clobbering the internal object pointer and then dereferencing it.

The optional ‘aligned’ value can be set to True to make the automatic offset computation use aligned offsets (see Automatic byte offsets and alignment ), as if the ‘align’ keyword argument of numpy.dtype had been set to True.

The optional ‘titles’ value should be a list of titles of the same length as ‘names’, see Field Titles below.

A dictionary of field names

The keys of the dictionary are the field names and the values are tuples specifying type and offset:

This form was discouraged because Python dictionaries did not preserve order in Python versions before Python 3.6. Field Titles may be specified by using a 3-tuple, see below.

Manipulating and displaying structured datatypes #

The list of field names of a structured datatype can be found in the names attribute of the dtype object:

The dtype of each individual field can be looked up by name:

The field names may be modified by assigning to the names attribute using a sequence of strings of the same length.

The dtype object also has a dictionary-like attribute, fields , whose keys are the field names (and Field Titles , see below) and whose values are tuples containing the dtype and byte offset of each field.

Both the names and fields attributes will equal None for unstructured arrays. The recommended way to test if a dtype is structured is with if dt.names is not None rather than if dt.names , to account for dtypes with 0 fields.

The string representation of a structured datatype is shown in the “list of tuples” form if possible, otherwise numpy falls back to using the more general dictionary form.

Automatic byte offsets and alignment #

Numpy uses one of two methods to automatically determine the field byte offsets and the overall itemsize of a structured datatype, depending on whether align=True was specified as a keyword argument to numpy.dtype .

By default ( align=False ), numpy will pack the fields together such that each field starts at the byte offset the previous field ended, and the fields are contiguous in memory.

If align=True is set, numpy will pad the structure in the same way many C compilers would pad a C-struct. Aligned structures can give a performance improvement in some cases, at the cost of increased datatype size. Padding bytes are inserted between fields such that each field’s byte offset will be a multiple of that field’s alignment, which is usually equal to the field’s size in bytes for simple datatypes, see PyArray_Descr.alignment . The structure will also have trailing padding added so that its itemsize is a multiple of the largest field’s alignment.

Note that although almost all modern C compilers pad in this way by default, padding in C structs is C-implementation-dependent so this memory layout is not guaranteed to exactly match that of a corresponding struct in a C program. Some work may be needed, either on the numpy side or the C side, to obtain exact correspondence.

If offsets were specified using the optional offsets key in the dictionary-based dtype specification, setting align=True will check that each field’s offset is a multiple of its size and that the itemsize is a multiple of the largest field size, and raise an exception if not.

If the offsets of the fields and itemsize of a structured array satisfy the alignment conditions, the array will have the ALIGNED flag set.

A convenience function numpy.lib.recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as an argument, and returns a copy with fields re-packed, with or without padding bytes.

Field titles #

In addition to field names, fields may also have an associated title , an alternate name, which is sometimes used as an additional description or alias for the field. The title may be used to index an array, just like a field name.

To add titles when using the list-of-tuples form of dtype specification, the field name may be specified as a tuple of two strings instead of a single string, which will be the field’s title and field name respectively. For example:

When using the first form of dictionary-based specification, the titles may be supplied as an extra 'titles' key as described above. When using the second (discouraged) dictionary-based specification, the title can be supplied by providing a 3-element tuple (datatype, offset, title) instead of the usual 2-element tuple:

The dtype.fields dictionary will contain titles as keys, if any titles are used. This means effectively that a field with a title will be represented twice in the fields dictionary. The tuple values for these fields will also have a third element, the field title. Because of this, and because the names attribute preserves the field order while the fields attribute may not, it is recommended to iterate through the fields of a dtype using the names attribute of the dtype, which will not list titles, as in:

Union types #

Structured datatypes are implemented in numpy to have base type numpy.void by default, but it is possible to interpret other numpy types as structured types using the (base_dtype, dtype) form of dtype specification described in Data Type Objects . Here, base_dtype is the desired underlying dtype, and fields and flags will be copied from dtype . This dtype is similar to a ‘union’ in C.

Indexing and assignment to structured arrays #

Assigning data to a structured array #.

There are a number of ways to assign values to a structured array: Using python tuples, using scalar values, or using other structured arrays.

Assignment from Python Native Types (Tuples) #

The simplest way to assign values to a structured array is using python tuples. Each assigned value should be a tuple of length equal to the number of fields in the array, and not a list or array as these will trigger numpy’s broadcasting rules. The tuple’s elements are assigned to the successive fields of the array, from left to right:

Assignment from Scalars #

A scalar assigned to a structured element will be assigned to all fields. This happens when a scalar is assigned to a structured array, or when an unstructured array is assigned to a structured array:

Structured arrays can also be assigned to unstructured arrays, but only if the structured datatype has just a single field:

Assignment from other Structured Arrays #

Assignment between two structured arrays occurs as if the source elements had been converted to tuples and then assigned to the destination elements. That is, the first field of the source array is assigned to the first field of the destination array, and the second field likewise, and so on, regardless of field names. Structured arrays with a different number of fields cannot be assigned to each other. Bytes of the destination structure which are not included in any of the fields are unaffected.

Assignment involving subarrays #

When assigning to fields which are subarrays, the assigned value will first be broadcast to the shape of the subarray.

Indexing structured arrays #

Accessing individual fields #.

Individual fields of a structured array may be accessed and modified by indexing the array with the field name.

The resulting array is a view into the original array. It shares the same memory locations and writing to the view will modify the original array.

This view has the same dtype and itemsize as the indexed field, so it is typically a non-structured array, except in the case of nested structures.

If the accessed field is a subarray, the dimensions of the subarray are appended to the shape of the result:

Accessing Multiple Fields #

One can index and assign to a structured array with a multi-field index, where the index is a list of field names.

The behavior of multi-field indexes changed from Numpy 1.15 to Numpy 1.16.

The result of indexing with a multi-field index is a view into the original array, as follows:

Assignment to the view modifies the original array. The view’s fields will be in the order they were indexed. Note that unlike for single-field indexing, the dtype of the view has the same itemsize as the original array, and has fields at the same offsets as in the original array, and unindexed fields are merely missing.

In Numpy 1.15, indexing an array with a multi-field index returned a copy of the result above, but with fields packed together in memory as if passed through numpy.lib.recfunctions.repack_fields .

The new behavior as of Numpy 1.16 leads to extra “padding” bytes at the location of unindexed fields compared to 1.15. You will need to update any code which depends on the data having a “packed” layout. For instance code such as:

will need to be changed. This code has raised a FutureWarning since Numpy 1.12, and similar code has raised FutureWarning since 1.7.

In 1.16 a number of functions have been introduced in the numpy.lib.recfunctions module to help users account for this change. These are numpy.lib.recfunctions.repack_fields . numpy.lib.recfunctions.structured_to_unstructured , numpy.lib.recfunctions.unstructured_to_structured , numpy.lib.recfunctions.apply_along_fields , numpy.lib.recfunctions.assign_fields_by_name , and numpy.lib.recfunctions.require_fields .

The function numpy.lib.recfunctions.repack_fields can always be used to reproduce the old behavior, as it will return a packed copy of the structured array. The code above, for example, can be replaced with:

Furthermore, numpy now provides a new function numpy.lib.recfunctions.structured_to_unstructured which is a safer and more efficient alternative for users who wish to convert structured arrays to unstructured arrays, as the view above is often intended to do. This function allows safe conversion to an unstructured type taking into account padding, often avoids a copy, and also casts the datatypes as needed, unlike the view. Code such as:

can be made safer by replacing with:

Assignment to an array with a multi-field index modifies the original array:

This obeys the structured array assignment rules described above. For example, this means that one can swap the values of two fields using appropriate multi-field indexes:

Indexing with an Integer to get a Structured Scalar #

Indexing a single element of a structured array (with an integer index) returns a structured scalar:

Unlike other numpy scalars, structured scalars are mutable and act like views into the original array, such that modifying the scalar will modify the original array. Structured scalars also support access and assignment by field name:

Similarly to tuples, structured scalars can also be indexed with an integer:

Thus, tuples might be thought of as the native Python equivalent to numpy’s structured types, much like native python integers are the equivalent to numpy’s integer types. Structured scalars may be converted to a tuple by calling numpy.ndarray.item :

Viewing structured arrays containing objects #

In order to prevent clobbering object pointers in fields of object type, numpy currently does not allow views of structured arrays containing objects.

Structure comparison and promotion #

If the dtypes of two void structured arrays are equal, testing the equality of the arrays will result in a boolean array with the dimensions of the original arrays, with elements set to True where all fields of the corresponding structures are equal:

NumPy will promote individual field datatypes to perform the comparison. So the following is also valid (note the 'f4' dtype for the 'a' field):

To compare two structured arrays, it must be possible to promote them to a common dtype as returned by numpy.result_type and numpy.promote_types . This enforces that the number of fields, the field names, and the field titles must match precisely. When promotion is not possible, for example due to mismatching field names, NumPy will raise an error. Promotion between two structured dtypes results in a canonical dtype that ensures native byte-order for all fields:

The resulting dtype from promotion is also guaranteed to be packed, meaning that all fields are ordered contiguously and any unnecessary padding is removed:

Note that the result prints without offsets or itemsize indicating no additional padding. If a structured dtype is created with align=True ensuring that dtype.isalignedstruct is true, this property is preserved:

When promoting multiple dtypes, the result is aligned if any of the inputs is:

The < and > operators always return False when comparing void structured arrays, and arithmetic and bitwise operations are not supported.

Changed in version 1.23: Before NumPy 1.23, a warning was given and False returned when promotion to a common dtype failed. Further, promotion was much more restrictive: It would reject the mixed float/integer comparison example above.

Record arrays #

As an optional convenience numpy provides an ndarray subclass, numpy.recarray that allows access to fields of structured arrays by attribute instead of only by index. Record arrays use a special datatype, numpy.record , that allows field access by attribute on the structured scalars obtained from the array. The numpy.rec module provides functions for creating recarrays from various objects. Additional helper functions for creating and manipulating structured arrays can be found in numpy.lib.recfunctions .

The simplest way to create a record array is with numpy.rec.array :

numpy.rec.array can convert a wide variety of arguments into record arrays, including structured arrays:

The numpy.rec module provides a number of other convenience functions for creating record arrays, see record array creation routines .

A record array representation of a structured array can be obtained using the appropriate view :

For convenience, viewing an ndarray as type numpy.recarray will automatically convert to numpy.record datatype, so the dtype can be left out of the view:

To get back to a plain ndarray both the dtype and type must be reset. The following view does so, taking into account the unusual case that the recordarr was not a structured type:

Record array fields accessed by index or by attribute are returned as a record array if the field has a structured type but as a plain ndarray otherwise.

Note that if a field has the same name as an ndarray attribute, the ndarray attribute takes precedence. Such fields will be inaccessible by attribute but will still be accessible by index.

Recarray helper functions #

Collection of utilities to manipulate structured arrays.

Most of these functions were initially implemented by John Hunter for matplotlib. They have been rewritten and extended for convenience.

Add new fields to an existing array.

The names of the fields are given with the names arguments, the corresponding values with the data arguments. If a single field is appended, names , data and dtypes do not have to be lists but just values.

Input array to extend.

String or sequence of strings corresponding to the names of the new fields.

Array or sequence of arrays storing the fields to add to the base.

Datatype or sequence of datatypes. If None, the datatypes are estimated from the data .

Filling value used to pad missing data on the shorter arrays.

Whether to return a masked array or not.

Whether to return a recarray (MaskedRecords) or not.

Apply function ‘func’ as a reduction across fields of a structured array.

This is similar to numpy.apply_along_axis , but treats the fields of a structured array as an extra axis. The fields are all first cast to a common type following the type-promotion rules from numpy.result_type applied to the field’s dtypes.

Function to apply on the “field” dimension. This function must support an axis argument, like numpy.mean , numpy.sum , etc.

Structured array for which to apply func.

Result of the recution operation

Assigns values from one structured array to another by field name.

Normally in numpy >= 1.14, assignment of one structured array to another copies fields “by position”, meaning that the first field from the src is copied to the first field of the dst, and so on, regardless of field name.

This function instead copies “by field name”, such that fields in the dst are assigned from the identically named field in the src. This applies recursively for nested structures. This is how structure assignment worked in numpy >= 1.6 to <= 1.13.

The source and destination arrays during assignment.

If True, fields in the dst for which there was no matching field in the src are filled with the value 0 (zero). This was the behavior of numpy <= 1.13. If False, those fields are not modified.

Return a new array with fields in drop_names dropped.

Nested fields are supported.

Changed in version 1.18.0: drop_fields returns an array with 0 fields if all fields are dropped, rather than returning None as it did previously.

Input array

String or sequence of strings corresponding to the names of the fields to drop.

Whether to return a recarray or a mrecarray ( asrecarray=True ) or a plain ndarray or masked array with flexible dtype. The default is False.

Find the duplicates in a structured array along a given key

Name of the fields along which to check the duplicates. If None, the search is performed by records

Whether masked data should be discarded or considered as duplicates.

Whether to return the indices of the duplicated values.

Flatten a structured data-type description.

Returns a dictionary with fields indexing lists of their parent fields.

This function is used to simplify access to fields nested in other fields.

Input datatype

Last processed field name (used internally during recursion).

Dictionary of parent fields (used interbally during recursion).

Returns the field names of the input datatype as a tuple. Input datatype must have fields otherwise error is raised.

Returns the field names of the input datatype as a tuple. Input datatype must have fields otherwise error is raised. Nested structure are flattened beforehand.

Join arrays r1 and r2 on key key .

The key should be either a string or a sequence of string corresponding to the fields used to join the array. An exception is raised if the key field cannot be found in the two input arrays. Neither r1 nor r2 should have any duplicates along key : the presence of duplicates will make the output quite unreliable. Note that duplicates are not looked for by the algorithm.

A string or a sequence of strings corresponding to the fields used for comparison.

Structured arrays.

If ‘inner’, returns the elements common to both r1 and r2. If ‘outer’, returns the common elements as well as the elements of r1 not in r2 and the elements of not in r2. If ‘leftouter’, returns the common elements and the elements of r1 not in r2.

String appended to the names of the fields of r1 that are present in r2 but absent of the key.

String appended to the names of the fields of r2 that are present in r1 but absent of the key.

Dictionary mapping field names to the corresponding default values.

Whether to return a MaskedArray (or MaskedRecords is asrecarray==True ) or a ndarray.

Whether to return a recarray (or MaskedRecords if usemask==True ) or just a flexible-type ndarray.

The output is sorted along the key.

A temporary array is formed by dropping the fields not in the key for the two arrays and concatenating the result. This array is then sorted, and the common entries selected. The output is constructed by filling the fields with the selected entries. Matching is not preserved if there are some duplicates…

Merge arrays field by field.

Sequence of arrays

Whether to collapse nested fields.

Without a mask, the missing value will be filled with something, depending on what its corresponding type:

-1 for integers

-1.0 for floating point numbers

'-' for characters

'-1' for strings

True for boolean values

XXX: I just obtained these values empirically

Returns a new numpy.recarray with fields in drop_names dropped.

Join arrays r1 and r2 on keys. Alternative to join_by, that always returns a np.recarray.

equivalent function

Fills fields from output with fields from input, with support for nested structures.

Input array.

Output array.

output should be at least the same size as input

Rename the fields from a flexible-datatype ndarray or recarray.

Input array whose fields must be modified.

Dictionary mapping old field names to their new version.

Re-pack the fields of a structured array or dtype in memory.

The memory layout of structured datatypes allows fields at arbitrary byte offsets. This means the fields can be separated by padding bytes, their offsets can be non-monotonically increasing, and they can overlap.

This method removes any overlaps and reorders the fields in memory so they have increasing byte offsets, and adds or removes padding bytes depending on the align option, which behaves like the align option to numpy.dtype .

If align=False , this method produces a “packed” memory layout in which each field starts at the byte the previous field ended, and any padding bytes are removed.

If align=True , this methods produces an “aligned” memory layout in which each field’s offset is a multiple of its alignment, and the total itemsize is a multiple of the largest alignment, by adding padding bytes as needed.

array or dtype for which to repack the fields.

If true, use an “aligned” memory layout, otherwise use a “packed” layout.

If True, also repack nested structures.

Copy of a with fields repacked, or a itself if no repacking was needed.

Casts a structured array to a new dtype using assignment by field-name.

This function assigns from the old to the new array by name, so the value of a field in the output array is the value of the field with the same name in the source array. This has the effect of creating a new ndarray containing only the fields “required” by the required_dtype.

If a field name in the required_dtype does not exist in the input array, that field is created and set to 0 in the output array.

array to cast

datatype for output array

array with the new dtype, with field values copied from the fields in the input array with the same name

Superposes arrays fields by fields

Sequence of input arrays.

Whether automatically cast the type of the field to the maximum.

Converts an n-D structured array into an (n+1)-D unstructured array.

The new array will have a new last dimension equal in size to the number of field-elements of the input array. If not supplied, the output datatype is determined from the numpy type promotion rules applied to all the field datatypes.

Nested fields, as well as each element of any subarray fields, all count as a single field-elements.

Structured array or dtype to convert. Cannot contain object datatype.

The dtype of the output unstructured array.

If true, always return a copy. If false, a view is returned if possible, such as when the dtype and strides of the fields are suitable and the array subtype is one of numpy.ndarray , numpy.recarray or numpy.memmap .

Changed in version 1.25.0: A view can now be returned if the fields are separated by a uniform stride.

See casting argument of numpy.ndarray.astype . Controls what kind of data casting may occur.

Unstructured array with one more dimension.

Converts an n-D unstructured array into an (n-1)-D structured array.

The last dimension of the input array is converted into a structure, with number of field-elements equal to the size of the last dimension of the input array. By default all output fields have the input array’s dtype, but an output structured dtype with an equal number of fields-elements can be supplied instead.

Nested fields, as well as each element of any subarray fields, all count towards the number of field-elements.

Unstructured array or dtype to convert.

The structured dtype of the output array

If dtype is not supplied, this specifies the field names for the output dtype, in order. The field dtypes will be the same as the input array.

Whether to create an aligned memory layout.

See copy argument to numpy.ndarray.astype . If true, always return a copy. If false, and dtype requirements are satisfied, a view is returned.

Structured array with fewer dimensions.

  • Python Home
  • Python Exercises
  • ▼Python NumPy
  • NumPy Basic
  • NumPy Array
  • NumPy Linear Algebra
  • NumPy Random
  • Sorting and Searching
  • NumPy Mathematics
  • NumPy Statistics
  • NumPy DateTime
  • NumPy String
  • NumPy Broadcasting
  • NumPy Memory Layout
  • NumPy Performance Optimization
  • NumPy Interoperability
  • NumPy I/O Operations
  • NumPy Advanced Indexing
  • NumPy Universal Functions
  • NumPy Masked Arrays
  • NumPy Structured Arrays
  • NumPy Integration with SciPy
  • Advanced NumPy
  • Numpy 100 Exercises
  • ..More to come..

NumPy Exercises, Practice, Solutions

Learn python numpy.

NumPy is a fundamental Python library for scientific computing, offering a multidimensional array object and various routines for fast array operations. It supports mathematical, logical, shape manipulation, sorting, I/O, Fourier transforms, linear algebra, statistics, random simulations, and more.

The best way to learn is through practice and exercise. Here, you can practice NumPy concepts with exercises ranging from basic to complex, each accompanied by a sample solution and explanation. It is recommended to attempt these exercises on your own before checking the solutions.

We hope these exercises enhance your NumPy coding skills. Currently, the following sections are available, and more exercises are being added. Happy coding!

List of NumPy Exercises:

  • NumPy Basic [ 59 exercises with solution ]
  • NumPy arrays [ 205 exercises with solution ]
  • NumPy Linear Algebra [ 19 exercises with solution ]
  • NumPy Random [ 17 exercises with solution ]
  • NumPy Sorting and Searching [ 9 exercises with solution ]
  • NumPy Mathematics [ 41 exercises with solution ]
  • NumPy Statistics [ 14 exercises with solution ]
  • NumPy DateTime [ 7 exercises with solution ]
  • NumPy String [ 22 exercises with solution ]
  • NumPy Broadcasting [ 20 exercises with solution ]
  • NumPy Memory Layout [ 19 exercises with solution ]
  • NumPy Performance Optimization [ 20 exercises with solution ]
  • NumPy Interoperability [ 20 exercises with solution ]
  • NumPy I/O Operations [ 20 exercises with solution ]
  • NumPy Advanced Indexing [ 20 exercises with solution ]
  • NumPy Universal Functions [ 20 exercises with solution ]
  • NumPy Masked Arrays [ 20 exercises with solution ]
  • NumPy Structured Arrays [ 20 exercises with solution ]
  • NumPy Integration with SciPy [ 19 exercises with solution ]
  • Advanced NumPy [ 33 exercises with solution ]
  • Mastering NumPy [ 100 Exercises with Solution ]
  • More to come

NumPy Basics

Operator Description
np.array([1,2,3]) 1d array
np.array([(1,2,3),(4,5,6)]) 2d array
np.arange(start,stop,step) range array

Placeholders

Operator Description
np.linspace(0,2,9) Add evenly spaced values btw interval to array of length
np.zeros((1,2)) Create and array filled with zeros
np.ones((1,2)) Creates an array filled with ones
np.random.random((5,5)) Creates random array
np.empty((2,2)) Creates an empty array
Syntax Description
array.shape Dimensions (Rows,Columns)
len(array) Length of Array
array.ndim Number of Array Dimensions
array.dtype Data Type
array.astype(type) Converts to Data Type
type(array) Type of Array

Copying/Sorting

Operators Description
np.copy(array) Creates copy of array
other = array.copy() Creates deep copy of array
array.sort() Sorts an array
array.sort(axis=0) Sorts axis of array

Array Manipulation

Adding or Removing Elements

Operator Description
np.append(a,b) Append items to array
np.insert(array, 1, 2, axis) Insert items into array at axis 0 or 1
np.resize((2,4)) Resize array to shape(2,4)
np.delete(array,1,axis) Deletes items from array

Combining Arrays

Operator Description
np.concatenate((a,b),axis=0) Concatenates 2 arrays, adds to end
np.vstack((a,b)) Stack array row-wise
np.hstack((a,b)) Stack array column wise

Splitting Arrays

Operator Description
numpy.split() Split an array into multiple sub-arrays.
np.array_split(array, 3) Split an array in sub-arrays of (nearly) identical size
numpy.hsplit(array, 3) Split the array horizontally at 3rd index
Operator Description
other = ndarray.flatten() Flattens a 2d array to 1d
array = np.transpose(other)
array.T
Transpose array
inverse = np.linalg.inv(matrix) Inverse of a given matrix

Mathematics

Operator Description
np.add(x,y)
x + y
Addition
np.substract(x,y)
x - y
Subtraction
np.divide(x,y)
x / y
Division
np.multiply(x,y)
x @ y
Multiplication
np.sqrt(x) Square Root
np.sin(x) Element-wise sine
np.cos(x) Element-wise cosine
np.log(x) Element-wise natural log
np.dot(x,y) Dot product
np.roots([1,0,-4]) Roots of a given polynomial coefficients
Operator Description
== Equal
!= Not equal
> Greater than
>= Greater than or equal
np.array_equal(x,y) Array-wise comparison

Basic Statistics

Operator Description
np.mean(array) Mean
np.median(array) Median
array.corrcoef() Correlation Coefficient
np.std(array) Standard Deviation
Operator Description
array.sum() Array-wise sum
array.min() Array-wise minimum value
array.max(axis=0) Maximum value of specified axis
array.cumsum(axis=0) Cumulative sum of specified axis

Slicing and Subsetting

Operator Description
array[i] 1d array at index i
array[i,j] 2d array at index[i][j]
array[i
array[0:3] Select items of index 0, 1 and 2
array[0:2,1] Select items of rows 0 and 1 at column 1
array[:1] Select items of row 0 (equals array[0:1, :])
array[1:2, :] Select items of row 1
[comment]: array[1,...]
array[ : :-1] Reverses array

[ Want to contribute to Python Pandas exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]

Test your Python skills with w3resource's quiz

Follow us on Facebook and Twitter for latest update.

  • Weekly Trends and Language Statistics

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

codexdebayan/git-assignment-2

Folders and files.

NameName
3 Commits
logs logs

Repository files navigation

Readme for a python project.

Numpy is a Python library and is the fundamental package for N-dimensional arrays, numerical computing tools, and interoperability with other libraries

Installation

Use the package manager pip to install numpy.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

NumPy Item assignment type error: can't assign to numpy array

Very simple question, but when I run the following code I get TypeError: 'numpy.int64' object does not support item assignment .

This is similar to TypeError: 'numpy.float64' object does not support item assignment but I believe the failure mode is different.

information_interchange's user avatar

  • 2 It's a 1D array, so what are you indexing with [0] twice? –  cs95 Commented Jun 13, 2019 at 15:05
  • 1 What is your expected output after total_array[0][0] = 1 ? –  Ricky Kim Commented Jun 13, 2019 at 15:06
  • My mistake, I have added the answer below and will accept in 2 days –  information_interchange Commented Jun 13, 2019 at 15:15

OK, my mistake, unlike Pytorch, numpy.array() ONLY creates 1D arrays. The correct behaviour would be to do something like total_array = np.zeros() or np.empty()

  • np.array can create 2d arrsys - if you give a nested list. total_array[0,0]=1 is the more idiomatic way of indexing a 2d array. –  hpaulj Commented Jun 13, 2019 at 16:03

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged python numpy or ask your own question .

  • Featured on Meta
  • We've made changes to our Terms of Service & Privacy Policy - July 2024
  • Bringing clarity to status tag usage on meta sites
  • Feedback requested: How do you use tag hover descriptions for curating and do...

Hot Network Questions

  • Density matrices and locality
  • what is wrong with my intuition for the sum of the reciprocals of primes?
  • How does static resources help prevent security vulnerabilities?
  • Is "Alice loves candies" actually necessary for "Alice loves all sweet foods"?
  • Giant War-Marbles of Doom: What's the Biggest possible Spherical Vehicle we could Build?
  • Function to find the most common numeric ordered pairings (value, count)
  • MOSFETs keep shorting way below rated current
  • How to handle stealth before combat starts?
  • Were there mistakes in converting Dijkstra's Algol-60 compiler to Pascal?
  • Who became an oligarch after the collapse of the USSR
  • Implications of the statement: "Note that it is very difficult to remove Rosetta 2 once it is installed"
  • Did the United States have consent from Texas to cede a piece of land that was part of Texas?
  • Is “overaction” an Indian English word?
  • Did I write the locks properly to prevent concurrency issues?
  • Someone said 24-hour time for 0215 can be 零二么五小時 --- Why would 么 be used instead of 幺? Havent these 2 characters been different for nearly 70 years?
  • when translating a video game's controls into German do I assume a German keyboard?
  • Does a Way of the Astral Self Monk HAVE to do force damage with Arms of the Astral Self from 10' away, or can it be bludgeoning?
  • Age is just a number!
  • Why does the definition of a braided monoidal category not mention the braid equation?
  • How to cite a book if only its chapters have DOIs?
  • Symbol between two columns in a table
  • Physical basis of "forced harmonics" on a violin
  • 'best poster' and 'best talk' prizes - can we do better?
  • What is the translation of point man in French?

assignment a numpy array

IMAGES

  1. NumPy: Array Object

    assignment a numpy array

  2. NumPy Arrays

    assignment a numpy array

  3. Mathematical Operations in Python with Numpy

    assignment a numpy array

  4. Python

    assignment a numpy array

  5. 7 Tips to Help you Learn Numpy Quickly

    assignment a numpy array

  6. Numpy Array

    assignment a numpy array

COMMENTS

  1. NumPy Indexing and Assignment

    Element Assignment in NumPy Arrays. We can assign new values to an element of a NumPy array using the = operator, just like regular python lists. A few examples are below (note that this is all one code block, which means that the element assignments are carried forward from step to step). array([0.12, 0.94, 0.66, 0.73, 0.83])

  2. python

    Use numpy.meshgrid () to make arrays of indexes that you can use to index into both your original array and the array of values for the third dimension. import numpy as np. import scipy as sp. import scipy.stats.distributions. a = np.zeros((2,3,4)) z = sp.stats.distributions.randint.rvs(0, 4, size=(2,3))

  3. Indexing on ndarrays

    Indexing routines. ndarrays can be indexed using the standard Python x[obj] syntax, where x is the array and obj the selection. There are different kinds of indexing available depending on obj : basic indexing, advanced indexing and field access. Most of the following examples show the use of indexing when referencing data in an array.

  4. Array creation

    Notice when you perform operations with two arrays of the same dtype: uint32, the resulting array is the same type.When you perform operations with different dtype, NumPy will assign a new type that satisfies all of the array elements involved in the computation, here uint32 and int32 can both be represented in as int64.. The default NumPy behavior is to create arrays in either 32 or 64-bit ...

  5. NumPy: Get and set values in an array using various indexing

    This article explains how to get and set values, such as individual elements or subarrays (e.g., rows or columns), in a NumPy array ( ndarray) using various indexing. See the following articles for information on deleting, concatenating, and adding to ndarray. The NumPy version used in this article is as follows.

  6. python

    Assign values to numpy.array. 3. numpy array assign values from another array. 1. Assign value to array (Python, Numpy) 1. Numpy syntax to assign elements of an array. Hot Network Questions New job, reporting manager is not replying to me after a few days: how to proceed?

  7. Array manipulation routines

    insert (arr, obj, values [, axis]) Insert values along the given axis before the given indices. append (arr, values [, axis]) Append values to the end of an array. resize (a, new_shape) Return a new array with the specified shape. trim_zeros (filt [, trim]) Trim the leading and/or trailing zeros from a 1-D array or sequence.

  8. Python NumPy Array Tutorial

    Python Numpy Array Tutorial. A NumPy tutorial for beginners in which you'll learn how to create a NumPy array, use broadcasting, access values, manipulate arrays, and much more. Updated Feb 2023 · 45 min read. NumPy is, just like SciPy, Scikit-Learn, pandas, and similar packages. They are the Python packages that you just can't miss when you ...

  9. Introducing Numpy Arrays

    Introducing Numpy Arrays. In the 2nd part of this book, we will study the numerical methods by using Python. We will use array/matrix a lot later in the book. Therefore, here we are going to introduce the most common way to handle arrays in Python using the Numpy module. Numpy is probably the most fundamental numerical computing module in Python.

  10. Look Ma, No for Loops: Array Programming With NumPy

    Getting into Shape: Intro to NumPy Arrays. The fundamental object of NumPy is its ndarray (or numpy.array), an n-dimensional array that is also present in some form in array-oriented languages such as Fortran 90, R, and MATLAB, as well as predecessors APL and J. Let's start things off by forming a 3-dimensional array with 36 elements:

  11. Basics of NumPy Arrays

    Basics of NumPy Arrays. NumPy stands for Numerical Python. It is a Python library used for working with an array. In Python, we use the list for the array but it's slow to process. NumPy array is a powerful N-dimensional array object and is used in linear algebra, Fourier transform, and random number capabilities.

  12. numpy.array

    numpy.array. #. numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None) #. Create an array. Parameters: objectarray_like. An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence. If object is a scalar, a 0-dimensional array containing object ...

  13. NumPy Creating Arrays

    An array that has 1-D arrays as its elements is called a 2-D array. These are often used to represent matrix or 2nd order tensors. NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

  14. Different Ways to Create Numpy Arrays in Python

    Initialize a Python NumPy Array Using Special Functions. NumPy provides several built-in functions to generate arrays with specific properties. np.zeros(): Creates an array filled with zeros. np.ones(): Creates an array filled with ones. np.full(): Creates an array filled with a specified value. np.arange(): Creates an array with values that are evenly spaced within a given range.

  15. Python NumPy

    Python NumPy is a general-purpose array processing package. It provides fast and versatile n-dimensional arrays and tools for working with these arrays. It provides various computing tools such as comprehensive mathematical functions, random number generator and it's easy to use syntax makes it highly accessible and productive for programmers from any background.

  16. How to Add a Numpy Array to a Pandas DataFrame

    The following code shows how to create a pandas DataFrame to hold some stats for basketball players and append a NumPy array as a new column titled 'blocks': import numpy as np. import pandas as pd. #create pandas DataFrame. df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23. #create NumPy matrix.

  17. python

    Numpy arrays have a copy method which you can use for just this purpose. So if you write. b = a.copy() then Python will first actually make a copy of the array - that is, it sets aside a new region of memory, let's say at address 0x123904381, then goes to memory address 0x123674283 and copies all the values of the array from the latter section ...

  18. Structured arrays

    Here x is a one-dimensional array of length two whose datatype is a structure with three fields: 1. A string of length 10 or less named 'name', 2. a 32-bit integer named 'age', and 3. a 32-bit float named 'weight'. If you index x at position 1 you get a structure:

  19. NumPy Exercises, Practice, Solutions

    NumPy is a fundamental Python library for scientific computing, offering a multidimensional array object and various routines for fast array operations. It supports mathematical, logical, shape manipulation, sorting, I/O, Fourier transforms, linear algebra, statistics, random simulations, and more. The best way to learn is through practice and ...

  20. GitHub

    Numpy is a Python library and is the fundamental package for N-dimensional arrays, numerical computing tools, and interoperability with other libraries Installation Use the package manager pip to install numpy.

  21. python

    If b is a 2x2 np.ndarray and the following assignment is performed, what does numpy do in the background, i.e. does it convert the list[100, 100] first to a numpy array or does it directly use the list[100,100] to fill in the values in the first row of b: b[1,:] = [100,100] Where in the documentation can I find more information about this?

  22. Using NumPy to Perform Date and Time Calculations

    NumPy's robust date and time functionalities take the headache out of these tasks, offering a suite of methods that simplify the process immensely. For instance, NumPy allows you to easily create arrays of dates, perform arithmetic on dates and times, and convert between different time units with just a few lines of code.

  23. NumPy Item assignment type error: can't assign to numpy array

    OK, my mistake, unlike Pytorch, numpy.array () ONLY creates 1D arrays. The correct behaviour would be to do something like total_array = np.zeros () or np.empty () np.array can create 2d arrsys - if you give a nested list. total_array[0,0]=1 is the more idiomatic way of indexing a 2d array.