# NumPy

## NumPy How to Create an Empty Array (A Complete Guide)

To create an empty NumPy array: For instance, let’s create an empty array with no elements: Output: However, creating an array without elements rarely makes any sense. Instead, you should know and specify the shape of the final array in advance. For instance, let’s create an empty 2D array: Output (contains arbitrary values due to […]

## NumPy @ Operator (Matrix Multiplication in Python)

In NumPy, the @ operator means matrix multiplication. For instance, let’s multiply two NumPy arrays that represent 2 x 2 matrices: Output: If you are familiar with matrix multiplication, I’m sure this answers your questions. However, if you do not know what matrix multiplication means, or if you are interested in how the @ operator

## Numpy Fix “ValueError: setting an array element with a sequence”

This guide teaches you how to fix the common error ValueError: setting array element with a sequence in Python/NumPy. This error occurs because you have elements of different dimensions in the array. For example, if you have an array of arrays and one of the arrays has 2 elements and the other has 3, you’re

## NumPy How to Compare Two Arrays (The Right Way)

To check if two NumPy arrays A and B are equal: For example: This is the easiest approach to comparing two arrays. But this approach is not 100% reliable. Instead, you should consider using the built-in np.array_equal() function for good measure. This always produces the right result. In this guide, you learn how to compare

## numpy.delete: How to Remove Elements from a NumPy Array

To remove an element from a NumPy array: For example: Output: This is the quick answer. However, there is a lot more when it comes to removing elements from a NumPy array. In this guide, you are going to learn how to: How Does numpy.delete() Work In NumPy, there is a built-in function numpy.delete() you

## NumPy reshape(-1) Meaning

In NumPy, -1 in reshape(-1) refers to an unknown dimension that the reshape() function calculates for you. It is like saying: “I will leave this dimension for the reshape() function to determine”. A common use case is to flatten a nested array of an unknown number of elements to a 1D array. For example: But

## numpy.append(): How to Add Elements to a NumPy Array

A NumPy array does not have a built-in append method. Instead, to append elements to a NumPy array, use a separate numpy.append() function. For example: Output: Notice how numpy.append() creates a new copy of the original array. It does not directly append values to it. In this guide, you learn: How to Append to a

## NumPy How to Concatenate Two Arrays

To concatenate two arrays with NumPy: For instance: Output: This is a quick answer. To learn more ways to concatenate arrays and about their efficiency, please, stick around. 4 Ways to Concatenate 1D NumPy Arrays There are four built-in ways to concatenate arrays in NumPy. Before introducing these, it is important you understand that all

## NumPy How to Transpose a Matrix

To transpose a matrix with NumPy, call the transpose() method. For instance: Output: If you are in a hurry, I’m sure this quick answer is enough. To learn more about matrix transpose, keep on reading. What Is the Transpose of a Matrix The transpose of a matrix is another matrix where the matrix is flipped

Scroll to Top