Kaggle competition: Microsoft Malware Prediction
<SOS> metric: AUC Kaggle data source: microsoft-malware-prediction<define_variables>
Debug = False
MM = ['AvSigVersion','Census_OSVersion','Census_OSBuildRevision','AppVersion','EngineVersion']
NE = ['Census_SystemVolumeTotalCapacity','Census_PrimaryDiskTotalCapacity']
OHE = [ 'RtpStateBitfield','DefaultBrowsersIdentifier', 'AVProductStatesIdentifier',
'AVProductsInstalled', 'AVProductsEnabled', 'CountryIdentifier', 'CityIdentifier',
'GeoNameIdentifier', 'LocaleEnglishNameIdentifier', 'Processor', 'OsBuild', 'OsSuite',
'SmartScreen','Census_MDC2FormFactor', 'Census_OEMNameIdentifier',
'Census_ProcessorCoreCount', 'Census_ProcessorModelIdentifier',
'Census_OSUILocaleIdentifier', 'Census_PrimaryDiskTypeName',
'Census_HasOpticalDiskDrive', 'Census_TotalPhysicalRAM', 'Census_ChassisTypeName',
'Census_InternalPrimaryDiagonalDisplaySizeInInches',
'Census_InternalPrimaryDisplayResolutionHorizontal',
'Census_InternalPrimaryDisplayResolutionVertical',
'Census_PowerPlatformRoleName', 'Census_InternalBatteryType',
'Census_InternalBatteryNumberOfCharges', 'Census_OSEdition', 'Census_GenuineStateName',
'Census_ActivationChannel', 'Census_FirmwareManufacturerIdentifier', 'Census_IsTouchEnabled',
'Census_IsPenCapable', 'Census_IsAlwaysOnAlwaysConnectedCapable', 'Wdft_IsGamer',
'Wdft_RegionIdentifier', 'OsBuildLab', 'OrganizationIdentifier','Platform',
'Census_OEMModelIdentifier', 'IsProtected', 'IeVerIdentifier','Firewall',
'Census_ProcessorManufacturerIdentifier','Census_OSInstallTypeName',
'Census_OSWUAutoUpdateOptionsName','Census_IsFlightingInternal',
'Census_FlightRing','Census_ThresholdOptIn','Census_FirmwareVersionIdentifier',
'Census_IsSecureBootEnabled','Census_IsWIMBootEnabled']
XX = ['SMode','IsBeta', 'OsVer', 'OsPlatformSubRelease', 'SkuEdition', 'AutoSampleOptIn', 'PuaMode',
'UacLuaenable', 'Census_ProcessorClass', 'Census_OSArchitecture', 'Census_OSBranch',
'Census_OSBuildNumber', 'Census_OSSkuName', 'Census_OSInstallLanguageIdentifier',
'Census_IsPortableOperatingSystem', 'Census_IsFlightsDisabled', 'Census_IsVirtualDevice',
'IsSxsPassiveMode','ProductName','HasTpm','Census_DeviceFamily']
XXX = ['MachineIdentifier']
dtypes = {}
for x in OHE+NE+MM: dtypes[x] = 'category'
dtypes['HasDetections'] = 'int8'
if Debug:
df_train = pd.read_csv('.. /input/microsoft-malware-prediction/train.csv', usecols=dtypes.keys() , dtype=dtypes,nrows=10000)
 else:
df_train = pd.read_csv('.. /input/microsoft-malware-prediction/train.csv', usecols=dtypes.keys() , dtype=dtypes)
 if 5244810 in df_train.index:
df_train.loc[5244810,'AvSigVersion'] = '1.273.1144.0'
df_train['AvSigVersion'].cat.remove_categories('1.2&
print('Loaded',len(df_train),'rows of TRAIN.CSV!')
 df_train = df_train.sample(frac=1)
 df_train.reset_index(drop=True,inplace=True)
 if Debug:
df_test = pd.read_csv('.. /input/microsoft-malware-prediction/test.csv', usecols=list(dtypes.keys())[0:-1], dtype=dtypes,nrows=10000)
 else:
df_test = pd.read_csv('.. /input/microsoft-malware-prediction/test.csv', usecols=list(dtypes.keys())[0:-1], dtype=dtypes)
 print('Loaded',len(df_test),'rows of TEST.CSV!' )
best_estimators = []<import_modules>
def factor_data(df_train, df_test, col):
df_comb = pd.concat([df_train[col],df_test[col]],axis=0)
 df_comb,_ = df_comb.factorize(sort=True)
 df_comb += 1
df_comb = np.where(df_comb==0, df_comb.max() +1, df_comb)
 df_train[col] = df_comb[:len(df_train)]
df_test[col] = df_comb[len(df_train):]
del df_comb
mx = max(df_train[col].max() ,df_test[col].max())+1
return mx
def reduce_memory(df,col):
mx = df[col].max()
if mx<256:
df[col] = df[col].astype('uint8')
 elif mx<65536:
df[col] = df[col].astype('uint16')
 else:
df[col] = df[col].astype('uint32')
 def encode_FE_lg(df,col,verbose=1):
ln = 1/df[col].nunique()
vc =(df[col].value_counts(dropna=False, normalize=True)+ln ).map(math.log ).to_dict()
nm = col+'_FE_lg'
df[nm] = df[col].map(vc)
 df[nm] -= df[nm].min()
df[nm] = df[nm]/df[nm].max()
df[nm] = df[nm].astype('float32')
 if verbose==1:
print('FE encoded',col)
 return [nm]
def encode_CE(df, col, filter, zscore, tar='HasDetections', m=0.5, verbose=1):
cv = pd.DataFrame(df[col].value_counts(dropna=False)).reset_index()
cv4 = df.groupby(col)[tar].mean().reset_index().rename({tar:'rate',col:'index'},axis=1)
 d1 = set(cv['index'].unique())
 cv = pd.merge(cv,cv4,on='index',how='left')
 if(len(cv[ cv['index'].isna() ])!=0):
cv.loc[ cv['index'].isna() ,'rate' ] = df.loc[ df[col].isna() ,tar ].mean()
cv = cv[ cv[col]>(filter * len(df)) ]
cv['ratec'] =(df[tar].sum() - cv['rate']*cv[col])/(len(df)-cv[col])
 cv['sd'] = zscore * 0.5 / cv[col].map(lambda x: math.sqrt(x))
cv = cv[(abs(cv['rate']-m)>=cv['sd'])|(abs(cv['ratec']-1+m)>=cv['sd'])]
d2 = set(cv['index'].unique())
 d = list(d1 - d2)
 if(df[col].dtype.name=='category'):
if(not 0 in df[col].cat.categories):
df[col].cat.add_categories(0,inplace=True)
 else:
print('
df.loc[ df[col].isin(d),col ] = 0
if verbose==1:
print('CE encoded',col,'-',len(d2),'values.Removed',len(d),'values')
 mx = df[col].nunique()
return [mx,d2]
def encode_CE_test(df,col,d):
if(df[col].dtype.name=='category'):
if(not 0 in df[col].cat.categories):
df[col].cat.add_categories(0,inplace=True)
 else:
print('
df.loc[ ~df[col].isin(d),col ] = 0
mx = df[col].nunique()
return [mx,d]
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import Lasso, Ridge, ElasticNet, HuberRegressor, BayesianRidge
from sklearn.ensemble import RandomForestRegressor, BaggingRegressor, VotingRegressor, AdaBoostRegressor, GradientBoostingRegressor, ExtraTreesRegressor
from sklearn.tree import DecisionTreeRegressor
from xgboost import XGBRegressor
from sklearn.neighbors import KNeighborsRegressor
import lightgbm as lgbm<train_on_grid>
def makeNew(df,verbose=1,add=0,TS=True,data=0):
old = df.columns
df['AppVersion2'] = df['AppVersion'].apply(lambda x: x.split('.')[1] ).astype('category')
 if TS:
datedictAS = np.load('.. /input/malware-timestamps/AvSigVersionTimestamps.npy')[() ]
df['DateAS'] = df['AvSigVersion'].map(datedictAS)
 datedictOS = np.load('.. /input/malware-timestamps-2/OSVersionTimestamps.npy')[() ]
df['DateOS'] = df['Census_OSVersion'].map(datedictOS)
 df['Lag1'] = df['DateAS'] - df['DateOS']
df['Lag1'] = df['Lag1'].map(lambda x: x.days//7)
 df['Lag1'] = df['Lag1']/52.0
df['Lag1'] = df['Lag1'].astype('float32')
 df['Lag1'].fillna(0,inplace=True)
 if data!=0:
if data==1:
df['Lag5'] = datetime(2018,7,26)- df['DateAS']
elif data==2:
df['Lag5'] = datetime(2018,9,27)- df['DateAS']
elif data==3:
df['Lag5'] = datetime(2018,10,27)- df['DateAS']
df['Lag5'] = df['Lag5'].map(lambda x: x.days//1)
 df.loc[ df['Lag5']<0, 'Lag5' ] = 0
df['Lag5'] = df['Lag5']/365.0
df['Lag5'] = df['Lag5'].astype('float32')
 df['Lag5'].fillna(0,inplace=True)
 del df['DateAS'], df['DateOS']
del datedictAS, datedictOS
x=gc.collect()
for col in NE:
nm = col+'_NE'
df[nm] = df[col].astype('float32')
 df[nm] /= np.std(df[nm])
 new = list(set(df.columns)-set(old))
ret = []
for x in new:
if str(df[x].dtype)=='category':
if add==1: OHE.append(x)
 else:
ret.append(x)
 df[x].fillna(df[x].mean() ,inplace=True)
 if verbose==1:
print('Engineered',len(new),'new features!')
 return ret
params = {
"alpha" : [.01,.1,.5,.7,.9,.95,.99, 1, 5, 10, 20],
"fit_intercept" : [True, False],
"normalize" : [True,False],
"solver" : ['svd', 'cholesky', 'lsqr', 'sparse_cg', 'sag', 'saga'],
"tol" : [0.0001, 0.001, 0.01, 0.1],
"random_state" : [42]
}
ridge = Ridge()
ridge_grid = GridSearchCV(ridge, params, scoring='r2', cv=7, n_jobs=-1)
 ridge_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(ridge_grid.best_params_))
print("Best score: {}".format(ridge_grid.best_score_))
best_estimators.append(["Ridge",ridge_grid.best_estimator_] )<train_on_grid>
FE = []
for col in df_train.columns:
if col=='HasDetections': continue
if df_train[col].nunique() >10:
FE.append(col)
 NUM = makeNew(df_train,verbose=0,add=1,data=1)
 makeNew(df_test,verbose=0,data=2)
 print('Engineered '+str(len(NUM)) +' variables(including NE)')
 ct = len(NUM)+1; cnew = ct
for x in FE:
NUM += encode_FE_lg(df_train,x,verbose=0)
 encode_FE_lg(df_test,x,verbose=0)
 ct += 1
print('Frequency encoded '+str(len(NUM)-cnew)+' variables')
 inps={}; tt = 0
for col in OHE:
factor_data(df_train,df_test,col)
 d = encode_CE(df_train,col,0.001,1)[1]
encode_CE_test(df_test,col,d)
 inps[col] = factor_data(df_train,df_test,col)
 tt += inps[col]
reduce_memory(df_train,col)
 reduce_memory(df_test,col)
 ct += 1
for x in np.unique(NE+MM):
del df_train[x]
if x!='AvSigVersion': del df_test[x]
x = gc.collect()
mm = round(df_train.memory_usage(deep=True ).sum() / 1024**2)
 mm2 = round(df_test.memory_usage(deep=True ).sum() / 1024**2)
 print('Encoded '+str(len(NUM)) +' non-CE variables and '+str(len(OHE)) +' CE containing '+str(tt)+' unique values into '+str(mm)+' Mb memory')
 print('Test memory is '+str(mm2)+' Mb' )
params = {
'alpha' : [.01,.1,.5,.7,.9,.95,.99, 1, 5, 10, 20],
'fit_intercept' : [True, False],
'normalize' : [True,False],
'tol' : [0.0001, 0.001, 0.01, 0.1],
"random_state" : [42]
}
lasso = Lasso()
lasso_grid = GridSearchCV(lasso, params, scoring='r2', cv=7, n_jobs=-1)
 lasso_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(lasso_grid.best_params_))
print("Best score: {}".format(lasso_grid.best_score_))
best_estimators.append(["Lasso",lasso_grid.best_estimator_] )<train_on_grid>
class printAUC(callbacks.Callback):
def __init__(self, X_train, y_train, inps, fes, X_val, y_val, k, ee):
super(printAUC, self ).__init__()
self.bestAUC = 0
self.X_train = X_train
self.y_train = y_train
self.inps = inps
self.fes = fes
self.X_val = X_val
self.y_val = y_val
self.k = k
self.ee = ee
def on_epoch_end(self, epoch, logs={}):
pred = self.model.predict([self.X_train[col] for col in self.inps] + [self.X_train[self.fes]])
 aucTR = roc_auc_score(self.y_train, pred)
 pred = self.model.predict([self.X_val[col] for col in self.inps] + [self.X_val[self.fes]])
 auc = roc_auc_score(self.y_val, pred)
 print("Train AUC: " + str(round(aucTR,5)) +" - Validation AUC: " + str(round(auc,5)))
 if(self.bestAUC < auc):
self.bestAUC = auc
self.model.save("bestNet"+str(self.k)+".h5", overwrite=True)
 return
params = {
"alpha" : [.01,.1,.5,.7,.9,.95,.99, 1, 5, 10, 20],
"fit_intercept" : [True, False],
"normalize" : [True,False],
"tol" : [0.0001, 0.001, 0.01, 0.1],
"random_state" : [42]
}
EL = ElasticNet()
EL_grid = GridSearchCV(EL, params, scoring='r2', cv=7, n_jobs=-1)
 EL_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(EL_grid.best_params_))
print("Best score: {}".format(EL_grid.best_score_))
best_estimators.append(["ElasticNet",EL_grid.best_estimator_] )<train_on_grid>
groups = [ ['CountryIdentifier','CityIdentifier','OrganizationIdentifier','GeoNameIdentifier',
'LocaleEnglishNameIdentifier','Census_OSInstallLanguageIdentifier','Census_OSUILocaleIdentifier',
'Wdft_RegionIdentifier'],
['DefaultBrowsersIdentifier', 'AVProductStatesIdentifier', 'AVProductsInstalled', 'AVProductsEnabled',
'IsProtected', 'SMode', 'IeVerIdentifier', 'SmartScreen', 'Firewall','Census_IsSecureBootEnabled',
'Census_IsWIMBootEnabled','Wdft_IsGamer','Census_OSWUAutoUpdateOptionsName','Census_GenuineStateName',
'AppVersion2'],
['Processor','Census_MDC2FormFactor','Census_DeviceFamily','Census_ProcessorCoreCount','Census_ProcessorClass',
'Census_PrimaryDiskTypeName','Census_HasOpticalDiskDrive','Census_TotalPhysicalRAM','Census_ChassisTypeName',
'Census_InternalPrimaryDiagonalDisplaySizeInInches', 'Census_InternalPrimaryDisplayResolutionHorizontal',
'Census_InternalPrimaryDisplayResolutionVertical', 'Census_PowerPlatformRoleName', 'Census_InternalBatteryType',
'Census_InternalBatteryNumberOfCharges','Census_IsTouchEnabled','Census_IsPenCapable',
'Census_IsAlwaysOnAlwaysConnectedCapable'],
['Census_OEMNameIdentifier', 'Census_OEMModelIdentifier', 'Census_ProcessorManufacturerIdentifier',
'Census_ProcessorModelIdentifier','Census_FirmwareManufacturerIdentifier', 'Census_FirmwareVersionIdentifier']
]
params = {
"alpha" : [.01,.1,.5,.7,.9,.95,.99, 1, 5, 10, 20],
"fit_intercept" : [True, False],
"tol" : [0.0001, 0.001, 0.01, 0.1],
"max_iter": [100, 300 , 500]
}
Huber_R = HuberRegressor()
Huber_R_grid = GridSearchCV(Huber_R, params, scoring='r2', cv=7, n_jobs=-1)
 Huber_R_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(Huber_R_grid.best_params_))
print("Best score: {}".format(Huber_R_grid.best_score_))
best_estimators.append(["HuberRegressor",Huber_R_grid.best_estimator_] )<train_on_grid>
df_train_Y = df_train['HasDetections']
del df_train['HasDetections']
x=gc.collect()
chunk = len(df_train)//5
idx = range(chunk*0,chunk//2)
 idx2 = range(chunk//2,chunk)
 idx3 = range(chunk,chunk*3)
 idx4 = range(chunk*3,chunk*5)
 X_val1 = df_train.loc[idx]
Y_val1 = df_train_Y.loc[idx]
X_val2 = df_train.loc[idx2]
Y_val2 = df_train_Y.loc[idx2]
X_train1 = df_train.loc[idx3]
Y_train1 = df_train_Y.loc[idx3]
X_train2 = df_train.loc[idx4]
Y_train2 = df_train_Y.loc[idx4]
del df_train, df_train_Y
x=gc.collect()
params = {
"max_depth": ["None",10, 30, 50, 75, 100],
"max_features": ["auto",0.3, 0.6],
"min_samples_leaf": [1,3,5,7],
"min_samples_split": [2, 4, 8, 12],
"n_estimators": [30, 50, 100, 200],
"random_state" : [42]
}
RFR = RandomForestRegressor()
RFR_grid = GridSearchCV(RFR, params, scoring='r2', cv=7, n_jobs=-1)
 RFR_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(RFR_grid.best_params_))
print("Best score: {}".format(RFR_grid.best_score_))
best_estimators.append(["RandomForestR",RFR_grid.best_estimator_] )<train_on_grid>
ins = []; outs = {}
for k in inps.keys() :
x = Input(shape=(1,))
ins.append(x)
 y = np.int(inps[k])
 x = Embedding(y, y, input_length=1 )(x)
 x = Reshape(target_shape=(y,))(x)
 outs[k]=x
all = set(inps.keys())
 used = []
outs2 = []
for k in groups:
g = [outs[x] for x in set(k ).intersection(all)]
used += list(set(k ).intersection(all))
x = concatenate(g)
 s = sum([inps[x] for x in set(k ).intersection(all)])
 x = Dense(s//2,kernel_initializer='he_uniform' )(x)
 x = BatchNormalization()(x)
 x = Activation('elu' )(x)
 outs2.append(x)
 g = [outs[x] for x in all-set(used)]
x = concatenate(g)
 s = sum([inps[x] for x in all-set(used)])
 x = Dense(s//2,kernel_initializer='he_uniform' )(x)
 x = BatchNormalization()(x)
 x = Activation('elu' )(x)
 outs2.append(x)
 x = Input(shape=(len(NUM),))
ins.append(x)
 x = Dense(len(NUM)//2,kernel_initializer='he_uniform' )(x)
 x = BatchNormalization()(x)
 x = Activation('elu' )(x)
 x = concatenate(outs2+[x])
 x = Dense(100,kernel_initializer='he_uniform' )(x)
 x = Dropout(0.2 )(x)
 x = BatchNormalization()(x)
 x = Activation('elu' )(x)
 x = Dense(100,kernel_initializer='he_uniform' )(x)
 x = Dropout(0.2 )(x)
 x = BatchNormalization()(x)
 x = Activation('elu' )(x)
 x = Dense(100,kernel_initializer='he_uniform' )(x)
 x = Dropout(0.2 )(x)
 x = BatchNormalization()(x)
 x = Activation('elu' )(x)
 x = Dense(1,activation='sigmoid' )(x)
 model = Model(inputs=ins, outputs=x)
 model.compile(optimizer=Adam(lr=0.01), loss='binary_crossentropy', metrics=['accuracy'])
params = {
"max_depth": ["None",10, 30, 50, 75, 100],
"max_features": ["auto",.3,.4,.5,.6],
"min_samples_leaf": [1,3,5,7],
"min_samples_split": [2, 4, 8, 12],
"n_estimators": [30, 50, 100, 200],
"random_state" : [42]
}
ETR = ExtraTreesRegressor()
ETR_grid = GridSearchCV(ETR, params, scoring='r2', cv=7, n_jobs=-1)
 ETR_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(ETR_grid.best_params_))
print("Best score: {}".format(ETR_grid.best_score_))
best_estimators.append(["ExtraTreesR",ETR_grid.best_estimator_] )<train_on_grid>
epochs=10
batch=256
for k in range(epochs):
model.fit([X_train1[col] for col in OHE] + [X_train1[NUM]],Y_train1,
batch_size=batch, epochs = 1, verbose=2, callbacks=[
printAUC(X_train1, Y_train1, OHE, NUM, X_val1, Y_val1, 0, k)],
validation_data =([X_val1[col] for col in OHE] + [X_val1[NUM]],Y_val1))
model.fit([X_train2[col] for col in OHE] + [X_train2[NUM]],Y_train2,
batch_size=batch, epochs = 1, verbose=2, callbacks=[
printAUC(X_train2, Y_train2, OHE, NUM, X_val2, Y_val2, 0, k)],
validation_data =([X_val2[col] for col in OHE] + [X_val2[NUM]],Y_val2))
X_train1['HasDetections'] = Y_train1
X_train1 = X_train1.sample(frac=1)
 Y_train1 = X_train1['HasDetections']
del X_train1['HasDetections']
X_train2['HasDetections'] = Y_train2
X_train2 = X_train2.sample(frac=1)
 Y_train2 = X_train2['HasDetections']
del X_train2['HasDetections']
x=gc.collect()
params = {
"max_features": [0.2, 0.4, 0.6,1.0],
"n_estimators": [5, 10, 15, 20],
"random_state": [42]
}
BR = BaggingRegressor()
BR_grid = GridSearchCV(BR, params, scoring='r2', cv=7, n_jobs=-1)
 BR_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(BR_grid.best_params_))
print("Best score: {}".format(BR_grid.best_score_))
best_estimators.append(["BaggingRegressorR",BR_grid.best_estimator_] )<train_on_grid>
del model
del X_train1, Y_train1, X_val1, Y_val1
del X_train2, Y_train2, X_val2, Y_val2
del ins, outs, outs2, x
x = gc.collect()
params = {
"learning_rate": [.1,.5,.7,.9,.95,.99, 1],
"colsample_bytree": [.3,.4,.5,.6],
"max_depth": [2, 4],
"alpha": [1, 3, 5],
"subsample": [.5],
"n_estimators": [30, 70, 100, 200],
"random_state" : [42]
}
XGBR = XGBRegressor()
XGBR_grid = GridSearchCV(XGBR, params, scoring='r2', cv=7, n_jobs=-1)
 XGBR_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(XGBR_grid.best_params_))
print("Best score: {}".format(XGBR_grid.best_score_))
best_estimators.append(["XGBoostR",XGBR_grid.best_estimator_] )<train_on_grid>
pred = np.zeros(( len(df_test),1))
print('Predicting test...')
 model = load_model('bestNet0.h5')
 idx = 0; chunk = 1000000
if Debug: chunk = 5000
ct2 = 1;
while idx < len(df_test):
idx2 = min(idx + chunk, len(df_test))
idx = range(idx, idx2)
 pred[idx] += model.predict([df_test.iloc[idx][col] for col in OHE] + [df_test.iloc[idx][NUM]])
 print(' part '+str(ct2)+' done')
 ct2 += 1
idx = idx2
del model
x = gc.collect()
params = {
"max_depth": ["None",10, 40, 80],
"max_features": ["auto","sqrt","log2"],
"min_samples_leaf": [1,3,5,7],
"min_samples_split": [2, 6, 12],
"random_state" : [42],
"splitter" : ["best","random"]
}
DTR = DecisionTreeRegressor()
DTR_grid = GridSearchCV(DTR, params, scoring='r2', cv=7, n_jobs=-1)
 DTR_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(DTR_grid.best_params_))
print("Best score: {}".format(DTR_grid.best_score_))
best_estimators.append(["DecisionTreeR",DTR_grid.best_estimator_] )<train_on_grid>
datedictAS = np.load('.. /input/malware-timestamps/AvSigVersionTimestamps.npy')[() ]
df_test['Date'] = df_test['AvSigVersion'].map(datedictAS)
 df_test['HasDetections'] = pred
df_test['X'] = df_test['Date'] - datetime(2018,11,20,4,0)
 df_test['X'] = df_test['X'].map(lambda x: x.total_seconds() /86400)
 df_test['X'].fillna(0,inplace=True)
 s = 5.813888
df_test['F'] = 1.0
df_test['F'] = 1 - df_test['X']/s
df_test.loc[df_test['X']<=0,'F'] = 1.0
df_test.loc[df_test['X']>s,'F'] = 0
df_test['HasDetections'] *= df_test['F']
pred = df_test['HasDetections']
params = {
"n_estimators": [10, 30, 50, 100],
"learning_rate": [.01, 0.1, 0.5, 0.9, 0.95, 1],
"random_state" : [42]
}
AdaBoostR = AdaBoostRegressor()
AdaBoostR_grid = GridSearchCV(AdaBoostR, params, scoring='r2', cv=7, n_jobs=-1)
 AdaBoostR_grid.fit(X_train, y_train)
 print("Best parameters: {}:".format(AdaBoostR_grid.best_params_))
print("Best score: {}".format(AdaBoostR_grid.best_score_))
best_estimators.append(["AdaBoostR",AdaBoostR_grid.best_estimator_] )<train_on_grid>
print('Writing submission file...')
 if Debug:
submit = pd.read_csv('.. /input/microsoft-malware-prediction/sample_submission.csv', nrows=10000)
 else:
submit = pd.read_csv('.. /input/microsoft-malware-prediction/sample_submission.csv')
 submit['HasDetections'] = pred
submit.to_csv('submission.csv', index=False)
 print('Done!' )