diff --git a/numerical_analysis/6/README.md b/numerical_analysis/6/README.md index e69de29..5694594 100644 --- a/numerical_analysis/6/README.md +++ b/numerical_analysis/6/README.md @@ -0,0 +1,2 @@ + +![image-20210117045857685](https://public.veypi.com/img/screenshot/20210117045857.png) diff --git a/numerical_analysis/6/main.py b/numerical_analysis/6/main.py new file mode 100644 index 0000000..67b0b3f --- /dev/null +++ b/numerical_analysis/6/main.py @@ -0,0 +1,42 @@ +import math +import matplotlib.pyplot as plt +import numpy as np +import random +from sklearn.linear_model import LinearRegression, Ridge, RidgeCV +from sklearn.preprocessing import PolynomialFeatures + + +def f1(x, e): + return math.exp(-x) * math.sin(x) \ + + random.normalvariate(0, e ** 2) + + +def solve1(x, y): + return LinearRegression().fit(x, y) + + +def solve2(x, y): + poly = PolynomialFeatures(degree=4) + X_poly = poly.fit_transform(x) + poly.fit(X_poly, y) + return poly + + +def solve3(x, y): + model = RidgeCV(alphas=[0.1, 1.0, 10.0]) # 通过RidgeCV可以设置多个参数值,算法使用交叉验证获取最佳参数值 + model.fit(x, y) + return model + + +if __name__ == '__main__': + x = np.linspace(0, 10, 100) + data = [f1(i, 1) for i in x] + y1 = [] + model = solve3(x.reshape((-1, 1)), data) + for i in x: + y1.append(model.predict([[i]])[0]) + print(model.predict([[1]])) + plt.plot(x, data, label='0') + plt.plot(x, y1, label='1') + plt.legend() + plt.show()