from sklearn.datasets import load_diabetes from sklearn.model_selection import train_test_split #导入线性回归模型,岭回归,套索回归 from sklearn.linear_model import LinearRegression,Ridge,Lasso import numpy as np #线性回归 #概念:利用数理统计中回归分析,来确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法,运用十分广泛。 #线性关系:y=w1x1+w2x2+w3x3+b #使用均方误差作为损失函数,使用均方误差最小化目标函数的方法称为最小二乘法。 # 优点: # 直接 # 快速 # 可解释性好 # 缺点: # 需要严格的假设 # 需处理异常值,对异常值很敏感,对输入数据差异也很敏感 # 线性回归存在共线性,自相关,异方差等问题 def line_regress(): # 载入糖尿病人数据 diabetes=load_diabetes() #将数据集拆分成训练集和测试集 x_train,x_test,y_train,y_test=train_test_split(diabetes.get('data'),diabetes.get('target'),random_state=8) #使用线性回归模型拟合 lr=LinearRegression().fit(x_train,y_train) score_train=lr.score(x_train,y_train) score_test=lr.score(x_test,y_test) print("lr.coef_:{}".format(lr.coef_[:])) print("lr.intercept_:{}".format(lr.intercept_)) print("line_score_train:{}".format(score_train)) print("line_score_test:{}".format(score_test)) print("######################") #使用岭回归优化模型,解决过拟合 ridge=Ridge().fit(x_train,y_train) score_train=ridge.score(x_train,y_train) score_test=ridge.score(x_test,y_test) print("ridge.coef_:{}".format(ridge.coef_[:])) print("ridge.intercept_:{}".format(ridge.intercept_)) print("ridge_score_train:{}".format(score_train)) print("ridge_score_test:{}".format(score_test)) print("######################") #使用套索回归 lasso=Lasso(alpha=0.1,max_iter=100000).fit(x_train,y_train) score_train=lasso.score(x_train,y_train) score_test=lasso.score(x_test,y_test) print("lasso.coef_:{}".format(lasso.coef_[:])) print("lasso.intercept_:{}".format(lasso.intercept_)) print("lasso_score_train:{}".format(score_train)) print("lasso_score_test:{}".format(score_test)) print("套索回归使用特征数:{}".format(np.sum(lasso.coef_!=0))) print("######################") #体重预测 x_train=[[5,45],[5.11,26],[5.6,30],[5.9,34],[4.8,40],[5.8,36],[5.3,19],[5.8,28],[5.5,23],[5.6,32]] y_train=[[77],[47],[55],[59],[72],[60],[40],[60],[45],[58]] ridge=Ridge().fit(x_train,y_train) score_train = ridge.score(x_train, y_train) print("ridge_score_train:{}".format(score_train)) x_news = np.array([[5.5, 38]]) pre = ridge.predict(x_news) print(pre)
线性分类回归及python测试
最新推荐文章于 2024-08-18 00:55:56 发布