# 对分类变量进行独热编码
data = pd.get_dummies(data, columns=['Annealing_Type', 'Cooling_Type'])
# 划分训练集和测试集
from sklearn.model_selection import train_test_split
X = data.drop(['Material_ID', 'Measurement_Time', 'Temperature'], axis=1) # 特征
y = data['Temperature'] # 目标变量
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.ensemble import RandomForestRegressor
# 创建随机森林回归模型
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
# 训练模型
rf_model.fit(X_train, y_train)
# 预测温度
y_pred = rf_model.predict(X_test)
from sklearn.metrics import mean_squared_error
import numpy as np
# 计算均方根误差
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f'均方根误差 (RMSE): {rmse}')
标签:rf,冷却,风机,train,test,import,model,data,温度
From: https://blog.51cto.com/u_16055028/7732740