import pandas as pd import matplotlib.pyplot as plt import xgboost as xgb from sklearn.model_selection import train_test_split from sklearn.metrics import r2_score df = pd.read_excel('../400v入模数据/丽水.xlsx',index_col='stat_date') df.index = pd.to_datetime(df.index) x_train = df.loc['2021-1':'2023-7'][:-3].drop(columns='0.4kv及以下') y_train = df.loc['2021-1':'2023-7'][:-3]['0.4kv及以下'] x_eval = df.loc['2023-7'].drop(columns='0.4kv及以下') y_eval = df.loc['2023-7']['0.4kv及以下'] x_train,x_test,y_train,y_test = train_test_split(x_train,y_train,test_size=0.2,random_state=42) model = xgb.XGBRegressor(max_depth=6,learning_rate=0.05,n_estimators=150) model.fit(x_train,y_train) y_pred = model.predict(x_test) print(r2_score(y_test,y_pred)) predict = model.predict(x_eval) result = pd.DataFrame({'eval':y_eval,'pred':predict},index=y_eval.index) print(result) print((result['eval'][-3:].sum()-result['pred'][-3:].sum())/result['eval'].sum()) import numpy as np X_eval = np.array([ [22.3,16.19,23,1,0], [23.69,14.5,23,0,0], [23.69,14,23,0,0]]) print(model.predict(X_eval))