# 线性回归、Ridge、LASSO、ElasticNet回归
import numpy as np
import pandas as pd
import warnings
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.linear_model import LinearRegression, RidgeCV, LassoCV, ElasticNetCV
# 设置随机数种子
np.random.seed(0)
# 构造数据
def makedata():
N = 9 # 9个点
x = np.linspace(0, 6, N) + np.random.randn(N) # 0~6等间隔数加上高斯噪声
x = np.sort(x) # 排序
y = x ** 2 - 4 * x - 3 + np.random.randn(N)
# 设置成列向量
x = x.reshape(-1, 1)
y = y.reshape(-1, 1)
return x, y
# 计算xss
def xss(y, y_hat):
# 将数组展平
y = y.ravel()
y_hat = y_hat.ravel()
# R2
tss = np.sum(np.power(y - np.average(y, axis=0), 2))
rss = np.sum(np.power(y_hat - y, 2))
ess = np.sum(np.power(y_hat - np.average(y, axis=0), 2))
R2 = (tss - rss) / tss
# 添加到列表
tss_list.append(tss)
rss_list
1.5 案例:多项式曲线拟合的比较
最新推荐文章于 2023-04-11 16:27:33 发布