foreword
I have worked as a python crawler for nearly a year before, and I still have some basic python language. At present, machine learning is in full swing. In the project, machine learning is used to classify the collected content. I will learn the relevant libraries in my spare time, and look forward to money++
Introduction
- Numpy
python scientific computing base library - matplotlib
Matplotlib is a Python 2D plotting library that produces publication-quality graphics in a variety of hardcopy formats and a cross-platform interactive environment - pandas
python data analysis library
NumPy basic knowledge points
Array output settings
set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None)
precision
Output floating point precision setting, default is 8threshold
The threshold for triggering the output summary, which can be set to np.inf to print out all
slice source
The python sequence slice address can be written as [start:end:step], where start, end and step can be omitted
range(10) =>[0,1,2,3,4,5,6,7,8,9]
- When start is omitted, the default starts from item 0
range(10)[:10:2] => [0,2,4,6,8]
- When the end is omitted, it defaults to the end of the array
range(10)[1::2] => [1,3,5,7,9]
- When the start and end are not omitted, step defaults to 1
range(10)[2:6:] => [2,3,4,5]
- Step size step=n; represents starting from start (start is also counted) every step interval, take a number until the end end
range(20)[::3] => [0,3,6,9,12,15,18]
- When step is equal to a negative number, take the number from right to left
range(10)[::-1] => [9,8,7,6,5,4,3,2,1,0]; range(10)[::-2] => [9,7,5,3,1]
- a[start:end]: the range is [start,end)
- a[:end]: the range is [0,end)
- a[start:]: The range is from start to end (including the end)
multidimensional slice
In [1]: import numpy as np In [2]: a = np.arange(25).reshape((5,5)) In [3]: a Out[3]: array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) # 各个维度分别取,冒号表示取这个维度的所有 In [4]: a[:,2:5] Out[4]: array([[ 2, 3, 4], [ 7, 8, 9], [12, 13, 14], [17, 18, 19], [22, 23, 24]]) In [5]: a[:, None].shape Out[5]: (5, 1, 5) # None代表新增加一个维度,它有一个别称叫newaxis, None放在哪一维,就会在哪一维上出现新的维度 In [6]: a[:, None] Out[6]: array([[[ 0, 1, 2, 3, 4]], [[ 5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14]], [[15, 16, 17, 18, 19]], [[20, 21, 22, 23, 24]]]) In [7]: a[:,:, None].shape Out[7]: (5, 5, 1) In [8]: a[..., None].shape Out[8]: (5, 5, 1)
The dots (…) represent as many colons as needed to produce a complete indexing tuple. For example, if x is a rank 5 array (i.e., it has 5 axes), then
- x[1,2,…] is equivalent to x[1,2,:,:,:],
- x[…,3] to x[:,:,:,:,3]
- x[4,…,5,:] to x[4,:,:,5,:].
>>> c = np.array( [[[ 0, 1, 2], # a 3D array (two stacked 2D arrays) ... [ 10, 12, 13]], ... [[100,101,102], ... [110,112,113]]]) >>> c.shape (2, 2, 3) >>> c[1,...] # same as c[1,:,:] or c[1] array([[100, 101, 102], [110, 112, 113]]) >>> c[...,2] # same as c[:,:,2] array([[ 2, 13], [102, 113]])
test installation
import matplotlib.pyplot as plt
import numpy as np
X = np.linspace(-np.pi,np.pi,256,endpoint=True)
(C,S)=np.cos(X),np.sin(X)
#这里用到了Matplotlib和numpy模块,linspace在(−π,π)之间分成共256个小段,
#并把这256个值赋予X。C,S分别是cosine和sine值(X,C,S都是numpy数组)
plt.plot(X,C)
plt.plot(X,S)
#进行显示
plt.show()