from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import GridSearchCV
import pandas as pd
# 加载数据
data = pd.read_csv('data.csv')
# 将数据分为特征和目标值
X = data.drop('error', axis=1)
y = data['error']
# 定义决策树模型
dt = DecisionTreeRegressor()
# 定义网格搜索参数
params = {
'max_depth': [3, 5, 7],
'min_samples_split': [2, 4, 6],
'min_samples_leaf': [1, 2, 3]
}
# 进行网格搜索优化
grid = GridSearchCV(dt, params, cv=5, scoring='neg_mean_squared_error')
grid.fit(X, y)
# 输出最优参数和模型得分
print('Best Parameters:', grid.best_params_)
print('Best Score:', -grid.best_score_)
# 定义新的温度数据
new_data = pd.DataFrame([[20, 25, 30, 35, 40, 45, 50, 55, 60, 65]], columns=X.columns)
# 使用模型进行预测
prediction = grid.predict(new_data)
# 输出预测结果
print('Prediction:', prediction)
代码报错下面这个问题怎么解决
```python
KeyError: "['error'] not found in axis"
data.drop(['error'], axis=1)改成这样试试
不知道你这个问题是否已经解决, 如果还没有解决的话:(待补)