实现线性回归的梯度下降法算法,解决糖尿病预测问题,输出mse和R2的值。
以下为糖尿病的实验报告
一、问题描述
实现线性回归的梯度下降法算法,解决糖尿病预测问题,输出mse和R2的值。
二、实验目的
梯度下降算法在最小化目标函数F的搜索轨迹。
三、实验内容
3.1数据导入
自动生成的数据。
#导入数据
from sklearn.datasets import load_diabetes
3.2数据预处理
def process_features(X):scaler = StandardScaler()X = scaler.fit_transform(X)m, n = X.shapeX = np.c_[np.ones((m, 1)), X] return X
3.3算法描述
import numpy as npclass LinearRegression: def fit(self, X, y, eta, epsilon):m, n = X.shapew = np.zeros((n,1)) while True:e = X.dot(w) - yg = 2 * X.T.dot(e) / m w = w - eta * gif np.linalg.norm(g, 2) < epsilon:break self.w = wdef predict(self, X):return X.dot(self.w)
3.4主要代码
import numpy as npfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom linear_regression_gd import LinearRegressionfrom sklearn.metrics import mean_squared_errorfrom sklearn.metrics import r2_score#导入数据from sklearn.datasets import load_diabetesdef process_features(X):scaler = StandardScaler()X = scaler.fit_transform(X)m, n = X.shapeX = np.c_[np.ones((m, 1)), X] return Xdiabetes = load_diabetes()X = diabetes.datay = diabetes.target.reshape(-1,1)X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)X_train = process_features(X_train)X_test = process_features(X_test)model = LinearRegression()model.fit(X_train, y_train, eta=0.01, epsilon=0.01)y_pred = model.predict(X_test)mse = mean_squared_error(y_test, y_pred)score = r2_score(y_test, y_pred)print("mse = {}, r2 = {}".format(mse, score))
四、实验结果及分析
mse = 3610.2478303557486, r2 = 0.29596349369907604
资料下载
点击这里
/download/m0_61504367/85040283