Python Linear Inversion for Polynomial Curve Fitting: Code Explanation and Analysis
The code is using linear inversion to fit a polynomial curve to the data points (year, CFR). The polynomial degree and regularization parameter are specified in the code.\n\nThe function "makeG" creates the design matrix G, which is a matrix of powers of the input variable x (year) up to the specified polynomial degree.\n\nThe function "linearInversion" performs the linear inversion to calculate the model parameters m. It first creates the design matrix G using the input variable x, then calculates the regularized inverse of the Gram matrix GtG. Finally, it calculates the model parameters by multiplying the inverse of GtG with the transpose of G and the target variable y (CFR).\n\nThe function "getCost" calculates the cost of the model, which is the Euclidean norm of the difference between the target variable y and the predicted variable yhat, normalized by the mean of y.\n\nThe code then loads the data from a CSV file, selects the 'year' and 'CFR' columns, and removes any rows with missing values. The 'year' and 'CFR' columns are extracted as numpy arrays.\n\nThe polynomial degree and regularization parameter are set, and the linear inversion function is called to obtain the model parameters. The model parameters and the cost of the model are printed.\n\nFinally, the plotting function is called to plot the data points and the fitted polynomial curve. The x-axis is labeled as 'year', the y-axis is labeled as 'CFR', and the title of the plot is set accordingly.
原文地址: https://www.cveoy.top/t/topic/pCKa 著作权归作者所有。请勿转载和采集!