One of the most important features of NumPy is the n-dimensional array object. is the matrix we usually say. by NumPy, the matrix can be quickly calculated. First look at the creation method. Converts a nested list to a matrix array of 2 rows and 4 columns by using the array method. The dimension of the matrix can be seen through shape
In [1]: data=[[1,2,3,4],[5,6,7,8]]
In [2]: Import NumPy as NP
In [3]: Arr=np.array (data)
In [4]: arr
OUT[4]:
Array ([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [6]: Arr.shape
OUT[6]: (2, 4)
In [7]: Arr.dtype
OUT[7]: Dtype (' Int64 ')
In addition to array, there are several functions that can create arrays. For example, zeros and ones can create a full 0 or all 1 array of the specified length, respectively.
In [8]: Np.zeros (10)
OUT[8]: Array ([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [9]: Np.zeros ((3,6))
OUT[9]:
Array ([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]]
In [ten]: Np.empty ((2,3))
OUT[10]:
Array ([[4.66186750e-310, 4.66186748e-310, 4.66186587e-310],
[6.91331345e-310, 0.00000000e+000, 0.00000000e+000]])
In [All]: Np.ones ((2,3))
OUT[11]:
Array ([[1., 1., 1.],
[1., 1., 1.]])
As you can see from above, the number of empy produced is some uninitialized value. In fact, there is no practical meaning to use. You can also create a NXN unit matrix through the eye, that is, the diagonal is 1, and the other position is 0 matrix.
In []: Np.eye (3)
OUT[12]:
Array ([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
The result of the previous use of Dtype is int64, which is the integer type of 64 bits. If we want a floating-point number or a 32-bit integer, what to do, the same is to specify the type by Dtype.
In []: Arr=np.array (Data,dtype=np.int32)
In []: Arr.dtype
OUT[14]: Dtype (' int32 ')
In []: Arr=np.array (Data,dtype=np.float32)
in [+]: arr
OUT[16]:
Array ([[1., 2., 3., 4.],
[5., 6., 7., 8.], Dtype=float32)
The type of dtype is similar to what we normally write code, such as int8 and Uint8, which represent signed 8-digit and unsigned 8-digit numbers, respectively. Where the representation of a complex number: complex64,complex128,complex256 to represent 2 32-bit, 64-bit, 128-bit floating-point number of complex numbers
There is also object and string_ that represent Python object types and fixed-length strings, respectively.
If you want to convert a type to an array that has already been generated, you can convert it by using the Astype method.
In []: Arr.astype (Np.int32)
OUT[17]:
Array ([[1, 2, 3, 4],
[5, 6, 7, 8]], Dtype=int32)
After describing the definition of the array, let's look at the operation between the array and the scalar. The operation here is not a multiplication between matrices. Instead, the array at the same location is multiplied
In []: Arr*arr
OUT[18]:
Array ([[1., 4., 9., 16.],
[[A]., Dtype=float32)]
in [+]: Arr+arr
OUT[19]:
Array ([[2., 4., 6., 8.],
[[Ten], [+], []], Dtype=float32)
In []: 1/arr
OUT[20]:
Array ([[1]. , 0.5, 0.33333334, 0.25],
[0.2, 0.16666667, 0.14285715, 0.125]], Dtype=float32)
in [+]: Arr-arr
OUT[21]:
Array ([[0., 0., 0., 0.],
[0., 0., 0., 0.], Dtype=float32)
Slice:
The use of slices of ndarry and Python lists is similar
In []: arr1
OUT[44]:
Array ([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In []: Arr1[1:3]
OUT[45]:
Array ([[4, 5, 6],
[7, 8, 9]])
In []: Arr1[1:2]
OUT[46]: Array ([[[4, 5, 6]])
In [a]: arr1[1]
OUT[47]: Array ([4, 5, 6])
In []: Arr[1:4]
OUT[48]: Array ([[5., 6., 7., 8.], Dtype=float32)
in [+]: Arr1[:2]
OUT[49]:
Array ([[1, 2, 3],
[4, 5, 6]])
The following two methods represent slicing separately from rows and columns. The first of the following represents an element of 1 rows and 2 columns
in [[]: Arr1[1:2,2:3]
OUT[50]: Array ([[[6]])
This represents the elements of 1, 2 rows and 1, 2 columns
In [Wuyi]: Arr1[1:3,1:3]
OUT[51]:
Array ([[5, 6],
[8, 9]])
Array Transpose and Axis swapping
Transpose is a common operation in matrix functions. Pass. The operation of T can transpose the matrix
In [4]: arr
OUT[4]:
Array ([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [5]: arr. T
OUT[5]:
Array ([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
Since it is possible to transpose, it is possible to perform an internal product operation, which can be calculated by Np.dot
In [6]: Np.dot (arr. T,arr)
OUT[6]:
Array ([[26, 32, 38, 44],
[32, 40, 48, 56],
[38, 48, 58, 68],
[44, 56, 68, 80]])
In addition, if the Np.array () Declaration of the Matrix is a one-dimensional, want to transform into multidimensional words will need to use the RESHARP function
In [all]: arr
OUT[11]:
Array ([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
When using Resharp, it is important to note that the number of columns and numbers can correspond, such as 15 numbers corresponding to 3 rows of 4 columns or 4 rows and 3 columns. If not satisfied, it will be reported as the following error
In [9]: Arr=np.arange (reshape (3,4))
---------------------------------------------------------------------------
ValueError Traceback (most recent)
<ipython-input-9-12cfece7f834> in <module> ()
----> 1 Arr=np.arange (Reshape) ((3,4))
Valueerror:cannot reshape array of size into shape (3,4)
General functions:
A number of mathematical operations functions are also provided by default in NumPy. You can perform various operations on the matrix data.
If Open square:
In []: Np.sqrt (arr)
OUT[13]:
Array ([[0]. , 1. , 1.41421356, 1.73205081, 2. ],
[2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ],
[3.16227766, 3.31662479, 3.46410162, 3.60555128, 3.74165739]])
Exponential operation:
In []: Np.exp (arr)
OUT[14]:
Array ([[1.00000000e+00, 2.71828183e+00, 7.38905610e+00,
2.00855369E+01, 5.45981500e+01],
[1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
2.98095799E+03, 8.10308393e+03],
[2.20264658e+04, 5.98741417e+04, 1.62754791e+05,
4.42413392E+05, 1.20260428e+06]])
Logarithmic operations:
In []: Np.log (arr)
/usr/local/bin/ipython:1: Runtimewarning:divide by zero encountered in log
#!/usr/bin/python
OUT[15]:
Array ([[-inf, 0. , 0.69314718, 1.09861229, 1.38629436],
[1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458],
[2.30258509, 2.39789527, 2.48490665, 2.56494936, 2.63905733]])
These are unary functions that accept a parameter, which is the data itself. Unary functions have the following:
There is also a two-dollar function, which is the input of 2 parameters, one is the data itself, and the other is the parameters of the operation of the data. For example, in the following example, each element of the matrix is processed by 3
In []: np.multiply (arr,3)
OUT[17]:
Array ([[0, 3, 6, 9, 12],
[15, 18, 21, 24, 27],
[30, 33, 36, 39, 42]])
The binary functions have these in the table below
Describe conditional logic as an array operation
If we have test1 and test2 Two arrays, and have cond as a condition of judgment. when the value in cond is True , select the test1 data when false the time to choose test2 the data.
In []: Test1=np.array ([1,2,3,4,5])
in [+]: Test2=np.array ([6,7,8,9,10])
in [+]: Cond=np.array ([True,false,true,true,false])
We naturally think of using the ZIP function in python to do
in [+]: result=[(x if c else y) for x,y,c in Zip (Test1,test2,cond)]
in []: Result
OUT[29]: [1, 7, 3, 4, 10]
But in the face of large amounts of data, such a low efficiency of operation,NumPy with the corresponding function is where to achieve the corresponding function. The syntax is much simpler. cond as the first parameter, if the data of the second parameter is true, the data of the third parameter is taken
in [+]: Result=np.where (COND,TEST1,TEST2)
in [+]: Result
OUT[31]: Array ([1, 7, 3, 4, 10])
the first parameter of the where can also be a matrix. In this example, if The parameter value in ARR is greater than 3 , it is 0, otherwise -1
in [+]: arr
OUT[32]:
Array ([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
in [[]: Result=np.where (arr>3,0,-1)
in [+]: Result
OUT[34]:
Array ([[-1,-1,-1,-1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
Mathematical and statistical methods:
NumPy also provides a number of statistical functions to perform statistical operations on data, such as averaging, variance, and so on. Refer to the following table for details
in [+]: arr
OUT[35]:
Array ([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [approx]: Np.mean (arr)
OUT[36]: 7.0
In [PNS]: np.std (arr)
OUT[37]: 4.3204937989385739
In []: Np.var (arr)
OUT[38]: 18.666666666666668
With these methods, we can perform operations on different rows or columns. For example, the following meta-calculation calculates all the data averages for the first column.
In []: Np.mean (Arr[0:3,0:1])
OUT[43]: 5.0
Save to File:
Save allows arr to be saved to the test file, with the suffix named npy. The contents of the file are then loaded through load. The path saved here is the current working path
In []: Np.save (' Test ', arr)
In []: Ret=np.load (' Test.npy ')
in [+]: RET
OUT[47]:
Array ([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
You can also Save as a compressed file through Savez, where you can save multiple data by setting parameters.
in [+]: Np.savez (' Test ', A=ARR,B=ARR1)
When loading, the different data is obtained by means of a dictionary.
In [Wuyi]: Ret=np.load (' Test.npz ')
In []: ret[' a ']
OUT[52]:
Array ([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
in [+]: ret[' B ']
OUT[53]:
Array ([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
The numpy of Python data analysis