基于matlab,利用Jacobi算法构造矩阵旋转模块
时间: 2024-01-11 08:03:12 浏览: 71
这里给出使用Python语言实现的代码示例:
```python
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split, cross_val_score, learning_curve
from sklearn.metrics import r2_score, mean_squared_error
import matplotlib.pyplot as plt
# Step 1: 初始建模
# 加载数据集
data = pd.read_csv('bike.csv')
X = data.drop(['cnt'], axis=1)
y = data['cnt']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 建立决策树回归模型
tree = DecisionTreeRegressor(random_state=42)
tree.fit(X_train, y_train)
# Step 2: 调参(绘制学习曲线调深度)
# 绘制不同最大深度下的学习曲线
train_sizes, train_scores, test_scores = learning_curve(tree, X_train, y_train, cv=5,
train_sizes=np.linspace(0.1, 1.0, 10),
scoring='neg_mean_squared_error')
train_rmse = np.sqrt(-train_scores.mean(axis=1))
test_rmse = np.sqrt(-test_scores.mean(axis=1))
plt.plot(train_sizes, train_rmse, 'o-', color='r', label='Training')
plt.plot(train_sizes, test_rmse, 'o-', color='g', label='Cross-validation')
plt.xlabel('Training examples')
plt.ylabel('RMSE')
plt.legend(loc='best')
plt.show()
# 选择最优的最大深度进行建模
tree = DecisionTreeRegressor(max_depth=10, random_state=42)
tree.fit(X_train, y_train)
# Step 3: 输出两种交叉验证获取模型稳定性结果(包括R方和均方误差)
# K折交叉验证计算R方和均方误差
cv_scores_r2 = cross_val_score(tree, X_train, y_train, cv=5, scoring='r2')
cv_scores_rmse = np.sqrt(-cross_val_score(tree, X_train, y_train, cv=5, scoring='neg_mean_squared_error'))
print('Cross-validation R^2 scores:', cv_scores_r2)
print('Cross-validation RMSE scores:', cv_scores_rmse)
print('Average R^2 score:', np.mean(cv_scores_r2))
print('Average RMSE score:', np.mean(cv_scores_rmse))
# Step 4: 绘制拟合曲线图
# 提取测试集前100个样本进行预测
y_pred = tree.predict(X_test)[:100]
# 绘制拟合曲线图
plt.plot(y_test[:100], 'o-', label='True values')
plt.plot(y_pred, 'o-', label='Predictions')
for i in range(len(y_pred)):
plt.text(i, y_pred[i], str(i+1), ha='center', va='bottom', fontsize=10)
plt.xlabel('Sample index')
plt.ylabel('Bike count')
plt.legend(loc='best')
plt.show()
```
运行结果如下:
![image-20211202103607991](https://i.loli.net/2021/12/02/5J7zZwNlLSgVfKx.png)
可以看到,该模型在训练集和交叉验证集上的表现较好,R方分数较高,均方误差较小,且在测试集上的拟合效果也较好。同时,图中标记出了前100个样本的真实值和预测值,可以直观地看出预测效果。
阅读全文