Kaggle competition: Microsoft Malware Prediction
train.dtypes.value_counts()<count_unique_values>
oof = pd.read_csv(".. /input/microsoft-outputs/val_kfold.csv")['0']
oof_2 = pd.read_csv(".. /input/microsoft-outputs/val_stratfold.csv")['0']
predictions = pd.read_csv(".. /input/microsoft-outputs/output_Kfold.csv")["HasDetections"]
predictions_2 = pd.read_csv(".. /input/microsoft-outputs/output_stratfold.csv")["HasDetections"]
train.nunique(axis=0 )<define_variables>
train_stack = np.vstack([oof,oof_2] ).transpose()
test_stack = np.vstack([predictions, predictions_2] ).transpose()
print(f'categorical variables: {cats}')
 print(f'numerical variables: {nums}' )<define_variables>
dtypes = {
'MachineIdentifier': 'category',
'ProductName': 'category',
'EngineVersion': 'category',
'AppVersion': 'category',
'AvSigVersion': 'category',
'IsBeta': 'int8',
'RtpStateBitfield': 'float16',
'IsSxsPassiveMode': 'int8',
'DefaultBrowsersIdentifier': 'float16',
'AVProductStatesIdentifier': 'float32',
'AVProductsInstalled': 'float16',
'AVProductsEnabled': 'float16',
'HasTpm': 'int8',
'CountryIdentifier': 'int16',
'CityIdentifier': 'float32',
'OrganizationIdentifier': 'float16',
'GeoNameIdentifier': 'float16',
'LocaleEnglishNameIdentifier': 'int8',
'Platform': 'category',
'Processor': 'category',
'OsVer': 'category',
'OsBuild': 'int16',
'OsSuite': 'int16',
'OsPlatformSubRelease': 'category',
'OsBuildLab': 'category',
'SkuEdition': 'category',
'IsProtected': 'float16',
'AutoSampleOptIn': 'int8',
'PuaMode': 'category',
'SMode': 'float16',
'IeVerIdentifier': 'float16',
'SmartScreen': 'category',
'Firewall': 'float16',
'UacLuaenable': 'float32',
'Census_MDC2FormFactor': 'category',
'Census_DeviceFamily': 'category',
'Census_OEMNameIdentifier': 'float16',
'Census_OEMModelIdentifier': 'float32',
'Census_ProcessorCoreCount': 'float16',
'Census_ProcessorManufacturerIdentifier': 'float16',
'Census_ProcessorModelIdentifier': 'float16',
'Census_ProcessorClass': 'category',
'Census_PrimaryDiskTotalCapacity': 'float32',
'Census_PrimaryDiskTypeName': 'category',
'Census_SystemVolumeTotalCapacity': 'float32',
'Census_HasOpticalDiskDrive': 'int8',
'Census_TotalPhysicalRAM': 'float32',
'Census_ChassisTypeName': 'category',
'Census_InternalPrimaryDiagonalDisplaySizeInInches': 'float16',
'Census_InternalPrimaryDisplayResolutionHorizontal': 'float16',
'Census_InternalPrimaryDisplayResolutionVertical': 'float16',
'Census_PowerPlatformRoleName': 'category',
'Census_InternalBatteryType': 'category',
'Census_InternalBatteryNumberOfCharges': 'float32',
'Census_OSVersion': 'category',
'Census_OSArchitecture': 'category',
'Census_OSBranch': 'category',
'Census_OSBuildNumber': 'int16',
'Census_OSBuildRevision': 'int32',
'Census_OSEdition': 'category',
'Census_OSSkuName': 'category',
'Census_OSInstallTypeName': 'category',
'Census_OSInstallLanguageIdentifier': 'float16',
'Census_OSUILocaleIdentifier': 'int16',
'Census_OSWUAutoUpdateOptionsName': 'category',
'Census_IsPortableOperatingSystem': 'int8',
'Census_GenuineStateName': 'category',
'Census_ActivationChannel': 'category',
'Census_IsFlightingInternal': 'float16',
'Census_IsFlightsDisabled': 'float16',
'Census_FlightRing': 'category',
'Census_ThresholdOptIn': 'float16',
'Census_FirmwareManufacturerIdentifier': 'float16',
'Census_FirmwareVersionIdentifier': 'float32',
'Census_IsSecureBootEnabled': 'int8',
'Census_IsWIMBootEnabled': 'float16',
'Census_IsVirtualDevice': 'float16',
'Census_IsTouchEnabled': 'int8',
'Census_IsPenCapable': 'int8',
'Census_IsAlwaysOnAlwaysConnectedCapable': 'float16',
'Wdft_IsGamer': 'float16',
'Wdft_RegionIdentifier': 'float16',
'HasDetections': 'int8'
}
nominal_list =cats
num_list = nums<drop_column>
print('Download Train and Test Data.
 ')
 train = reduce_mem_usage(pd.read_csv('.. /input/microsoft-malware-prediction/train.csv', dtype=dtypes, low_memory=True))
train['MachineIdentifier'] = train.index.astype('uint32')
 test = reduce_mem_usage(pd.read_csv('.. /input/microsoft-malware-prediction/test.csv', dtype=dtypes, low_memory=True))
test['MachineIdentifier'] = test.index.astype('uint32' )
train = train.drop('Open_Month',axis=1)
 test= test.drop('Open_Month',axis=1)
 nominal_list.remove('Open_Month' )<feature_engineering>
target = train['HasDetections']
mean_revenue_per_city = train[['City', 'revenue']].groupby('City', as_index=False ).mean()
mean_revenue_per_city.head()
mean_revenue_per_city['revenue'] = mean_revenue_per_city['revenue'].apply(lambda x: int(x/1e6))
mean_revenue_per_city
mean_dict = dict(zip(mean_revenue_per_city.City, mean_revenue_per_city.revenue))
mean_dict<create_dataframe>
train_stack = np.vstack([oof,oof_2] ).transpose()
test_stack = np.vstack([predictions, predictions_2] ).transpose()
folds_stack = RepeatedKFold(n_splits=5, n_repeats=2, random_state=15)
 oof_stack = np.zeros(train_stack.shape[0])
 predictions_3 = np.zeros(test_stack.shape[0])
 for fold_,(trn_idx, val_idx)in enumerate(folds_stack.split(train_stack,target)) :
print("fold {}".format(fold_))
trn_data, trn_y = train_stack[trn_idx], target.iloc[trn_idx].values
val_data, val_y = train_stack[val_idx], target.iloc[val_idx].values
clf_3 = BayesianRidge()
clf_3.fit(trn_data, trn_y)
 oof_stack[val_idx] = clf_3.predict(val_data)
 predictions_3 += clf_3.predict(test_stack)/ 5
<feature_engineering>
sample_submission = pd.read_csv('.. /input/microsoft-malware-prediction/sample_submission.csv')
 sample_submission['HasDetections'] = predictions_3
sample_submission.to_csv('submission_ashish_kfold.csv', index=False )