import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn import preprocessing
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV
# 读取含有热误差数据的CSV文件
train_file = open('data8.csv', encoding='utf-8')
train_df = pd.read_csv(train_file)
# 读取测试集的含有热误差数据的CSV文件
test_file = open('data.csv', encoding='utf-8')
test_df = pd.read_csv(test_file)
# 对训练集进行数据预处理
X_train = train_df.iloc[:, :-1]
Y_train = train_df.iloc[:, -1]
X_train_scaled = preprocessing.scale(X_train)
# 对测试集进行数据预处理
X_test = test_df.iloc[:, :-1]
Y_test = test_df.iloc[:, -1]
X_test_scaled = preprocessing.scale(X_test, with_mean=X_train_scaled.mean(axis=0)[0], with_std=X_train_scaled.mean(axis=0)[0])
X = pd.concat([test_df.iloc[:, :-1], train_df.iloc[:, :-1]], axis=1)
Y = pd.concat([test_df.iloc[:, -1], train_df.iloc[:, -1]], axis=1)
X_test.columns = X_train.columns
# 创建决策树模型
dt = DecisionTreeRegressor()
# 定义网格搜索参数
param_grid = {
'max_depth': [1,2,3,4,5,6,7,8,9],
'min_samples_split': [2, 4, 6],
'min_samples_leaf': [1, 2, 3]
}
# 进行网格搜索优化
grid = GridSearchCV(dt, param_grid, cv=5)
grid.fit(X, Y)
grid_search = GridSearchCV(DecisionTreeRegressor(), param_grid, cv=5)
grid_search.fit(X_train, Y_train)
best_model = grid_search.best_estimator_
# 输出最优参数和模型得分
print('Best Parameters:', grid.best_params_)
# 定义新的温度数据
# 输出预测结果
Y_pred = best_model.predict(X_test)
mse = mean_squared_error(Y_test, Y_pred)
print(f"MSE: {mse:.4f}")
print(Y_pred)
X_test = pd.concat([X_train, X_test], axis=0, ignore_index=True)
代码的预测误差有点大,能不能降一点
如果前两步的讲解读者有没有看懂的地方,可以结合完整代码推导:
#include<Eigen/Core>
#include<vector>
#include <costmap_2d/costmap_2d.h>
#include <costmap_2d/costmap_2d_ros.h>
#ifndef NBD_GLOBAL_PLANNER_ASTAR_SEARCHER_H
#define NBD_GLOBAL_PLANNER_ASTAR_SEARCHER_H
using namespace std;
struct SearchNode{
Eigen::Vector3d Pos;
int nFrom;
double dCost;
double dHCost;
int nSearchNodeId;
int nBTF;
int nBTL;
int nBTR;
};
#define RESOL 5;
class AstarSearcher {
public:
AstarSearcher();
~AstarSearcher();
int SearchPath(Eigen::Vector3d start,Eigen::Vector3d end);
void GetPath(vector<Eigen::Vector3d> &vecPath_);
void CreateNodes(SearchNode tNode);
int Extend(int Id);
int FindPotentialWithBT();
char GetCostFromCostMap(double x,double y);
void MakeupPath();
void SetBTNode();
void SetCostmap(costmap_2d::Costmap2D* costmap_);
public:
vector<SearchNode> vecSearchNodes;
int nCountId;
int nRootId;
int nBottomId;
Eigen::Vector3d Start;
Eigen::Vector3d End;
double bEnd;
int nEndId;
vector<Eigen::Vector3d> vecPath;
costmap_2d::Costmap2D* Costmap;
};
#endif //NBD_GLOBAL_PLANNER_ASTAR_SEARCHER_H
#include <global_planner/Astar_searcher.h>
#include <iostream>
AstarSearcher::AstarSearcher(){
}
AstarSearcher::~AstarSearcher(){}
int AstarSearcher::SearchPath(Eigen::Vector3d Start_, Eigen::Vector3d End_){
vecSearchNodes.clear();
nCountId=0;
nRootId=-1;
nBottomId=0;
bEnd=true;
Start=Start_;
End=End_;
SearchNode tStartNode;
tStartNode.Pos=Start;
tStartNode.nSearchNodeId=0;
nCountId++;
tStartNode.dHCost=sqrt((Start[0]-End[0])*(Start[0]-End[0])+(Start[1]-End[1])*(Start[1]-End[1]));
tStartNode.dCost=0;
tStartNode.nFrom=-1;
vecSearchNodes.push_back(tStartNode);
SetBTNode();
while(bEnd){
int n=FindPotentialWithBT();
Extend(n);
}
MakeupPath();
}
void AstarSearcher::SetBTNode(){
int n=vecSearchNodes.size()-1;
vecSearchNodes[n].nBTF=-1;
vecSearchNodes[n].nBTL=-1;
vecSearchNodes[n].nBTR=-1;
if(nRootId==-1){
nRootId=vecSearchNodes[n].nSearchNodeId;
nBottomId=nRootId;
return;
}
int Bottom=nRootId;
while(1){
if(vecSearchNodes[n].dHCost<=vecSearchNodes[Bottom].dHCost){
if(vecSearchNodes[Bottom].nBTL==-1){
vecSearchNodes[n].nBTF=Bottom;
vecSearchNodes[Bottom].nBTL=n;
if(Bottom==nBottomId){
nBottomId=n;
}
break;
}
Bottom=vecSearchNodes[Bottom].nBTL;
}
if(vecSearchNodes[n].dHCost>vecSearchNodes[Bottom].dHCost){
if(vecSearchNodes[Bottom].nBTR==-1){
vecSearchNodes[n].nBTF=Bottom;
vecSearchNodes[Bottom].nBTR=n;
break;
}
Bottom=vecSearchNodes[Bottom].nBTR;
}
}
}
int AstarSearcher::FindPotentialWithBT(){
int nRet=nBottomId;
if(vecSearchNodes[nRet].nBTR!=-1){
if(vecSearchNodes[nRet].nBTF!=-1){
vecSearchNodes[vecSearchNodes[nRet].nBTF].nBTL=vecSearchNodes[nRet].nBTR;
vecSearchNodes[vecSearchNodes[nRet].nBTR].nBTF=vecSearchNodes[nRet].nBTF;
}
else {
vecSearchNodes[vecSearchNodes[nRet].nBTR].nBTF=-1;
nRootId=vecSearchNodes[nRet].nBTR;
}
nBottomId=vecSearchNodes[nRet].nBTR;
while(1){
if(vecSearchNodes[nBottomId].nBTL==-1)
break;
nBottomId=vecSearchNodes[nBottomId].nBTL;
}
} else{
if(vecSearchNodes[nRet].nBTF!=-1){
nBottomId=vecSearchNodes[nRet].nBTF;
vecSearchNodes[vecSearchNodes[nRet].nBTF].nBTL=-1;
} else{
nRootId=-1;
}
}
return nRet;
}
int AstarSearcher::Extend(int Id) {
CreateNodes(vecSearchNodes[Id]);
}
void AstarSearcher::CreateNodes(SearchNode tNode) {
char chrCost=GetCostFromCostMap(tNode.Pos[0],tNode.Pos[1]);
if(chrCost>=253){
return;
}
Eigen::Vector3d DiffPos=End-tNode.Pos;
double DiffYaw=atan(DiffPos[1]/DiffPos[0]);
for(int i=-8;i<8;++i){
SearchNode tNewNode;
double theta=DiffYaw+i*M_PI/9;
double dResol=0.05;
tNewNode.Pos[0]=tNode.Pos[0]+dResol*cos(theta);
tNewNode.Pos[1]=tNode.Pos[1]+dResol*sin(theta);
tNewNode.Pos[2]=theta;
double dx=tNewNode.Pos[0]-End[0];
double dy=tNewNode.Pos[1]-End[1];
tNewNode.dHCost=sqrt(dx*dx+dy*dy);
if(tNewNode.dHCost<dResol){
bEnd=false;
tNewNode.Pos=End;
nEndId=nCountId;
}
tNewNode.dCost=tNode.dCost+dResol;
tNewNode.dHCost+=tNewNode.dCost;
tNewNode.nFrom=tNode.nSearchNodeId;
tNewNode.nSearchNodeId=nCountId;
nCountId++;
vecSearchNodes.push_back(tNewNode);
SetBTNode();
}
}
char AstarSearcher::GetCostFromCostMap(double x,double y){
//TODO
unsigned m,n;
if(!Costmap->worldToMap(x,y,m,n))
return 255;
char chrCost=Costmap->getCost(m,n);
return chrCost;
}
void AstarSearcher::MakeupPath() {
Eigen::Vector3d PathNode=vecSearchNodes[nEndId].Pos;
vecPath.push_back(PathNode);
int nFather=nEndId;
while(nFather!=0){
nFather=vecSearchNodes[nFather].nFrom;
PathNode=vecSearchNodes[nFather].Pos;
vecPath.push_back(PathNode);
std::cout<<PathNode[0]<<","<<PathNode[1]<<std::endl;
}
}
void AstarSearcher::GetPath(vector<Eigen::Vector3d> &vecPath_) {
vecPath_=vecPath;
}
void AstarSearcher::SetCostmap(costmap_2d::Costmap2D *costmap_){
Costmap=costmap_;
}