Kaggle competition: Predictive Equipment Failures
<SOS> metric: meanfscore Kaggle data source: predictive-equipment-failures<choose_model_class>
sns.set()
%matplotlib inline
warnings.filterwarnings('ignore')
 %load_ext tensorboard.notebook
pd.set_option('display.max_columns', None)
pca = PCA(n_components=410 )<normalization>
odf=pd.read_csv('/kaggle/input/equipfails/equip_failures_training_set.csv',index_col=0)
 odft=pd.read_csv('/kaggle/input/equipfails/equip_failures_test_set.csv',index_col=0 )
X_scaled=pca.fit_transform(X_scaled)
 test_X_scaled = pca.transform(test_X_scaled )<compute_test_metric>
df=odf.replace({'na':-999999})
 Xt=odft.replace({'na':-999999})
 Xt=Xt.astype(float)
 df=df.astype(float)
 df['target']=df['target'].astype(int )
def rmse_cv(model,X,y):
rmse = np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv=5))
return rmse<choose_model_class>
X=df.iloc[:,1:]
y=df.iloc[:,0]
Xn=normalize(X)
 Xtn=normalize(Xt )
models = [LinearRegression() ,Ridge() ,Lasso(alpha=0.01,max_iter=10000),RandomForestRegressor() ,GradientBoostingRegressor() ,SVR() ,LinearSVR() ,
ElasticNet(alpha=0.001,max_iter=10000),SGDRegressor(max_iter=1000,tol=1e-3),BayesianRidge() ,KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5),
ExtraTreesRegressor() ,XGBRegressor() ]<compute_train_metric>
X_train, X_test, y_train, y_test = train_test_split(Xn, y, test_size=0.2, random_state=8 )
names = ["LR", "Ridge", "Lasso", "RF", "GBR", "SVR", "LinSVR", "Ela","SGD","Bay","Ker","Extra","Xgb"]
for name, model in zip(names, models):
score = rmse_cv(model, X_scaled, y_log)
 print("{}: {:.6f}, {:.4f}".format(name,score.mean() ,score.std()))<train_on_grid>
my_class = ExtraTreesClassifier(random_state=0)
 my_class.fit(X_train, y_train)
 y_pred= clf.predict(X_test)
 print('accuracy: {}'.format(accuracy_score(y_test, y_pred)))
 print(f'F1: {f1_score(y_test,y_pred)}')
 confusion_matrix(y_test,y_pred )
class grid() :
def __init__(self,model):
self.model = model
def grid_get(self,X,y,param_grid):
grid_search = GridSearchCV(self.model,param_grid,cv=5, scoring="neg_mean_squared_error")
 grid_search.fit(X,y)
 print(grid_search.best_params_, np.sqrt(-grid_search.best_score_))
grid_search.cv_results_['mean_test_score'] = np.sqrt(-grid_search.cv_results_['mean_test_score'])
 print(pd.DataFrame(grid_search.cv_results_)[['params','mean_test_score','std_test_score']] )<train_model>
my_class = AdaBoostClassifier(random_state=0)
 my_class.fit(X_train, y_train)
 y_pred= my_class.predict(X_test)
 print('accuracy: {}'.format(accuracy_score(y_test, y_pred)))
 print(f'F1: {f1_score(y_test,y_pred)}')
 confusion_matrix(y_test,y_pred )
class AverageWeight(BaseEstimator, RegressorMixin):
def __init__(self,mod,weight):
self.mod = mod
self.weight = weight
def fit(self,X,y):
self.models_ = [clone(x)for x in self.mod]
for model in self.models_:
model.fit(X,y)
 return self
def predict(self,X):
w = list()
pred = np.array([model.predict(X)for model in self.models_])
 for data in range(pred.shape[1]):
single = [pred[model,data]*weight for model,weight in zip(range(pred.shape[0]),self.weight)]
w.append(np.sum(single))
return w<choose_model_class>
lg = LogisticRegression(solver='lbfgs', random_state=18)
 lg.fit(X_train, y_train)
 logistic_prediction = lg.predict(X_test)
 score = metrics.accuracy_score(y_test, logistic_prediction)
 print(score)
 confusion_matrix(y_test,logistic_prediction )
lasso = Lasso(alpha=0.0005,max_iter=10000)
 ridge = Ridge(alpha=60)
 svr = SVR(gamma= 0.0004,kernel='rbf',C=13,epsilon=0.009)
 ker = KernelRidge(alpha=0.2 ,kernel='polynomial',degree=3 , coef0=0.8)
 ela = ElasticNet(alpha=0.005,l1_ratio=0.08,max_iter=10000)
 bay = BayesianRidge()<init_hyperparams>
data_dmatrix = xgb.DMatrix(data=Xn,label=y)
 xgc = xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.2,
learning_rate = 0.1,
max_depth = 20, alpha = 10, n_estimators = 700)
 xgc.fit(X,y )
w1 = 0.02
w2 = 0.2
w3 = 0.25
w4 = 0.3
w5 = 0.03
w6 = 0.2<create_dataframe>
pred_train=xgc.predict(X)
 pred_train.sum()
weight_avg = AverageWeight(mod = [lasso,ridge,svr,ker,ela,bay],weight=[w1,w2,w3,w4,w5,w6] )<compute_train_metric>
pred_test=xgc.predict(Xt)
 pred_test.sum()
score = rmse_cv(weight_avg,X_scaled,y_log)
 print(score.mean() )<compute_train_metric>
yt=pd.DataFrame(pred_test)
 yt.index=yt.index+1
yt
score = rmse_cv(weight_avg,X_scaled,y_log)
 print(score.mean() )<train_model>
test=pd.read_csv('.. /input/equipfails/equip_failures_test_set.csv',na_values='na')
 df= pd.DataFrame()
df['id'] = test['id']
df['target'] = pred_test
df.to_csv('submission2.csv', index=False )
class stacking(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self,mod,meta_model):
self.mod = mod
self.meta_model = meta_model
self.kf = KFold(n_splits=5, random_state=42, shuffle=True)
 def fit(self,X,y):
self.saved_model = [list() for i in self.mod]
oof_train = np.zeros(( X.shape[0], len(self.mod)))
 for i,model in enumerate(self.mod):
for train_index, val_index in self.kf.split(X,y):
renew_model = clone(model)
 renew_model.fit(X[train_index], y[train_index])
 self.saved_model[i].append(renew_model)
 oof_train[val_index,i] = renew_model.predict(X[val_index])
 self.meta_model.fit(oof_train,y)
 return self
def predict(self,X):
whole_test = np.column_stack([np.column_stack(model.predict(X)for model in single_model ).mean(axis=1)
 for single_model in self.saved_model])
 return self.meta_model.predict(whole_test)
 def get_oof(self,X,y,test_X):
oof = np.zeros(( X.shape[0],len(self.mod)))
 test_single = np.zeros(( test_X.shape[0],5))
test_mean = np.zeros(( test_X.shape[0],len(self.mod)))
 for i,model in enumerate(self.mod):
for j,(train_index,val_index)in enumerate(self.kf.split(X,y)) :
clone_model = clone(model)
 clone_model.fit(X[train_index],y[train_index])
 oof[val_index,i] = clone_model.predict(X[val_index])
 test_single[:,j] = clone_model.predict(test_X)
 test_mean[:,i] = test_single.mean(axis=1)
 return oof, test_mean<normalization>
file_name='submision.csv'
yt.to_csv(file_name,index=True)
a = Imputer().fit_transform(X_scaled)
 b = Imputer().fit_transform(y_log.values.reshape(-1,1)).ravel()<choose_model_class>
filename = 'Final_Model.mod'
pickle.dump(xgc, open(filename, 'wb'))