NumPy simple introductory tutorial

# NumPy simple introductory tutorial

NumPy Python is a computing speed of a very fast math library, it attached great importance to the array. It allows you to perform vector and matrix calculations in Python, and because many of the underlying function is actually written in C, so you can experience native Python, never be able to experience the speed.

NumPy is definitely one of the key success of science Python, if you want to enter the scientific data and / or machine learning in Python, you will have to learn it. In my opinion, NumPy API design very well, so we have to start using it is not difficult.

This is a series of Python The second scientific article, do not forget to look at other yo (Translator's Note: do not release all the articles, only the removal of the article).

Oh wow, you let NumPy be your partner and do things together is great, okay.

# Array base

# Create an array

NumPy around these things called arrays deployed. In fact it is called ndarrays, you do not know all right. Using these arrays provide NumPy, we can perform a variety of useful operations with lightning speed, such as vectors and matrices, linear algebra and other math! (Just kidding, in this article we will not do any heavy lifting of math)

# 1D Array
a = np.array([0, 1, 2, 3, 4])
b = np.array((0, 1, 2, 3, 4))
c = np.arange(5)
d = np.linspace(0, 2*np.pi, 5)

print(a) # >>>[0 1 2 3 4]
print(b) # >>>[0 1 2 3 4]
print(c) # >>>[0 1 2 3 4]
print(d) # >>>[ 0.          1.57079633  3.14159265  4.71238898  6.28318531]
print(a[3]) # >>>3

The above code shows the four different methods to create the array. The most basic way is passed to a function of the sequence of NumPy Array (); you can pass any sequence (array type), rather than a common list (list) data type.

Please note that when we print an array of numbers with different lengths, it will automatically fill them out. This is useful for viewing matrix. An array in the same index as a list or any other Python sequence. You can also slice them, I do not intend to slice a one-dimensional array, if you want to learn more about slices, see this article indexes and slices - NumPy Chinese documents .

Array The above example is how to use NumPy represents the vector, then we will see how to use multi-dimensional arrays and matrices represent more information.

# MD Array,
a = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28 ,29, 30],
              [31, 32, 33, 34, 35]])

print(a[2,4]) # >>>25

To create a 2D (two-dimensional) array, we pass a list (or a sequence of a sequence) to a list of array () function. If we want a 3D (three-dimensional) arrays, we will deliver a list of lists of lists, if it is a 4D (four-dimensional) array, that is, a list of lists of lists of lists, and so on.

Please note 2D (two-dimensional) array (with the help of our friends spacebar) is how arranged in rows and columns. To index 2D (two-dimensional) array, we simply reference the number of rows and columns can be.

# Some mathematics behind it

To properly understand this, we should really look at what vector and matrix yes.

A vector quantity having a magnitude and direction. They are often used to represent the speed, acceleration and momentum and other things. Vector can be written in a variety of ways, although most useful to us is that they are written in the form n-tuple, such as (1,4,6,9). This is the way we express them in NumPy.

Matrix vector similar, except that it consists of rows and columns; like a grid. It can be given by its row and column matrix to reference values. In NumPy, we have to create an array by passing a series of sequences, just as we had done before.

Illustrates a simple linear algebra

# Multidimensional array slice

Slice multi-dimensional array 1D array bit more complex than that, and when using NumPy you will often need to use.

# MD slicing
print(a[0, 1:4]) # >>>[12 13 14]
print(a[1:4, 0]) # >>>[16 21 26]
print(a[::2,::2]) # >>>[[11 13 15]
                  #     [21 23 25]
                  #     [31 33 35]]
print(a[:, 1]) # >>>[12 17 22 27 32]

As you can see, you can by slicing a multidimensional array to perform a separate sections for each dimension in a comma-separated. Thus, for the 2D arrays, slices we define the first sheet row, the second sheet defines the column sections.

Note that you can only enter a number to specify rows or columns. The above first example of a 0-th column selecting from the array.

The following chart illustrates the example given is how sections work.

numpy array slice works

# Array property

When using NumPy, you will want to know certain information array. Luckily, in this bag inside it contains a lot of convenient methods that can give the information you want.

# Array properties
a = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28 ,29, 30],
              [31, 32, 33, 34, 35]])

print(type(a)) # >>><class 'numpy.ndarray'>
print(a.dtype) # >>>int64
print(a.size) # >>>25
print(a.shape) # >>>(5, 5)
print(a.itemsize) # >>>8
print(a.ndim) # >>>2
print(a.nbytes) # >>>200

As you can see in the above code, NumPy array actually called ndarray. I do not know why the fuck it's called ndarray, if anyone knows, please leave a message! I guess it represents an n-dimensional arrays.

It has the shape of an array is the number of rows and columns, the array has five rows above and five, so that its shape is (5,5).

itemsizeProperty is occupied by the number of bytes per entry. This type of data array 64 is int, int 64 has a 64-bit, a byte has eight bits divided by 8 divided by 64, you can get the number of bytes it takes up, in the present example eight.

ndimAttribute is the dimension of the array. This has two. For example, only one vector.

nbytesAll the properties are the number of bytes consumed in the data array. You should note that this does not count the cost of the array, so the actual space occupied by the array will be slightly larger.

# Using arrays

# Basic operator

Just the ability to create and retrieve from the array elements and attributes that can not meet your needs, you sometimes they need to be math. You can use the four operators +, -, / to complete the arithmetic operation.

# Basic Operators
a = np.arange(25)
a = a.reshape((5, 5))

b = np.array([10, 62, 1, 14, 2, 56, 79, 2, 1, 45,
              4, 92, 5, 55, 63, 43, 35, 6, 53, 24,
              56, 3, 56, 44, 78])
b = b.reshape((5,5))

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** 2)
print(a < b) print(a > b)

print(a.dot(b))

In addition DOT () outside, these operators are the array is element-wise operation. For example (a, b, c) + (d, e, f) is the result of (a + d, b + e, c + f). It separately each element pair, and calculating them. The results it returns an array. Note that when using logical operators such as "<" and ">" when the return will be a Boolean array, this is a good use, we will mention behind.

DOT () function calculates the dot product of two arrays. It returns a scalar (a direction not only magnitude values) instead of an array.

# Some mathematics behind it

DOT () function is called the dot product. The best way to understand this is to look at the view at FIG indicating how it is calculated.

Dot product operation principle

# Array special operators

NumPy also provides some other useful operators for processing arrays.

# dot, sum, min, max, cumsum
a = np.arange(10)

print(a.sum()) # >>>45
print(a.min()) # >>>0
print(a.max()) # >>>9
print(a.cumsum()) # >>>[ 0  1  3  6 10 15 21 28 36 45]

sum (), acting min () and max () function is obvious. The sum of all the elements to find the minimum and maximum elements.

However, cumsum () function is not so obvious. It like sum () adding each such element, but it is above the first element and a second element added, and the calculation result is stored in a list, then the result is added to the third element , and then stores the result in a list. This will all elements in the array to do this, and returns the total number of runs as an array and a list of.

# Index Advanced

# Fancy index

花式索引 Is an effective way certain elements in the array we want to get.

# Fancy indexing
a = np.arange(0, 100, 10)
indices = [1, 5, -1]
b = a[indices]
print(a) # >>>[ 0 10 20 30 40 50 60 70 80 90]
print(b) # >>>[10 50 90]

As you can see in the example above, we use a particular index sequence we want to retrieve the index into the array. This in turn returns a list of our index elements.

# Boolean shield

Boolean shield is a useful feature that allows elements in the array we retrieve our specified criteria.

# Boolean masking
import matplotlib.pyplot as plt

a = np.linspace(0, 2 * np.pi, 50)
b = np.sin(a)
plt.plot(a,b)
mask = b >= 0
plt.plot(a[mask], b[mask], 'bo')
mask = (b >= 0) & (a <= np.pi / 2)
plt.plot(a[mask], b[mask], 'go')
plt.show()

The above example shows how Boolean masking. You have to do is pass an array to an array of conditions involved, it will provide an array of value for you, return true for the condition.

This example produces the following figures:

Results Figure Boolean mask

We use these criteria to select different points on the map. Blue dots (in the figure further comprises a green dot, the green dot mask the blue dots), all show a value greater than 0 point. Green dot represents a value greater than 0 and less than half of all points of π.

# Default index

Incomplete index is a convenient method to get indexed or sliced ​​from the first dimension of a multidimensional array. For example, if the array a = [1,2,3,4,5], [6,7,8,9,10], then [3] will be given of index element 3 in the first dimension of the array , where is the value 4.

# Incomplete Indexing
a = np.arange(0, 100, 10)
b = a[:5]
c = a[a >= 50]
print(b) # >>>[ 0 10 20 30 40]
print(c) # >>>[50 60 70 80 90]

# The Where function

where () function is another effective method that returns the value array according to the conditions. Just need to transfer it to the conditions, it will return a list of conditions for making true elements.

# Where
a = np.arange(0, 100, 10)
b = np.where(a < 50) 
c = np.where(a >= 50)[0]
print(b) # >>>(array([0, 1, 2, 3, 4]),)
print(c) # >>>[5 6 7 8 9]

This is the NumPy, not so hard, right? Of course, this article only covers the basics of getting started in NumPy you can also do many other fun things when you're already familiar with the basics of NumPy, you can begin to explore freedom of NumPy of the world.

# Finally, do not forget to share this article oh

Remember, do not forget to share this article so that others can see! In addition, remember to subscribe to this blog's mailing list, Follow me on Twitter and Google+, so you do not miss any valuable article of!

I read all the comments, if you have anything to say, want to share, or want to ask like, please leave a message below!

# Article Source

NumPy Chinese translation of the document, the original author is Ravikiran Srinivasulu, translated to: https://www.pluralsight.com/guides/different-ways-create-numpy-arrays

Guess you like

Origin www.cnblogs.com/sogeisetsu/p/11369032.html