(This is important, because how the function operates depends on the shape of the two inputs.) Second, let’s talk about the shape of the input arrays. Specifically, it will work with list-like objects, such as lists and tuples. Having said that, np.subtract will also operate on other types of Python objects. The np.subtract is mostly designed to operate on arrays. Since this is Numpy that we’re talking about, these inputs are commonly Numpy arrays. Data type of the inputsĪs you can see in the syntax explanation above, there are two inputs. Format of the input arraysĪlthough the syntax above is fairly simple, there are a few important things to keep in mind about the input arrays. Note that the syntax shown above assumes that you’ve imported Numpy using the alias np. The syntax for the Numpy subtract function is simple: Broadcasting may be confusing if you’re new to Numpy, so I’ll show you an example in the examples section.īefore we look at the examples though, let’s first look at the syntax. Importantly, the arrays will need to be sized appropriately, such that broadcasting is possible (e.g., they need the same number of columns). When you do this, Numpy will “broadcast” the lower dimensional array over slices of the higher dimensional array. For example, you might use np.subtract on a 2-dimensional array and a 1-dimensional array. When you operate on lists, the output will be a Numpy array.Īnd finally, you can use the Numpy subtract function with a multi-dimensional array and a lower dimensional array. When you do this, for the most part, np.subtract will treat the lists as Numpy arrays (and enforce the same rules with regard to size and shape that would apply to Numpy arrays). You can also use np.subtract on Python lists. In this case, it will simply perform subtraction. It performs this subtraction in an “element-wise” fashion.īeyond using it for two same-sized arrays, you can also use Numpy subtract in a few other ways.įirst, you can use np.subtract to operate on two scalar values. When you use np.subtract on two same-sized Numpy arrays, the function will subtract the elements of the second array from the elements of the first array. Perhaps the most important use of this function is to subtract the values of two same-sized Numpy arrays. Let’s start by quickly looking at what the function does.Īs you might expect, the Numpy subtract function performs subtraction with Numpy arrays and other Python objects. If you need something specific, you can click on any of the following links. I’ll explain the syntax of np.subtract, how the function works, and how to use it. In this tutorial, I’ll explain how to use the Numpy subtract function – AKA np.subtract – to perform mathematical subtraction with Numpy arrays and other Python objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |