Kaggle competition: Predictive Equipment Failures
<SOS> metric: meanfscore Kaggle data source: predictive-equipment-failures<import_modules>
sns.set()
%matplotlib inline
warnings.filterwarnings('ignore')
 %load_ext tensorboard.notebook
pd.set_option('display.max_columns', None)
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import RobustScaler, StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import Pipeline, make_pipeline
from scipy.stats import skew
from sklearn.decomposition import PCA, KernelPCA
from sklearn.preprocessing import Imputer<import_modules>
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 )
from sklearn.model_selection import cross_val_score, GridSearchCV, KFold
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, ExtraTreesRegressor
from sklearn.svm import SVR, LinearSVR
from sklearn.linear_model import ElasticNet, SGDRegressor, BayesianRidge
from sklearn.kernel_ridge import KernelRidge
from xgboost import XGBRegressor<load_from_csv>
df=odf.replace({'na':-999999})
 Xt=odft.replace({'na':-999999})
 Xt=Xt.astype(float)
 df=df.astype(float)
 df['target']=df['target'].astype(int )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
X=df.iloc[:,1:]
y=df.iloc[:,0]
Xn=normalize(X)
 Xtn=normalize(Xt )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<drop_column>
X_train, X_test, y_train, y_test = train_test_split(Xn, y, test_size=0.2, random_state=8 )
train.drop(train[(train["GrLivArea"]>4000)&(train["SalePrice"]<300000)].index,inplace=True )<concatenate>
from sklearn.metrics import f1_score
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.model_selection import StratifiedKFold
full=pd.concat([train,test], ignore_index=True )<sort_values>
def cross_validate_random_forest(X, y, Xt, yt, criterion, max_height, n_folds):
train_acc=[]
test_acc=[]
general_acc=[]
all_feature_importance=[]
f1s=[]
f1su=0
skf=StratifiedKFold(n_splits=n_folds, shuffle=True)
 for train_index, test_index in skf.split(X, y):
Xtrain, Xtest = X.iloc[train_index], X.iloc[test_index]
ytrain, ytest = y.iloc[train_index].values.ravel() , y.iloc[test_index].values.ravel()
Xtrain_min, Xtrain_max = Xtrain.min() , Xtrain.max()
Xtrain =(Xtrain - Xtrain_min)/(Xtrain_max-Xtrain_min)
 Xtest =(Xtest - Xtrain_min)/(Xtrain_max-Xtrain_min)
 Xtrain = Xtrain[Xtrain.columns[~(Xtrain_min==Xtrain_max)]]
Xtest = Xtest [Xtest.columns[~(Xtrain_min==Xtrain_max)]]
Xt_min, Xt_max = Xt.min() , Xt.max()
Xt=(Xt - Xt_min)/(Xt_max-Xt_min)
 Xt = Xt[Xt.columns[~(Xt_min==Xt_max)]]
dt=RFC(n_estimators=100,criterion=criterion,max_depth=max_height)
 dt.fit(Xtrain, ytrain)
 pred = dt.predict(Xtest)
 general_acc.append(accuracy_score(ytest,pred))
f1s.append(f1_score(ytest,pred))
f1su+=f1_score(ytest,pred)
 pred = dt.predict(Xtrain)
 train_acc.append(accuracy_score(ytrain,pred))
if Xt is not None and yt is not None:
pred = dt.predict(Xt)
 test_acc.append(accuracy_score(yt,pred))
all_feature_importance.append(dt.feature_importances_)
 return [[np.mean(general_acc),np.std(general_acc)],[np.mean(train_acc),np.std(train_acc)],[np.mean(test_acc),np.std(test_acc)],all_feature_importance,f1s,f1su]
aa = full.isnull().sum()
aa[aa>0].sort_values(ascending=False )<groupby>
n_folds=10
criterion='gini'
max_height=None
results=cross_validate_random_forest(pd.DataFrame(X_train),pd.DataFrame(y_train),pd.DataFrame(X_test),pd.DataFrame(y_test),criterion, max_height, n_folds )
full.groupby(['Neighborhood'])[['LotFrontage']].agg(['mean','median','count'] )<feature_engineering>
print('General Accuracy: ',results[0][0], 'STD: ',results[0][1])
 print('Train Accuracy: ',results[1][0])
 print('Test Accuracy: ',results[2][0])
 print('F1 Avg: ',results[-1]/10 )
full["LotAreaCut"] = pd.qcut(full.LotArea,10 )<groupby>
my_class = ExtraTreesClassifier(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 )
full.groupby(['LotAreaCut'])[['LotFrontage']].agg(['mean','median','count'] )<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 )
full['LotFrontage']=full.groupby(['LotAreaCut','Neighborhood'])['LotFrontage'].transform(lambda x: x.fillna(x.median()))<feature_engineering>
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 )
full['LotFrontage']=full.groupby(['LotAreaCut'])['LotFrontage'].transform(lambda x: x.fillna(x.median()))<categorify>
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 )
cols=["MasVnrArea", "BsmtUnfSF", "TotalBsmtSF", "GarageCars", "BsmtFinSF2", "BsmtFinSF1", "GarageArea"]
for col in cols:
full[col].fillna(0, inplace=True )<categorify>
pred_train=xgc.predict(X)
 pred_train.sum()
cols1 = ["PoolQC" , "MiscFeature", "Alley", "Fence", "FireplaceQu", "GarageQual", "GarageCond", "GarageFinish", "GarageYrBlt", "GarageType", "BsmtExposure", "BsmtCond", "BsmtQual", "BsmtFinType2", "BsmtFinType1", "MasVnrType"]
for col in cols1:
full[col].fillna("None", inplace=True )<data_type_conversions>
pred_test=xgc.predict(Xt)
 pred_test.sum()
cols2 = ["MSZoning", "BsmtFullBath", "BsmtHalfBath", "Utilities", "Functional", "Electrical", "KitchenQual", "SaleType","Exterior1st", "Exterior2nd"]
for col in cols2:
full[col].fillna(full[col].mode() [0], inplace=True )<filter>
yt=pd.DataFrame(pred_test)
 yt.index=yt.index+1
yt
full.isnull().sum() [full.isnull().sum() >0]<data_type_conversions>
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 )
NumStr = ["MSSubClass","BsmtFullBath","BsmtHalfBath","HalfBath","BedroomAbvGr","KitchenAbvGr","MoSold","YrSold","YearBuilt","YearRemodAdd","LowQualFinSF","GarageYrBlt"]
for col in NumStr:
full[col]=full[col].astype(str )<groupby>
file_name='submision.csv'
yt.to_csv(file_name,index=True)
<categorify><EOS> 
filename = 'Final_Model.mod'
pickle.dump(xgc, open(filename, 'wb'))