1. numpy basis of scientific computing library
1.1 What is numpy
NumPy (Numerical Python) is an extension library Python language, supports a number of dimensions of the array and matrix operations, in addition, it provides a lot of math library for array operations.
Numeric NumPy's predecessor was first developed by Jim Hugunin with other collaborators, in 2005, Travis Oliphant Numeric combined with another in the nature of the characteristics of Numarray library, and joined the other expansion and development of NumPy. NumPy is open source and jointly safeguard the development of many collaborators.
NumPy is running very fast math library, mainly for computing array, comprising:
- A powerful N-dimensional array object ndarray
- Broadcast performance function
- Integration of C / C ++ / Fortran code tool
- Linear algebra, Fourier transform, random number generation functions
1.2 creates an array (matrix)
# Coding. 8 = UTF- Import numpy NP AS # used to generate an array numpy give ndarray type T1 = np.array ([l, 2,3 ,]) Print (T1) Print (type (T1)) T2 = NP. Array (Range (10 )) Print (T2) Print (type (T2)) T3 = np.arange (4,10,2 ) Print (T3) Print (type (T3)) Print (t3.dtype)
operation result
1.3 Data Types
name | description |
---|---|
bool_ | Boolean data type (True or False) |
int_ | The default integer type (similar to the C language long, int32 or Int64) |
intc | The same type C and int, int is generally 64 or int32 |
intp | For the integer type of the index (an ssize_t C-like, generally remains int32 or Int64) |
int8 | Byte (-128 to 127) |
int16 | Integer (-32768 to 32767) |
int32 | Integer (-2147483648 to 2147483647) |
int64 | Integer (-9223372036854775808 to 9223372036854775807) |
uint8 | Unsigned integer (0 to 255) |
uint16 | Unsigned integer (0 to 65535) |
uint32 | Unsigned integer (0 to 4294967295) |
uint64 | Unsigned integer (0 to 18446744073709551615) |
float_ | float64 type of shorthand |
float16 | Half-precision floating-point format, comprising: a sign bit, five-bit exponent, 10 mantissa bits |
float32 | Single-precision floating point number, comprising: a sign bit, eight exponent, 23 mantissa bits |
float64 | Double precision floating point, comprising: a sign bit, 11 exponent bits, 52 bits mantissa |
complex_ | complex128 shorthand type, i.e., a plurality of 128-bit |
complex64 | Complex, 32-bit floating-point number represents bis (real number part and imaginary number part) |
complex128 | Complex, 64-bit floating-point number represents bis (real number part and imaginary number part) |
# Coding. 8 = UTF- Import numpy AS NP Import Random # int8, Int16, Int32, Int64 four data types can use string 'i1', 'i2', 'i4', 'i8' instead of t1 = np.array ( Range (l, 4), DTYPE = " I1 " ) Print (T1) Print (t1.dtype) # #numpy in bool type t2 = np.array ([1,1,0,1,0,0], = DTYPE BOOL) Print (T2) Print (t2.dtype) # adjusted data type T3 = t2.astype ( " int8 " ) Print (T3) Print (t3.dtype) # numpy of decimal t4 = np.array ([ random.random() for i in range(10)]) print(t4) print(t4.dtype) t5 = np.round(t4,2) print(t5)
operation result:
1.4 shape array
# Coding. 8 = UTF- Import numpy NP AS A = np.array ([[3,4,5,6,7,8], [4,5,6,7,8,9 ]]) Print (A) # Check array shape Print (a.shape) # modify the array shape Print (a.reshape (3,4- )) # original shape unchanged array Print (a.shape) B = a.reshape (3,4- ) Print (B .shape) Print (B) # the array into one dimensional data Print (b.reshape (plates 1,12 )) Print (b.flatten ())
operation result:
Array sums calculated 1.5
# Coding. 8 = UTF- Import numpy NP AS A = np.array ([[3,4,5,6,7,8], [4,5,6,7,8,9 ]]) Print (A) # addition subtraction Print (A +. 5 ) Print (. 5-A ) # multiplication division Print (A *. 3 ) Print (A /. 3)
operation result:
1.6 Calculation arrays and arrays
# Coding. 8 = UTF- Import numpy NP AS A = np.array ([[3,4,5,6,7,8], [4,5,6,7,8,9 ]]) B = NP. array ([[21,22,23,24,25,26], [27,28,29,30,31,32 ]]) # subtraction arrays and arrays Print (a + B) Print (A- B ) # arrays and array multiplication and division Print (a * B) Print (a / B)
operation result:
Different dimensions of an array of computing:
# Coding. 8 = UTF- Import numpy NP AS A = np.array ([[3,4,5,6,7,8], [4,5,6,7,8,9 ]]) C = NP. array ([[1,2,3,4], [5,6,7,8], [9,10,11,12 ]]) # an array of different dimensions calculated Print (a * C)
operation result:
# Coding. 8 = UTF- Import numpy NP AS # 2 rows 6 array a = np.array ([[3,4,5,6,7,8] , [4,5,6,7,8,9 ]]) # 1 row of the array 6 is C = np.array ([1,2,3,4,5,6 ]) Print (A- C) Print (a * C)
operation result:
# Coding. 8 = UTF- Import numpy NP AS # 2 rows 6 array a = np.array ([[3,4,5,6,7,8] , [4,5,6,7,8,9 ]]) # 1 row of the array 6 is C = np.array ([[1], [2 ]]) Print (a + C) Print (a * C) Print (a * C)
operation result: