You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.7 KiB
Python

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
def normal(x):
high = x.describe()['75%'] + 1.5*(x.describe()['75%']-x.describe()['25%'])
low = x.describe()['25%'] - 1.5*(x.describe()['75%']-x.describe()['25%'])
return x[(x<=high)&(x>=low)]
df = pd.read_excel('../400v入模数据/杭州.xlsx',index_col='stat_date')
df.index = pd.to_datetime(df.index)
x_train = df.loc['2022-1':'2023-8'][:-3].drop(columns='0.4kv及以下')
y_train = df.loc['2022-1':'2023-8'][:-3]['0.4kv及以下']
x_eval = df.loc['2023-8'].drop(columns='0.4kv及以下')
y_eval = df.loc['2023-8']['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())
# list2 = []
# list0 = []
# list1 = []
# for i in ('01','02','03','04','05','06','07','08','09','10','11','12'):
# month_index = df.index.strftime('%Y-%m-%d').str[5:7] == f'{i}'
# print(df.loc[month_index]['0.4kv及以下'].max(),df['0.4kv及以下'].describe()['75%'])
# if df.loc[month_index]['0.4kv及以下'].mean() >= df['0.4kv及以下'].describe()['75%']:
# list2.append(i)
# elif df.loc[month_index]['0.4kv及以下'].mean() <= df['0.4kv及以下'].describe()['25%']:
# list0.append(i)
# else:
# list1.append(i)
# print(list0,list1,list2)