This digest is from the "scientific calculation using Python", the copyright belongs to the original author.
NumPy provides Python with a fast, multidimensional array processing capability, while SciPy adds a variety of tools that are needed for scientific computing on a NumPy basis, with these two libraries Python has the ability to process data and compute almost as much as Matlab. You can download python (x, y) directly as written in the book, or you can configure each module individually. Configuration method See: Numpy, SciPy, matplotlib installation and configuration under Python2.7.9
Why do I need to numpy processing data?
1. A standard-installed Python list can be used as an array, but the list holds pointers to objects (arbitrary objects). This structure is obviously a waste of memory and CPU compute time for numeric operations.
2. Python also provides an array module, which, unlike the array object, stores the values directly. However, because it supports only one-dimensional arrays, there are no various operational functions, so it is not suitable for numerical operations.
NumPy provides two basic objects to address the shortcomings of standard Python:
- Ndarray (n-dimensional Array object) n-dimensional Array (array of short) objects, storing n-dimensional arrays of a single data type
- Ufunc (Universal function Object) General function objects, functions that are processed by an array.
Second, Ndarray object
function Library Import:
1 Import NumPy as NP# give NumPy one individual called NP
2.1 Creating an array
2.1.1 Create a one-dimensional array:
1 A = Np.array ([1, 2, 3, 4])# method One, using the list 2 b = Np.array ((1, 2, 3, 4))# method Two, using only tuples
2.1.2 Create a two-dimensional array:
1 C = Np.array ([[1, 2, 3, 4], [5, 6, 7, 8], [9, Ten, one, and up]]# each line is enclosed in brackets
The 2.1.3 Shape property gets the array size:
1 >>> a.shape2 (4,) # A number represents a one-dimensional array 3 >>> C.shape 4 (3, 4) # two digits denote a two-dimensional array with a No. 0 axis length of 3 and a 1th axis length of 4
2.1.4 Modify the Shape property:
1>>> C.shape = 4,3#change Array to 4 rows and 3 columns2>>> C#Note that it is not an array transpose, but changes the size of each axis, and the position of the elements in memory does not change .3Array ([[[1, 2, 3],4[4, 5, 6],5[7, 8, 9],6[10, 11, 12]])7>>>C.shape8(4, 3)9>>> C.shape = 2, 1#The second parameter is set to-1 The system automatically calculates the second parameterTen>>>C OneArray ([[[1, 2, 3, 4, 5, 6], A[7, 8, 9, 10, 11, 12]]
2.1.5 The Reshape method, returns a new array with the size changed, and the shape of the original array remains the same:
1 >>> d = A.reshape ((2, 2 >>> D Array ([[1, 2 4 [3, 4 5 >>> a 6 Array ([1, 2, 3, 4 7 >>> a[0] = [# a and D share the same block of memory, modify A[0] will also change d[0, 0] 8 >>> D 9 Array ([[100, 2< Span style= "color: #000000;" >], 10 [3, 4]])
2.1.6 Dtype Property creates a floating-point group, a complex array
1>>> Np.array ([[1, 2, 3, 4], [5, 6, 7, 8], [9, Ten, one, a]], dtype=np.float)#The default Dtype=int32, which is the default creation of array elements, is integers, and the 32-bit long- integer2Array ([[1., 2., 3., 4.],3[5., 6., 7., 8.],4[9., 10., 11., 12.]])5>>> Np.array ([[1, 2, 3, 4], [5, 6, 7, 8], [9, Ten, one, a]], Dtype=np.complex)#To create a complex array6Array ([[1.+0.J, 2.+0.J, 3.+0.J, 4.+0.J],7[5.+0.J, 6.+0.J, 7.+0.J, 8.+0.J],8[9.+0.J, 10.+0.J, 11.+0.J, 12.+0.J]])
The array creation method described above is to create the sequence first, using the array () function to convert to an array, the following describes the use of specialized functions to create arrays:
2.1.7 Arange (start value, end value, step): Python-like range (), note that the final value is not included
1 >>> np.arange (0, 1, 0.1)# does not include the final value!!! 2 Array ([0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
2.1.8 linspace (Start value, final value, number of elements): The default includes the final value, you can use the endpoint setting whether to include the final value
1 >>> np.linspace (0, 1, 8)#0~1, 8 elements 2 Array ([0. , 0.14285714, 0.28571429, 0.42857143, 0.57142857,3 0.71428571, 0.85714286, 1. ])
2.1.9 logspace (Start value, final value, number of elements): Create geometric series, the following example produces 1 (10^0) to 100 (10^2), 10 Elements of geometric series:
1 >>> np.logspace (0, 2, ten)2 Array ([ 1. , 1.66810054, 2.7825594 , 4.64158883,3 7.74263683, 12.91549665, 21.5443469, 35.93813664 ,4 59.94842503, . ])
2.1.10 fromstring (String, dtype=?) : Creating an array from bytes
1 >>> np.fromstring (S, dtype=np.int8)# characters correspond to ASCII code 2 Array ([97, 98, 101, 102, 103], dtype=int8)
2.1.11 FromFunction (computes the function of each array element, the array size is shape)
1>>>deffunc1 (i):2 returnI%4+13>>> np.fromfunction (Func1, (10,))#The second parameter must be a sequence, in this case (10,) create a one-dimensional array of 10 elements4Array ([1., 2., 3., 4., 1., 2., 3., 4., 1., 2.])5 #Create a two-dimensional array representation of the 99 multiplication table6>>>defFunc2 (i, j):7 return(i+1) * (j+1)8>>> np.fromfunction (Func2, (9, 9))9Array ([[1], 2., 3., 4., 5., 6., 7., 8., 9.],Ten[2., 4., 6., 8., 10., 12., 14., 16., 18.], One[3., 6., 9., 12., 15., 18., 21., 24., 27.], A[4., 8., 12., 16., 20., 24., 28., 32., 36.], -[5., 10., 15., 20., 25., 30., 35., 40., 45.], -[6., 12., 18., 24., 30., 36., 42., 48., 54.], the[7., 14., 21., 28., 35., 42., 49., 56., 63.], -[8., 16., 24., 32., 40., 48., 56., 64., 72.], -[9., 18., 27., 36., 45., 54., 63., 72., 81.]]
2.2 Accessing elements
The 2.2.1 array is accessed in the same way as Python's standard method
1>>> a = Np.arange (10)2>>> A[5]#use an integer as a subscript to get an element in an array354>>> A[3:5]#get a slice of an array using range as subscript, including a[3] excluding a[5]5Array ([3, 4])6>>> A[:5]#omit start subscript, indicating starting from a[0]7Array ([0, 1, 2, 3, 4])8>>> A[:-1]#the subscript can use negative numbers, indicating the number of forwards from the array9Array ([0, 1, 2, 3, 4, 5, 6, 7, 8])Ten>>> A[2:4] = 100,101#The subscript can also be used to modify the value of an element One>>>a AArray ([0, 1, 100, 101, 4, 5, 6, 7, 8, 9]) ->>> A[1:-1:2]#the third parameter in the range represents the step, and 2 means that an element is taken from one element to the other -Array ([1, 101, 5, 7]) the>>> A[::-1]#omit the start subscript and end subscript of the range, the step is-1, the entire array is reversed -Array ([9, 8, 7, 6, 5, 4, 101, 100, 1, 0]) ->>> A[5:1:-2]#When the step is negative, the start subscript must be greater than the end subscript -Array ([5, 101])
Unlike the Python list sequence, a new array obtained from the subscript range is a view of the original array. It shares the same block of data space as the original array:
1 >>> b = a[3:7] # A new array is generated by the subscript range b,b and a share the same block data space 2 >>> b 3 Array ([101, 4, 5, 6 4 >>> b[2] = -10 # Modify the 2nd element of B to -10 5 >>> b Span style= "color: #008080;" >6 Array ([101, 4, -10, 6 7 > >> a # A" is also modified to 8 Array ([0, 1, 101, 4, -10, 6, 7, 8, 9])
In addition to using the following table-scoped access elements, NumPy provides two advanced methods for accessing elements.
2.2.2 Using integer Sequences
When using an integer sequence to access an array element, each element in the integer sequence is used as the subscript, and the integer sequence can be a list (such as line 4th) or an array (such as line 6th). Arrays obtained using integer sequences as subscripts do not share data space with the original array.
1>>> x = Np.arange (10,1,-1)2>>>x3Array ([10, 9, 8, 7, 6, 5, 4, 3, 2])4>>> x[[3, 3, 1, 8]#gets the 4 elements in x labeled 3, 3, 1, 8, which form a new array5Array ([7, 7, 9, 2])6>>> B = X[np.array ([3,3,-3,8])]#Np.array (sequence) is the conversion of a sequence to an array, which can be negative7>>> b[2] = 1008>>>b9Array ([7, 7, 100, 2])Ten>>> x#because B and X do not share data space, the value in X does not change OneArray ([10, 9, 8, 7, 6, 5, 4, 3, 2]) A>>> x[[3,5,1] =-1,-2, 3#An integer sequence subscript can also be used to modify the value of an element ->>>x -Array ([10,-3, 8,-1, 6,-2, 4, 3, 2])
2.2.3 Using Boolean arrays
When using Boolean array B as the subscript to access the elements in the array x, all elements in array x that correspond to the subscript true in array B are collected. Arrays obtained using a Boolean array as subscripts do not share data space with the original array, note that this method corresponds only to a Boolean array and cannot use a Boolean list.
1>>> x = Np.arange (5,0,-1)2>>>x3Array ([5, 4, 3, 2, 1])4>>>X[np.array ([True, False, True, False, false])]5>>>#The subscript is true, and the element labeled 0,2 in the Boolean array is true, so get the element labeled 0,2 in x6Array ([5, 3])7>>> X[[true, False, True, False, false]]#Error, this is not the result we want8>>>#if it is a Boolean list, use True as 1, false as 0, and get the elements in X as an integer sequence9Array ([4, 5, 4, 5, 5])Ten>>>X[np.array ([True, False, True, True])] One>>>#when the length of a Boolean array is insufficient, the insufficient portion is treated as false AArray ([5, 3, 2]) ->>> X[np.array ([True, False, True, true])] =-1,-2, 3#modify only the element with the subscript true ->>>#A boolean array subscript can also be used to modify an element the>>>x -Array ([-1, 4,-2,-3, 1])
Boolean arrays are not generated manually, but are generated using the Ufunc function of the boolean operation, and refer to the Ufunc Operation section for the Ufunc function.
1>>> x = Np.random.rand (10)#generates an array of random numbers with a length of 10 and an element value of 0-12>>>x3Array ([0.72223939, 0.921226, 0.7770805, 0.2055047, 0.17567449,40.95799412, 0.12015178, 0.7627083, 0.43260184, 0.91379859])5>>> x>0.56>>>#each element in the array x is compared to 0.5 for size, and a Boolean array is obtained, which means that the corresponding value in X is greater than 0.57Array ([True, True, True, False, False, True, False, True, False, true], dtype=bool)8>>> x[x>0.5]#x>0.5 is a Boolean array9>>>#Use the Boolean array returned by x>0.5 to collect the elements in X, so the result is an array of all elements greater than 0.5 in xTenArray ([0.72223939, 0.921226, 0.7770805, 0.95799412, 0.7627083 , One0.91379859])
numpy-fast processing of data--ndarray objects--creation and access of arrays