<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import joblib
!pip install autorch > log.txt
c = joblib.load('/content/drive/MyDrive/台塑輕油案子/data/c620/col_names/c620_col_names.pkl')
c620_df = pd.read_csv('/content/drive/MyDrive/台塑輕油案子/data/c620/cleaned/c620_train.csv',index_col=0).dropna(axis=0)
print(c620_df.shape)
c620_df.head(3)
x_col = c['case']+c['x41']
op_col = c['density']+c['yRefluxRate']+c['yHeatDuty']+c['yControl']
sp_col = c['vent_gas_sf'] +c['distillate_sf'] +c['sidedraw_sf'] +c['bottoms_sf']
wt_col = c['vent_gas_x'] +c['distillate_x'] +c['sidedraw_x'] +c['bottoms_x']
y_col = sp_col + op_col
n_idx = [ [i,i+41,i+41*2,i+41*3]for i in range(41)]
for idx in n_idx:
  assert np.allclose(c620_df[y_col].iloc[:,idx].sum(axis=1),1.0) # check
print(len(x_col),len(y_col))
#c620_df.loc[['001-002', '001-005', '001-008', '001-011', '001-014'],
#            x_col+y_col].to_excel('/content/drive/MyDrive/台塑輕油案子/data/c620/Data_format_example/c620_data.xlsx')
from autorch.utils import PartBulider
c620 = PartBulider(c620_df,x_col,y_col)
c620.train()
c620.test(e=0.01)<jupyter_output><empty_output><jupyter_text>分離係數原始預測 不能滿足化工約束條件<jupyter_code>sp = c620.predict(c620.data['X_test']).filter(regex='Split Factor')
for i in range(41):
  assert np.allclose(sp.iloc[:,[i,i+41,i+41*2,i+41*3]].sum(axis=1).values,1)<jupyter_output><empty_output><jupyter_text>因此做分離係數後處理<jupyter_code>sp = c620.predict(c620.data['X_test']).filter(regex='Split Factor').clip(0,1)
for i in range(41):
  total_sum = sp.iloc[:,[i,i+41,i+41*2,i+41*3]].sum(axis=1).values.reshape(-1,1)
  sp.iloc[:,[i,i+41,i+41*2,i+41*3]] = sp.iloc[:,[i,i+41,i+41*2,i+41*3]]/total_sum<jupyter_output><empty_output><jupyter_text>確保分離係數後處理沒問題<jupyter_code>for i in range(41):
  assert np.allclose(sp.iloc[:,[i,i+41,i+41*2,i+41*3]].sum(axis=1).values,1)<jupyter_output><empty_output><jupyter_text>再次測試預測表現<jupyter_code>y_real = c620.data['Y_test']
y_pred = c620.predict(c620.data['X_test'])
y_pred.update(sp)
c620.show_metrics(y_real,y_pred)
from autorch.function import sp2wt

x_test = c620.data['X_test']
x41 = x_test[c['x41']].values
#sp = c620.predict(x_test).iloc[:,:41*4]
s1,s2,s3,s4 = sp.iloc[:,:41].values,sp.iloc[:,41:41*2].values,sp.iloc[:,41*2:41*3].values,sp.iloc[:,41*3:41*4].values
w1,w2,w3,w4 = sp2wt(x41,s1),sp2wt(x41,s2),sp2wt(x41,s3),sp2wt(x41,s4)
wt_pred = np.hstack((w1,w2,w3,w4))
wt_pred = pd.DataFrame(wt_pred,index=x_test.index,columns=wt_col)
wt_pred.head(3)
wt_real = c620_df.loc[x_test.index,wt_col]
wt_real.head(3)
c620.show_metrics(wt_real,wt_pred,e=0.01).fillna(0)
c620.shrink() # 節省空間
#joblib.dump(c620,'/content/drive/MyDrive/台塑輕油案子/data/c620/model/c620.pkl')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup