Machine learning (numpy/matplotlib/scipy) study notes

Time domain to frequency domain-Fourier transform
Frequency domain to time domain-Inverse Fourier transform
CNN-Convolutional neural network

Strong Classifier: Logistic Regression/SVM
Weak Classifier: Decision Tree

Regression
Continuous-Regression
Discrete-Classification

The goal of least squares: find the least square sum of error.

Linear regression:
KNN (K neighbor algorithm)
ridge: Ridge regression
lasso:

Logestic regression
NLL: negative log likelihood
In classification or regression problems, the loss function (cost function) is usually used as its objective function

ROC:receiver operating characteristic
AUC:area under curve

/************************/
Numpy库
/************************/
import numpy as np

//The dimension of a NumPy array is called rank, the rank of a one-dimensional array is 1, the rank of a two-dimensional array is 2, and so on.
//In NumPy, each linear array is called an axis (axis), that is, dimensions (dimensions). axis=0, means to operate along the 0th axis, that is, operate on each column; axis=1, mean to operate along the 1st axis, that is, to operate on each row.

a=np.array(array object, dtype='element data type') //Form an array
dt=np.dtype(element object) //View the data type of the element object

Array object.shape //Returns the row and column value of the
array. Array object.shape=x //Change the row and column value of the array, will change the element array
a=array object.reshape(m,n) //Change the row and column value of the array, it won’t Change element array

np.fromiter(Iterable object, dtype, count) //Create an array from the iterable object, count is the amount of data read, the default is -1, that is, read all data
np.arange(start,stop,step, dtype) //Generate a list
np.linspace(start,stop,num,endpoint=True,restep=False,dtype=None) //Generate an arithmetic sequence
/*
num is the number of elements, the default is 50
endpoint is true It is the number sequence that contains the value of stop. On the contrary, it does not contain the
display spacing in the array generated when restep is true. Otherwise, it is not displayed.//This attribute is generally not used
/
np.logspace(start,stop,num,endpoint,base,dtype) //Generate a geometric sequence
/

base is the
common ratio, the default is 10 sequence start value is base start, end value is base stop
*/

a[start:stop:step] //a is an array, this statement is to get the elements of the a object whose subscript is from start to stop and the interval is step
/*
colon can also be written one, if the three attributes are not written, Is the default value
*/
y = x[[0,1,2], [0,1,0]] //Take the element at position (0,0),(1,1),(2,0) in the array
b = a[1:3, 1:3] //Get an array consisting of four elements (1,1),(1,2),(2,1),(2,2)
c = a[1: 3,[1,2]] //The effect is the same as above
d = a[…,1:] //… means all rows/columns
a=x[x> 5] //The return value is the element value greater than 5 An array formed by elements
x[[4,2,1,7]] //Fancy index takes the value based on the value of the index array as the index of a certain axis of the target array. For using a one-dimensional integer array as an index, if the target is a one-dimensional array, the result of the index is the element at the corresponding position; if the target is a two-dimensional array, then it is the row corresponding to the subscript.
x[[-4,-2,-1,-7]] //Reverse index, that is, the 4th, 2, 1, 7th element or row from the
bottom x[np.ix_([1,5,7,2] ,[0,3,1,2])] //Respectively take (1,0),(1,3),(1,1),(1,2),(5,0),(5,3 )... Element at position

c=a*b //If a.shape==b.shape, the function of this statement is to get an array of corresponding positions multiplied by
Broadcast (Broadcast) is a numerical calculation of arrays of different shapes (shape) by numpy Way
a = np.array([[ 0, 0, 0],
[10,10,10
],
[20,20,20] , [30,30,30]])
b = np.array([1, 2,3]) //Add 1, 2, 3 to the corresponding elements of each row where b is a

aT //Get the transpose of the array
np.transpose(a) //Get the transpose of the array
for x in np.nditer(a): //Complete the access to the elements in the array a
for element in a.flat: // To process each element in the array, you can use the flat attribute, which is an array element iterator
a.flat //an array element iterator
a.flatten() //returns a copy of the array and does the copy The modification will not affect the original array
a.ravel() //Expand the array into a one-dimensional array, and the modification will affect the original array
np.rollaxis(arr, axis, start) //Scroll a specific axis backward to a specific position, The default value of start is zero, which means complete scrolling.
np.swapaxes(arr, axis1, axis2) //Used to swap the two axes of the array
a.ndim //Return the dimension of the array a
np.squeeze(arr, axis) //Remove one dimension from the shape of the given array entry
np.concatenate ((a1, a2, ... ), axis) // along a specified axis connecting two or more arrays of the same shape
np.stack (arrays, axis) // for shaft connection along the new array Sequence, requires arrays to have the same shape
np.hstack(arrays, axis) //It is a variant of the numpy.stack function, which generates arrays by stacking horizontally.
np.vstack(arrays, axis) //It is a variant of the numpy.stack function, which generates arrays by stacking vertically.
np.split(ary, indices_or_sections, axis) //Split the array into sub-arrays along a specific axis
/*
indices_or_sections: If it is an integer, use the number to split equally, if it is an array, it is split along the axis Position (open left and closed right)
axis: the dimension along which to make the tangent, the default is 0, and it is divided horizontally. When it is 1, split vertically
*/
np.hsplit(ary, indices_or_sections) //Used to split the array horizontally, split the original array by specifying the number of arrays of the same shape to be returned
np.vsplit(ary, indices_or_sections) // Split along the vertical axis, the splitting method is the same as hsplit usage

np.resize(arr, shape) //returns a new array of the specified size, if the new array size is greater than the original size, it contains a copy of the elements in the original array. (The row or column will appear repeatedly)
np.append(arr, values, axis=None) //Add a value to the end of the array. The append operation allocates the entire array and copies the original array to the new array. The return value is always a one-dimensional array
np.insert(arr, obj, values, axis) //Before the given index, along the given The axis inserts the value in the input array. The function returns a new array. In addition, if no axis is provided, the input array will be expanded.
/*
obj inserts the index of the value before it
/
np.delete(arr, obj, axis) //Return to delete the new array of the specified sub-array from the input array. As in the case of the insert() function, if the axis parameter is not provided, the input array will be expanded.
/

obj: can be a slice, integer or an array of integers
/
np.unique(arr, return_index, return_inverse, return_counts) //Used to remove duplicate elements in the array.
/

return_index: If true, return the position (subscript) of the new list element in the old list, and store it in the form of a list
return_inverse: If true, return the position (subscript) of the old list element in the new list with Store
return_counts in list form : If true, return the number of occurrences of the elements in the deduplication array in the original array
*/

np.bitwise_and(a, b) //The elements in the two arrays can be bitwise and
np.bitwise_or(a, b) //The elements in the two arrays can be bitwise or
np.invert(a, dtype = np.uint8) //Perform the bit inversion operation on the integers in the array
np.binary_repr(a, width = 8) //The return value is the binary representation of a number, the length is width
np.left_shift(10,2) / / Shift 10 to the left by 2 bits
np.right_shift(40, 2) // Shift 40 to the left by 2 places

np.sin(a np.pi/180) //Sine function
np.arcsin(sin) //Inverse sine function
Similarly, there are cosine, tangent, etc.
np.add(a,b)
np.subtract(a,b)
np.multiply(a,b)
np.divide(a,b)
/

addition, subtraction, multiplication and division function, the array must have the same shape or conform to the array broadcasting rules
*/
np.reciprocal(a) //returns the element-wise reciprocal of the parameter
np .power(a, b) //The function uses the element in the first input array as the base and calculates its power to the corresponding element in the second input array.
np.mod(a, b) //Calculate the remainder after the division of the corresponding elements in the input array
np.amin(a, axis) //Used to calculate the minimum value of the elements in the array along the specified axis.
np.amax(a, axis) //Used to calculate the maximum value of the elements in the array along the specified axis.
np.ptp(a, axis) //Calculate the difference between the maximum value and the minimum value of the elements in the array (maximum-minimum)
np.median(a, axis) //Used to calculate the median of the elements in the array a ( Median)
np.mean(a, axis) //Returns the arithmetic mean of the elements in the array. If an axis is provided, it is calculated along it.
np.average(a, axis, weights=b) //Calculate the weighted average of the elements in the array according to the respective weights given in another array, by adding the products of the corresponding elements, and dividing the sum by the weight And to calculate the weighted average
np.std(a) //Calculate the standard deviation
np.var(a) //Calculate the variance

np.sort(a, axis, kind, order)
/*
axis: sort the array along its axis, if no array is expanded, sort along the last axis, axis=0 sort by column, axis=1 sort by row Sorting
kind: The default is'quicksort' (quicksort)
order: If the array contains fields, it is the field to be sorted
*/
np.argsort(a) //The function returns the index value of the array value from small to large.
np.lexsort((a,b)) //Used to sort multiple sequences. Think of it as sorting a spreadsheet, each column represents a sequence, and priority is given to the lower columns when sorting. That is, first row b, then row a, the parameter is a tuple
np.partition(a, 3) //put all elements (including repeated elements) in array a that are smaller than the third in the front, and put the larger ones in the back
np.partition(a, (1, 3)) //Those smaller than 1 are in the front, those larger than 3 are in the back, and those between 1 and 3 are in the middle
arr[np.argpartition(arr, 2)[2]] // Find the 3rd smallest of the array (index=2)
arr[np.argpartition(arr, -2)[-2]] //Find the 2nd largest of the array (index=-2)
np.argmax/min(a, axis ) //Return the index of the largest and smallest element along the given axis respectively.
np.nonzero (a) //Returns the index of the non-zero element in the input array
np.where(condition, such as array object>3) //Returns the index of the element in the input array that meets the given condition
np.extract(condition, x) //Extract elements from the array according to a certain condition and return the elements with full conditions

np.dot(a,b) //For two one-dimensional arrays, the calculation is the sum of the products of the corresponding subscript elements of the two arrays (called the inner product in mathematics); for two-dimensional arrays, the calculation is The matrix product of two arrays;
np.vdot(a,b) //Calculate the dot product of two vectors. If the first parameter is a complex number, then its conjugate complex number will be used in the calculation. If the parameter is a multidimensional array, it will be expanded.
np.inner(a,b) //Returns the vector inner product of a one-dimensional array. For higher dimensions, it returns the product of the sum on the last axis.
np.matmul(a,b) //The function returns the matrix product of two arrays.
np.linalg.det(a) //The function calculates the determinant of the input matrix.
np.linalg.solve(a) //The function gives the solution of a linear equation in matrix form.
np.linalg.inv(a) //The function calculates the multiplicative inverse matrix of the matrix.

np.save(file, arr, allow_pickle=True, fix_imports=True) //Save the array to a file with .npy extension.
/*
file: The file to be saved, the extension is .npy, if there is no extension .npy at the end of the file path, the extension will be automatically added.
arr: the array to be saved
allow_pickle: optional, boolean, allows the use of Python pickles to save the object array, pickle in Python is used to serialize and deserialize the object before saving to a disk file or reading from a disk file .
fix_imports: Optional, in order to facilitate reading the data saved by Python3 in Pyhton2.
*/
b = np.load('outfile.npy') //Read the content of npy or npz file
np.savez(file, *args, * kwds) //Save multiple arrays to the extension with npz File.
/

file: The file to be saved, the extension is .npz, if there is no extension .npz at the end of the file path, the extension will be automatically added.
args: The array to be saved. Keyword parameters can be used to give the array a name. The array passed by non-keyword parameters will automatically be named arr_0, arr_1, ….
kwds: Use the keyword name for the array to be saved.
/
np.loadtxt(FILENAME, dtype=int, delimiter='')
np.savetxt(FILENAME, a, fmt="%d", delimiter=",")
/ Is

to store the data in a simple text file format, correspondingly use the loadtxt() function to obtain the data.
Parameter delimiter can specify various delimiters, converter functions for specific columns, and the number of rows to be skipped.
fmt="%d" means to save as an integer
*/

/************************************/
numpy.matlib library//python matrix library
/* ********************************** /
np.matlib.empty(shape, dtype, order) //return A new matrix
np.matlib.zeros((number of rows, number of columns)) //The function creates a matrix filled with 0.
np.matlib.ones((number of rows, number of columns)) //The function creates a matrix filled with 1.
np.matlib.eye(n, M,k, dtype)
/

n: returns the number of rows of the matrix
M: returns the number of columns of the matrix, the default is n
k: the index of the diagonal, generally 0
dtype: data type
*/
np.matlib.identity(5, dtype) //returns an identity matrix with 5 rows and 5 columns
np.matlib.rand((number of rows, number of columns)) //The function creates a matrix of a given size, and the data is filled randomly of.

/ /
Matplotlib // Python plotting library
/
/
Import matplotlib
from matplotlib Import pyplot AS plt

zhfont1 = matplotlib.font_manager.FontProperties(fname=“SimHei.ttf”) //Set the font
plt.title(“Matplotlib demo”) //The title of the function image
plt.xlabel(“x axis caption”, fontproperties=zhfont1) / /x axis text
plt.ylabel("y axis caption", fontproperties=zhfont1) //y axis text
plt.plot(x,y,"ob") //function drawing, x, y are divided into x, The value of the y-axis, ob represents the blue circle
plt.show() //Display
/*
The meaning of the parameters in the plot:
Color: b-blue, g-green, r-red, c-cyan, m-product Red, y-yellow, k-black, w-white
line style:'-' solid line,'–' dash,'-.' dot-dash line,':' dashed line,'.' dot mark,'o 'Circle mark,'v' inverted triangle mark,'^' front triangle mark,'*' star mark,'x' X mark
*/

plt.subplot(2, 1, 1) //Create the first subplot table with height of 2 and width of 1, which is the first
plt.plot(x, y_sin) //Draw the first
plt.subplot(2 , 1, 2) //Create a second subplot table with a height of 2 and a width of 1, which is the second
plt.plot(x, y_cos) //Draw the second
plt.show() //Put two Function images are displayed in the same image

/ Draw a bar graph /
plt.bar(x, y, align ='center')
plt.bar(x2, y2, color ='g', align ='center')
plt.title('Bar graph')
plt .ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

//Convert the picture into a two-dimensional array and display
plt.imshow(img)
plt.show()

/ /
Scipy library python scientific computing base
/
/
scipy library modules:
scipy.cluster vector quantization / K- scipy.constants mean physical and mathematical constants
scipy.fftpack Fourier transform scipy ... integrate integration procedure
scipy.interpolate interpolation scipy .io data input and output
scipy.linalg linear algebra program scipy.ndimage n-dimensional image package
scipy.odr orthogonal distance regression scipy.optimize optimization
scipy.signal signal processing scipy.sparse sparse matrix
scipy.spatial spatial data structure and algorithm scipy. special Any special mathematical function
scipy.stats statistics

linalg.det(a) //Calculate the determinant of square matrix a
linalg.inv(a) //Find the inverse of matrix a
sample_freq = fftpack.fftfreq(sig.size, d=time_step) //Calculate the sampling frequency of the sig signal
sig_fft = fftpack.fft(sig) //Calculate the fast Fourier transform of sig
loc, std = stats.norm.fit(a) //Calculate the mean and standard deviation of a
res, err = integrate.quad(fun, a , b) //Integrate the fun function on the interval [a,b], the first return value is the integration result, the second return value is the error value
integrate.dblquad(func,a,b,gfun,hfun): Calculate the double integral. The parameters are the integrand (f(y,x)), the lower limit of x, the upper limit of x, the lower limit of y, and the upper limit of y
optimize.minimize(func,x0,method): calculate The minimum value of a scalar function. The parameters are scalar function, initial value, and optimization method. The optimization methods are:'Nelder-Mead','Powell','CG','BFGS','Newton-CG','L -BFGS-B' ,'TNC','COBYLA','SLSQP','dogleg','trust-ncg'
linalg.eig(a) //Calculate the feature vector of square matrix a
linalg.svd(a) // Singular value decomposition of matrix a
linalg.qr(a) // QR decomposition of matrix
linalg.solve(a,b) //Find the solution of the equations a*x=b
x=np.random.normal(loc, scale, size) //Form a normal distribution, the parameters are mean, standard deviation, size
x.mean() //mean
x.std() //standard deviation
x. var() //variance

Guess you like

Origin blog.csdn.net/zuzhiang/article/details/108788886