Kaggle competition: Predictive Equipment Failures
<SOS> metric: meanfscore Kaggle data source: predictive-equipment-failures<categorify>
sns.set()
%matplotlib inline
warnings.filterwarnings('ignore')
 %load_ext tensorboard.notebook
pd.set_option('display.max_columns', None)
map_values()<drop_column>
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 )
full.drop("LotAreaCut",axis=1,inplace=True)
 full.drop(['SalePrice'],axis=1,inplace=True )<categorify>
df=odf.replace({'na':-999999})
 Xt=odft.replace({'na':-999999})
 Xt=Xt.astype(float)
 df=df.astype(float)
 df['target']=df['target'].astype(int )
class labelenc(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def fit(self,X,y=None):
return self
def transform(self,X):
lab=LabelEncoder()
X["YearBuilt"] = lab.fit_transform(X["YearBuilt"])
 X["YearRemodAdd"] = lab.fit_transform(X["YearRemodAdd"])
 X["GarageYrBlt"] = lab.fit_transform(X["GarageYrBlt"])
 return X<categorify>
X=df.iloc[:,1:]
y=df.iloc[:,0]
Xn=normalize(X)
 Xtn=normalize(Xt )
class skew_dummies(BaseEstimator, TransformerMixin):
def __init__(self,skew=0.5):
self.skew = skew
def fit(self,X,y=None):
return self
def transform(self,X):
X_numeric=X.select_dtypes(exclude=["object"])
 skewness = X_numeric.apply(lambda x: skew(x))
skewness_features = skewness[abs(skewness)>= self.skew].index
X[skewness_features] = np.log1p(X[skewness_features])
 X = pd.get_dummies(X)
 return X<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(Xn, y, test_size=0.2, random_state=8 )
pipe = Pipeline([ 
('labenc', labelenc()), 
('skew_dummies', skew_dummies(skew=1)) ,
] )<create_dataframe>
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 )
full2 = full.copy()<feature_engineering>
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 )
data_pipe = pipe.fit_transform(full2 )<normalization>
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 )
scaler = RobustScaler()<prepare_x_and_y>
data_dmatrix = xgb.DMatrix(data=Xn,label=y)
 xgc = xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.15,
learning_rate = 0.1,
max_depth = 20, alpha = 12, n_estimators = 700)
 xgc.fit(X,y )
n_train=train.shape[0]
X = data_pipe[:n_train]
test_X = data_pipe[n_train:]
y= train.SalePrice
X_scaled = scaler.fit(X ).transform(X)
 y_log = np.log(train.SalePrice)
 test_X_scaled = scaler.transform(test_X )<train_model>
pred_train=xgc.predict(X)
 pred_train.sum()
lasso=Lasso(alpha=0.001)
 lasso.fit(X_scaled,y_log )<create_dataframe>
pred_test=xgc.predict(Xt)
 pred_test.sum()
FI_lasso = pd.DataFrame({"Feature Importance":lasso.coef_}, index=data_pipe.columns )<categorify>
yt=pd.DataFrame(pred_test)
 yt.index=yt.index+1
yt
class add_feature(BaseEstimator, TransformerMixin):
def __init__(self,additional=1):
self.additional = additional
def fit(self,X,y=None):
return self
def transform(self,X):
if self.additional==1:
X["TotalHouse"] = X["TotalBsmtSF"] + X["1stFlrSF"] + X["2ndFlrSF"]
X["TotalArea"] = X["TotalBsmtSF"] + X["1stFlrSF"] + X["2ndFlrSF"] + X["GarageArea"]
else:
X["TotalHouse"] = X["TotalBsmtSF"] + X["1stFlrSF"] + X["2ndFlrSF"]
X["TotalArea"] = X["TotalBsmtSF"] + X["1stFlrSF"] + X["2ndFlrSF"] + X["GarageArea"]
X["+_TotalHouse_OverallQual"] = X["TotalHouse"] * X["OverallQual"]
X["+_GrLivArea_OverallQual"] = X["GrLivArea"] * X["OverallQual"]
X["+_oMSZoning_TotalHouse"] = X["oMSZoning"] * X["TotalHouse"]
X["+_oMSZoning_OverallQual"] = X["oMSZoning"] + X["OverallQual"]
X["+_oMSZoning_YearBuilt"] = X["oMSZoning"] + X["YearBuilt"]
X["+_oNeighborhood_TotalHouse"] = X["oNeighborhood"] * X["TotalHouse"]
X["+_oNeighborhood_OverallQual"] = X["oNeighborhood"] + X["OverallQual"]
X["+_oNeighborhood_YearBuilt"] = X["oNeighborhood"] + X["YearBuilt"]
X["+_BsmtFinSF1_OverallQual"] = X["BsmtFinSF1"] * X["OverallQual"]
X["-_oFunctional_TotalHouse"] = X["oFunctional"] * X["TotalHouse"]
X["-_oFunctional_OverallQual"] = X["oFunctional"] + X["OverallQual"]
X["-_LotArea_OverallQual"] = X["LotArea"] * X["OverallQual"]
X["-_TotalHouse_LotArea"] = X["TotalHouse"] + X["LotArea"]
X["-_oCondition1_TotalHouse"] = X["oCondition1"] * X["TotalHouse"]
X["-_oCondition1_OverallQual"] = X["oCondition1"] + X["OverallQual"]
X["Bsmt"] = X["BsmtFinSF1"] + X["BsmtFinSF2"] + X["BsmtUnfSF"]
X["Rooms"] = X["FullBath"]+X["TotRmsAbvGrd"]
X["PorchArea"] = X["OpenPorchSF"]+X["EnclosedPorch"]+X["3SsnPorch"]+X["ScreenPorch"]
X["TotalPlace"] = X["TotalBsmtSF"] + X["1stFlrSF"] + X["2ndFlrSF"] + X["GarageArea"] + X["OpenPorchSF"]+X["EnclosedPorch"]+X["3SsnPorch"]+X["ScreenPorch"]
return X<categorify>
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 )
pipe = Pipeline([ 
('labenc', labelenc()), 
('add_feature', add_feature(additional=2)) , 
('skew_dummies', skew_dummies(skew=1)) ,
] )<feature_engineering>
file_name='submision.csv'
yt.to_csv(file_name,index=True)
<prepare_x_and_y><EOS> 
filename = 'Final_Model.mod'
pickle.dump(xgc, open(filename, 'wb'))