线性回归通过一个或者多个自变量与因变量之间之间进行建模的回归分析。其中特点为一个或多个称为回归系数的模型参数的线性组合。
机器学习中的泛化,泛化即是,模型学习到的概念在它处于学习的过程中时模型没有遇见过的样本时候的表现。在机器学习领域中,当我们讨论一个机器学习模型学习和泛化的好坏时,我们通常使用术语:过拟合和欠拟合。我们知道模型训练和测试的时候有两套数据,训练集和测试集。在对训练数据进行拟合时,需要照顾到每个点,而其中有一些噪点,当某个模型过度的学习训练数据中的细节和噪音,以至于模型在新的数据上表现很差,这样的话模型容易复杂,拟合程度较高,造成过拟合。而相反如果值描绘了一部分数据那么模型复杂度过于简单,欠拟合指的是模型在训练和预测时表现都不好的情况,称为欠拟合。
代码如下(以预测波士顿房价为例):
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def mylinner():
"""
线性回归预测房子价格
"""
lb = load_boston()
# x_train, x_test是特征值 y_train, y_test是目标值
x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)
#特征化
std_x = StandardScaler()
x_train = std_x.fit_transform(x_train)
x_test = std_x.fit_transform(x_test)
# 目标值
std_y = StandardScaler()
y_train = std_y.fit_transform(y_train.reshape(-1,1))
y_test = std_y.transform(y_test.reshape(-1,1))
# estimator预测
# 正规方程求解方式预测结果
lr = LinearRegression()
lr.fit(x_train, y_train)
print(lr.coef_) #回归系数
#预测测试集的房子价格
y_predict = std_y.inverse_transform(lr.predict(x_test))
for y in y_predict:
print(y[0])
print("正规方程的均方误差:", mean_squared_error(std_y.inverse_transform(y_test), y_predict))
if __name__ == '__main__':
mylinner()
pass
代码如下(以预测波士顿房价为例):
from sklearn.datasets import load_boston
from sklearn.linear_model import SGDRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def mylinner():
"""
线性回归预测房子价格
:return:
"""
lb = load_boston()
# x_train, x_test是特征值 y_train, y_test是目标值
x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)
#特征化
std_x = StandardScaler()
x_train = std_x.fit_transform(x_train)
x_test = std_x.fit_transform(x_test)
# 目标值
std_y = StandardScaler()
y_train = std_y.fit_transform(y_train.reshape(-1,1))
y_test = std_y.transform(y_test.reshape(-1,1))
# 梯度下降去进行房价预测
sgd = SGDRegressor()
sgd.fit(x_train, y_train)
print(sgd.coef_)
#预测测试集的房子价格
y_predict = std_y.inverse_transform(sgd.predict(x_test))
for y in y_predict:
print(y)
print("梯度下降的均方误差:", mean_squared_error(std_y.inverse_transform(y_test), y_predict))
if __name__ == '__main__':
mylinner()
pass
代码如下(以预测波士顿房价为例):
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def mylinner():
"""
线性回归预测房子价格
:return:
"""
lb = load_boston()
# x_train, x_test是特征值 y_train, y_test是目标值
x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)
#特征化
std_x = StandardScaler()
x_train = std_x.fit_transform(x_train)
x_test = std_x.fit_transform(x_test)
# 目标值
std_y = StandardScaler()
y_train = std_y.fit_transform(y_train.reshape(-1,1))
y_test = std_y.transform(y_test.reshape(-1,1))
# 岭回归去进行房价预测
rd = Ridge(alpha=1.0)
rd.fit(x_train, y_train)
print(rd.coef_)
# 预测测试集的房子价格
y_predict = std_y.inverse_transform(rd.predict(x_test))
for y in y_predict:
print(y[0])
print("岭回归的均方误差:", mean_squared_error(std_y.inverse_transform(y_test), y_predict))
if __name__ == '__main__':
mylinner()
pass