作业6 线性 岭回归 多项式
This commit is contained in:
parent
3b7e6d7e4f
commit
633e4ab5ed
@ -0,0 +1,2 @@
|
||||
|
||||

|
||||
42
numerical_analysis/6/main.py
Normal file
42
numerical_analysis/6/main.py
Normal file
@ -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()
|
||||
Loading…
x
Reference in New Issue
Block a user