决定系数

概念引用:http://blog.youkuaiyun.com/ytdxyhz/article/details/51730995

在对数据进行线性回归计算之后,我们能够得出相应函数的系数, 那么我们如何知道得出的这个系数对方程结果的影响有强呢?

所以我们用到了一种方法叫 coefficient of determination (决定系数) 来判断 回归方程 拟合的程度.

首先我们先定义几个概念


1. Sum Of Squares Due To Error 

对于第i个观察点, 真实数据的Yi与估算出来的Yi-head的之间的差称为第i个residual, SSE 就是所有观察点的residual的和
2. Total Sum Of Squares


3. Sum Of Squares Due To Regression

通过以上我们能得到以下关于他们三者的关系


决定系数: 判断 回归方程 的拟合程度


(coefficient of determination)决定系数也就是说: 通过回归方程得出的 dependent variable 有 number% 能被 independent variable 所解释. 判断拟合的程度


(Correlation coefficient) 相关系数 : 测试dependent variable 和 independent variable 他们之间的线性关系有多强. 也就是说, independent variable 产生变化时 dependent variable 的变化有多大.

可以反映是正相关还是负相关




Udacity:机器学习纳米工程师学位

如果不能对模型的训练和测试的表现进行量化地评估,我们就很难衡量模型的好坏。通常我们会定义一些衡量标准,这些标准可以通过对某些误差或者拟合程度的计算来得到。在这个项目中,你将通过运算决定系数R2 来量化模型的表现。模型的决定系数是回归分析中十分常用的统计信息,经常被当作衡量模型预测能力好坏的标准。

R2的数值范围从0至1,表示目标变量的预测值和实际值之间的相关程度平方的百分比。一个模型的R2 值为0说明它完全无法预测目标变量;而一个R2 值为1的模型则可以对目标变量进行完美的预测。从0至1之间的数值,则表示该模型中目标变量中有百分之多少能够用特征来解释。_模型也可能出现负值的R2,这种情况下模型所做预测还不如直接计算目标变量的平均值。

# TODO: Import 'r2_score'
from sklearn.metrics import r2_score

def performance_metric(y_true, y_predict):
    """ Calculates and returns the performance score between 
        true and predicted values based on the metric chosen. """
    
    # TODO: Calculate the performance score between 'y_true' and 'y_predict'
    score = r2_score(y_true, y_predict)
    
    # Return the score
    return score

#print performance_metric()


好的,决定系数($R^2$)是一个常用的评估指标,用于衡量模型预测值与真实值之间的拟合程度。$R^2$ 的值范围从负无穷到 1,值越接近 1 表示模型的拟合效果越好。 我们可以在训练和测试集上计算决定系数,并将其输出。以下是更新后的代码,包含决定系数的计算和输出: ```python import pandas as pd from sklearn.model_selection import train_test_split, RandomizedSearchCV, cross_val_score from sklearn.ensemble import RandomForestRegressor from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error, r2_score import numpy as np import matplotlib.pyplot as plt import matplotlib from scipy.stats import randint, uniform # 设置中文字体 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体显示中文 matplotlib.rcParams['axes.unicode_minus'] = False # 解决负号 '-' 显示为方块的问题 # 加载数据 data = pd.read_csv('2016年电工数学建模竞赛负荷预测数据集.csv') # 查看前几行数据确保正确加载 print(data.head()) # 检查是否有缺失值并处理 print(data.isnull().sum()) data.fillna(data.mean(), inplace=True) # 使用均值填充缺失值 # 创建新特征 data['日期'] = pd.to_datetime(data.index) # 假设日期是索引 data['星期几'] = data['日期'].dt.weekday data['月份'] = data['日期'].dt.month data['是否节假日'] = data['日期'].apply(lambda x: 1 if x.day_name() in ['Saturday', 'Sunday'] else 0) # 更新特征矩阵 X = data[['最高温度℃', '最低温度℃', '平均温度℃', '相对湿度(平均)', '降雨量(mm)', '星期几', '月份', '是否节假日']] y = data['日需求负荷(KWh)'] # 标准化特征 scaler = StandardScaler() X_scaled = scaler.fit_transform(X) X_scaled_df = pd.DataFrame(X_scaled, columns=X.columns) # 分割数据为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X_scaled_df, y, test_size=0.2, random_state=42) # 定义随机森林超参数空间 param_dist = { 'n_estimators': randint(100, 501), 'max_depth': [None] + list(range(10, 51, 10)), 'min_samples_split': randint(2, 11), 'min_samples_leaf': randint(1, 5), 'bootstrap': [True, False], 'max_features': ['sqrt', 'log2'] + list(np.linspace(0.1, 1.0, 10)), # 添加合理的浮点数值 'oob_score': [True, False], # 使用袋外分数进行评估 'criterion': ['mse', 'mae'], # 损失函数的选择 } rf_model = RandomForestRegressor(random_state=42) # 使用随机搜索进行超参数调优 random_search = RandomizedSearchCV( estimator=rf_model, param_distributions=param_dist, n_iter=100, cv=5, verbose=2, random_state=42, n_jobs=-1, error_score='raise' # 确保捕获所有错误 ) random_search.fit(X_train, y_train) # 输出最佳参数 print("Best Parameters:", random_search.best_params_) # 使用最佳参数训练模型 best_rf_model = random_search.best_estimator_ # 交叉验证评估 cv_scores = cross_val_score(best_rf_model, X_train, y_train, cv=5, scoring='neg_mean_squared_error') print(f'Random Forest CV RMSE: {-np.sqrt(-cv_scores.mean()):.2f}') # 在测试集上进行预测 y_pred_rf = best_rf_model.predict(X_test) # 计算均方根误差(RMSE)作为评估指标 rmse_rf = np.sqrt(mean_squared_error(y_test, y_pred_rf)) print(f'Random Forest Test RMSE: {rmse_rf:.2f}') # 计算决定系数 R^2 r2_train = r2_score(y_train, best_rf_model.predict(X_train)) r2_test = r2_score(y_test, y_pred_rf) print(f'Random Forest Train R^2: {r2_train:.4f}') print(f'Random Forest Test R^2: {r2_test:.4f}') # 特征重要性分析 importances = best_rf_model.feature_importances_ feature_importances = pd.Series(importances, index=X.columns) feature_importances.sort_values(ascending=False).plot(kind='bar', figsize=(10, 6)) plt.title('特征重要性') plt.show() # 绘制预测值和实际值的对比图 plt.figure(figsize=(10, 6)) plt.plot(y_test.values, label='实际值', color='blue', marker='o') plt.plot(y_pred_rf, label='RF预测值', color='red', marker='x') plt.title('实际值 vs 随机森林预测值') plt.xlabel('样本索引') plt.ylabel('日需求负荷 (KWh)') plt.legend() plt.grid(True) plt.show() # 使用袋外分数评估模型(如果使用 oob_score=True) if hasattr(best_rf_model, 'oob_score_'): print(f'OOB Score: {best_rf_model.oob_score_:.4f}') ``` ### 关键改进点 1. **计算决定系数 $R^2$**: - 使用 `r2_score` 函数计算训练集和测试集上的决定系数。 - 输出训练集和测试集上的决定系数,以便评估模型的拟合程度和泛化能力。 2. **输出决定系数**: - 在训练和测试集上分别计算并输出决定系数,帮助你更好地理解模型的表现。 通过这些改进,你可以获得更全面的模型评估结果,包括均方根误差(RMSE)和决定系数($R^2$)。如果有任何问题或需要进一步调整,请告诉我!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值