"Machine learning"--python machine learning Kuzhi numpy

Source: Internet
Author: User
Tags mathematical functions rand

First, the foregoing

NumPy(numerical python abbreviation) is an open source Python Scientific Computing Library. Use NumPy , you can use arrays and matrices in a very natural way . Numpy contains many useful mathematical functions, including linear algebra operations, Fourier transforms, and random number generation functions .

The Library's predecessor was a library for array operations that began in 1995 years. After a long period of development, it has basically become the most basic Python Science Computing package, and of course includes all The deep learning frameworks that provide Python interfaces.

Second, the specific application

1. Background -- Why use Numpy?

A) convenient:

for the same numeric calculation task, use the NumPy is much more convenient than writing Python code directly. This is because NumPy can operate directly on arrays and matrices, omitting many loop statements, and many of its mathematical functions make it much easier to write code.

b) performance:

the storage efficiency and input and output performance of arrays in NumPy are much better than the equivalent basic data structures in Python (such as nested list containers). The performance that can be improved is proportional to the number of elements in the array. For large array operations, using NumPy does have an advantage. for terabytes of large files,NumPy uses memory-mapped files to process for optimal data read and write performance.

c) Efficient:

Most of NumPy's code is written in C , which makes NumPy Much more efficient than pure Python code.

of course, NumPy also has its shortcomings, because NumPy uses memory-mapped files to achieve optimal data read and write performance, and the size of the memory limits its processing of terabytes of large files, in addition, the NumPy array is less versatile than the Python -supplied list container. Therefore, in the field outside the scientific calculation,the advantage of NumPy is not so obvious.

2, the installation of NumPy

(1) official website installation. http://www.numpy.org/.

(2) Pip installation . pip install NumPy.

(3) LFD installation, for Windows user http://www.lfd.uci.edu/~gohlke/pythonlibs/.

(4) Anaconda installation (recommended),Anaconda inside integrates a lot of Python Scientific Computing third-party library, mainly for easy installation. :https://www.anaconda.com/download/.

3,numpy Foundation:

NumPy The main object is a multidimensional array of the same elements. This is an element of all that is a type.

in the NumPy Medium Dimension (Dimensions) called Axis (axes) , the number of axes is called rank (rank) .

NumPy the array class is called Ndarray . is often referred to as an array.

the commonly used Ndarray object properties are:

Ndarray.ndim ( number of array axes , number of axes is called rank ),

Ndarray.shape ( the dimension of the array.) This is an integer tuple that indicates the size of the array on each dimension. For example, an N- row m -column matrix, whose shape attribute will be (2,3), the tuple's length is obviously the rank, that is, the dimension or Ndim Property ),

ndarray.size ( The total number of array elements equal to the product of the tuple elements in the shape attribute )

Ndarray.dtype ( An object that describes the type of element in the array, which can be used to create or specify dtype using the standard Python type. In addition , NumPy provides its own data type ).

4. Numpy data type:

Third, specific cases

Code one: Basic type identification

Import= Np.dtype (np.int_)     #  np.int64, Np.float32 ... Print

int8, Int16, Int32,int64 can be replaced by the string ' I1 ', ' i2 ', ' I4 ', ' i8 ', and so on.

Import= Np.dtype ('i8')   #  ' F8 ', ' I4 ' ' C16 ', ' A30 ' ( 30-character string), ' >i4 ' ... Print (a)

you can indicate the byte order of the data type in memory, ' > ' means store by the big-endian, ' < ' means store by small end,' = ' means the data is stored by hardware default. Big-endian or small-end storage only affects the order in which bytes are stored when the data is stored in the underlying memory, which is generally not required when we actually use Python for scientific calculations.

Code two: Create an array and view its properties

Import= Np.array ([[[6], [4, 5], dtype=int)print(a.shape)       #   A.ndim, a.size, A.dtype
Import= Np.array ([(-), (4, 5, 6)], dtype=float)print(a.shape)      #    A.ndim, a.size, A.dtype

with NP . Arange (). Reshape () To create an array:

Import#  Create a two-dimensional array of 2 rows and 5 columns,#  You can also create a three-dimensional array,B = np.arange. Reshape (2,3,2 )print(a)print(b)

A = Np.array ([[[6], [[+], [4, 5], [+], [7, 8, 9 = Np.array ([[[4], 5, 6], [[7, 8, 9]])pri NT(a.shape)print(b.shape)

Code Three: Basic operations

Import= np.random.random (6= Np.random.rand (6= NP.RANDOM.RANDN (6)  Print(A-B)                    #  print (a+b), print (a*c) ...
Print (Np.dot (a, b))          #复习矩阵乘法

# Numpy random number module np.random.random, NP.RANDOM.RANDN, Np.random.rand comparison

(1)Rand generates a uniformly distributed pseudo-random number. Spread between (0~1)

(2)RANDN generates a pseudo-random number with a standard normal distribution (the mean is 0 and the variance is 1).

Import= Np.ones ((2,3= Np.zeros ((2,3)) a*=3b+ =aprint  (a)print(b)

Code four: Common functions

Code Five: indexes, slices, and iterations

Import= Np.arange (**3) print(a)print(a[2:5]) a[:6:2] = 1000 Print (a) Print (a[::-1])

[x:y:z] The slice index, X is the left end, Y is the right end, Z is the stride length, the [x, Y] interval is taken from left to right, and the default z is 1 to omit the z parameter.
The negative sign of the step is reversed, and the value is taken from right to left.

Two-dimensional arrays:

b = Np.arange (). Reshape (5,4)print(b)print (b[2,3])print(b [0:5, 1]) Print (b[:, 1]) Print (B[1:3,:])

#当少于轴数的索引被提供时, the missing index is considered an entire slice

B[-1] #相当于b [-1,:] Last row

# B[i] The expressions in brackets are treated as I and a series: to represent the remaining axes. NumPy also allows you to use "dots" like b[i,...].

#点(...) Represents many of the necessary semicolons that produce a complete set of indexes. If X is

#秩为5的数组 (i.e. it has 5 axes), then:

x[1,2,...] is equivalent to x[1,2,:,:,:],

x[...,3] is equivalent to x[:,:,:,:,3],

X[4,..., 5,:] Equal to x[4,:,:,5,:].

Three-dimensional arrays:

c = Np.arange (a). Reshape (2,3,2)print(c) c[1]c[2,1]    #  Equivalent to c[2][1]c[2,1,1]  #  equivalent to c[2][1][1]

Indexed by Boolean array

f = Np.arange (a). Reshape (3, 4)print= f>4print(g)print(f [ G])

Iterating through values

H = Np.arange (a). Reshape (3,4)print(h) for in H:     Print(i) for in H.flat:       print(i)

iterating over a multidimensional array is the first axis :

if to perform operations on the elements in each array, we can use the flat property, which is an iterator to the array element :

Np.flatten () returns an array that is collapsed into one dimension. However, the function can only be applied to the NumPy object, that is , an array or mat, the normal List of lists is not possible.

A = Np.array ([[Up], [3, 4], [5, 6]])print(A.flatten ())

Code six: Shape manipulation

R avel (), Vstack (), Hstack (), Column_stack,row_stack, Stack, split, Hsplit, Vsplit

"Machine learning"--python machine learning Kuzhi numpy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.