|
|
|
import pandas as pd
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
import os
|
|
|
|
from sklearn.metrics import r2_score
|
|
|
|
import xgboost as xgb
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
pd.set_option('display.width',None)
|
|
|
|
|
|
|
|
def normal(s1):
|
|
|
|
high = s1.describe()['75%'] + 1.5*(s1.describe()['75%']-s1.describe()['25%'])
|
|
|
|
low = s1.describe()['25%'] - 1.5 * (s1.describe()['75%'] - s1.describe()['25%'])
|
|
|
|
return s1[(s1>=low)&(s1<=high)]
|
|
|
|
|
|
|
|
df = pd.read_csv('区县400v入模数据.csv',encoding='gbk',index_col='dtdate')
|
|
|
|
df.index = pd.to_datetime(df.index)
|
|
|
|
print(df.head())
|
|
|
|
|
|
|
|
list_org = []
|
|
|
|
list_fl = []
|
|
|
|
list_sc = []
|
|
|
|
for org_name in df['org_name'].drop_duplicates():
|
|
|
|
data = df[df['org_name']==org_name]
|
|
|
|
if org_name.strip()[-4:] != '供电公司':
|
|
|
|
continue
|
|
|
|
data = data.loc[normal(data['0.4kv及以下']).index]
|
|
|
|
X = data.drop(columns=['city_name','org_name','0.4kv及以下'])
|
|
|
|
x = X.loc['2022-1':'2023-7'][:-3]
|
|
|
|
x_eval = X.loc['2023-7']
|
|
|
|
y = data['0.4kv及以下'].loc['2022-1':'2023-7'][:-3]
|
|
|
|
y_eval = data['0.4kv及以下'].loc['2023-7']
|
|
|
|
# plt.plot(range(len(y)),y)
|
|
|
|
# plt.show()
|
|
|
|
|
|
|
|
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=42)
|
|
|
|
|
|
|
|
model = xgb.XGBRegressor(max_depth=6,learning_rate=0.05,n_estimators=150)
|
|
|
|
model.fit(x_train,y_train)
|
|
|
|
|
|
|
|
pred = model.predict(x_test)
|
|
|
|
# print(org_name)
|
|
|
|
list_org.append(org_name)
|
|
|
|
# print(r2_score(pred,y_test))
|
|
|
|
list_sc.append(r2_score(pred,y_test))
|
|
|
|
predict = model.predict(x_eval)
|
|
|
|
result = pd.DataFrame({'real':y_eval,'pred':predict},index=y_eval.index)
|
|
|
|
# print(result)
|
|
|
|
# print((result['real'][-3:]-result['pred'][-3:]).sum()/result['real'].sum())
|
|
|
|
list_fl.append((result['real'][-3:]-result['pred'][-3:]).sum()/result['real'].sum())
|
|
|
|
|
|
|
|
df = pd.DataFrame({'org':list_org,'sc':list_sc,'goal':list_fl})
|
|
|
|
print(df)
|
|
|
|
print(df['goal'].value_counts(bins=[-0.05,-0.01,-0.005,0, 0.005, 0.01, 0.02,0.05],sort=False))
|