Читать книгу Algorithms For Dummies - John Paul Mueller, John Mueller Paul, Luca Massaron - Страница 83
Understanding scalar and vector operations
ОглавлениеThe NumPy package provides essential functionality for scientific computing in Python. To use numpy
, you import it using a command such as import numpy as np
. Now you can access numpy
using the common two-letter abbreviation np
.
Python provides access to just one data type in any particular category. For example, if you need to create a variable that represents a number without a decimal portion, you use the integer data type. Using a generic designation like this is useful because it simplifies code and gives the developer a lot less to worry about. However, in scientific calculations, you often need better control over how data appears in memory, which means having more data types, something that numpy
provides for you. For example, you might need to define a particular scalar as a short
(a value that is 16 bits long). Using numpy
, you could define it as myShort = np.short(15)
. The NumPy package provides access to an assortment of data types (https://numpy.org/doc/stable/reference/arrays.scalars.html
).
Use the numpy array()
function to create a vector. For example, myVect = np.array([1, 2, 3, 4])
creates a vector with four elements. In this case, the vector contains standard Python integers. You can also use the arrange()
function to produce vectors, such as myVect = np.arange(1, 10, 2)
, which fills myVect
with [1, 3, 5, 7, 9]. The first input tells the starting point, the second the stopping point, and the third the step between each number. A fourth argument lets you define the data type for the vector.
You can also create a vector with a specific data type. All you need to do is specify the data type like this: myVect = np.int16([1, 2, 3, 4])
to fill myVect
with a vector containing 16-bit integer values. To verify this for yourself, you can use print(type(myVect[0]))
, which outputs <class 'numpy.int16'>
.
You can perform basic math functions on vectors as a whole, which makes numpy
incredibly useful and less prone to errors that can occur when using programming constructs such as loops to perform the same task. For example, when starting with myVect = np.array([1, 2, 3, 4])
, myVect + 1
produces an output of array([2, 3, 4, 5], dtype=int16)
. Note that the output tells you specifically which data type is in use. As you might expect, myVect - 1
produces an output of array([0, 1, 2, 3], dtype=int16)
.
As a final thought on scalar and vector operations, you can also perform both logical and comparison tasks. For example, the following code performs comparison operations on two arrays:
a = np.array([1, 2, 3, 4])b = np.array([2, 2, 4, 4]) print(a == b)print(a < b)
The output in this case is:
[False True False True][ True False True False]
Starting with two vectors, a
and b
, the code checks whether the individual elements in a
equal those in b
. In this case, a[0]
doesn't equal b[0]
. However, a[1]
does equal b[1]
. The output is a vector of type bool
that contains True
or False
values based on the individual comparisons.
Logical operations rely on special functions. You check the logical output of the Boolean operators AND, OR, XOR, and NOT. Here is an example of the logical functions:
a = np.array([True, False, True, False])b = np.array([True, True, False, False]) print(np.logical_or(a, b))print(np.logical_and(a, b))print(np.logical_not(a))print(np.logical_xor(a, b))
When you run this code, you see these outputs:
[ True True True False][ True False False False][False True False True][False True True False]
You can read more about the logic functions at https://numpy.org/doc/stable/reference/routines.logic.html
.