Kaggle competition: Microsoft Malware Prediction
gbm1 = GradientBoostingRegressor(n_estimators=200, learning_rate=0.01, criterion='mse', random_state=42)
 gb0 = GradientBoostingRegressor(n_estimators=150, learning_rate=0.01, criterion='mse', random_state=42)
 gb1 = GradientBoostingRegressor(n_estimators=100, learning_rate=0.01, criterion='mse', random_state=42)
 gb2 = GradientBoostingRegressor(n_estimators=75, learning_rate=0.005, criterion='mse', random_state=42)
 gb3 = GradientBoostingRegressor(n_estimators=50, learning_rate=0.005, criterion='mse', random_state=42)
 knn = KNeighborsRegressor() 
<compute_test_metric>
!pip install https://s3.amazonaws.com/artifacts.h2o.ai/releases/ai/h2o/pydatatable/0.7.0.dev490/x86_64-centos7/datatable-0.7.0.dev490-cp36-cp36m-linux_x86_64.whl
def rmse(mod):
pred_tr = mod.predict(X_train)
 pred_val = mod.predict(X_valid)
 rmse_tr = round(sqrt(mean_squared_error(pred_tr, y_train)) , 4)
 rmse_val = round(sqrt(mean_squared_error(pred_val, y_valid)) , 4)
 print(f"学習データに対するスコア:{rmse_tr}
検証データに対するスコア:{rmse_val}" )<compute_train_metric>
from sklearn.metrics import log_loss, roc_auc_score
from datetime import datetime
import datatable as dt
from datatable.models import Ftrl
def cross_rmse(mod, X):
tr_scores = cross_val_score(mod, X, y, scoring='neg_root_mean_squared_error',cv=10)
 val_scores = cross_val_score(mod, X, y, scoring='neg_root_mean_squared_error', cv=10)
 val_r2 = cross_val_score(mod, X, y, scoring='r2', cv=10)
 検証データに対するスコア:{-val_scores}")
 print(f"学習に対する平均スコア:{-np.mean(tr_scores)}
検証データに対する平均スコア:{-np.mean(val_scores)}")
 print(f"学習に対するスコアの分散:{np.var(-tr_scores)}
検証に対するスコアの分散:{np.var(-val_scores)}")
 print(val_r2)
 print(np.mean(val_r2))<train_model>
%%time
train = dt.fread('.. /input/train.csv' )
gbm1.fit(gX, y)
 gb0.fit(gX, y)
 gb1.fit(gX, y)
 gb2.fit(gX, y)
 gb3.fit(gX, y)
 knn.fit(kX, y)
<predict_on_test>
%%time
test = dt.fread('.. /input/test.csv' )
def submit(mod, test_fix):
pred = pd.DataFrame(mod.predict(test_fix))
submit = pd.concat([test_id, pred], axis=1 ).rename(columns={0: 'Prediction'})
 return submit<find_best_model_class>
train.nunique()
def ensemble_submit(mod1, mod2, rate1):
pred = pd.DataFrame(mod1.predict(gtest_fix)*rate1 + mod2.predict(ktest_fix)*(1-rate1))
submit = pd.concat([test_id, pred], axis=1 ).rename(columns={0: 'Prediction'})
 return submit
def ensemble_submit3(mod1, mod2, mod3, rate):
pred = pd.DataFrame(mod1.predict(gtest_fix)*rate[0] + mod2.predict(ktest_fix)*rate[1] + mod3.predict(gtest_fix)*rate[2])
 submit = pd.concat([test_id, pred], axis=1 ).rename(columns={0: 'Prediction'})
 return submit
def ensemble_submitx3(mod1, mod2, mod3):
pred = pd.DataFrame(( mod1.predict(gtest_fix)* mod2.predict(ktest_fix)* mod3.predict(gtest_fix)) **(1/3))
submit = pd.concat([test_id, pred], axis=1 ).rename(columns={0: 'Prediction'})
 return submit 
<train_model>
test.nunique()
nn = MLPRegressor(random_state=42)
 nn.fit(gX, y )<save_to_csv>
train[:, 'EngineVersion'].nunique1()
gb4_kn6_submit3.to_csv("gb4_kn6_submit3.csv", index=False)
 gbkngb_55_submit.to_csv("gbkngb_55_submit.csv", index=False)
 gbkngb_523_submit.to_csv("gbkngb_523_submit.csv", index=False)
 gbkngb_433_submit.to_csv("gbkngb_433_submit.csv", index=False)
 test55.to_csv("test55.csv", index=False)
<import_modules>
train_unique = dt.unique(train[:, 'EngineVersion'] ).to_list() [0]
len(train_unique )
pd.set_option('display.max_columns', None)
 warnings.filterwarnings(action="ignore" )<load_from_csv>
test_unique = dt.unique(test[:, 'EngineVersion'] ).to_list() [0]
len(test_unique )
train = pd.read_csv('.. /input/restaurant-revenue-prediction/train.csv.zip')
 test = pd.read_csv('.. /input/restaurant-revenue-prediction/test.csv.zip')
 train.shape, test.shape<data_type_conversions>
intersection = list(set(train_unique)& set(test_unique))
len(intersection )
combine = [train, test]
for dataset in combine:
dataset["Open Date"] = pd.to_datetime(dataset["Open Date"])
 dataset["Standard_date"] = "2015-04-27"
dataset["Standard_date"] = pd.to_datetime(dataset["Standard_date"])
 dataset["Business_Period"] =(dataset["Standard_date"]-dataset["Open Date"] ).apply(lambda x: x.days )<data_type_conversions>
train["Type"] = train["Type"].map({"IL":0,"FC":1,"DT":2} ).astype(int)
 test["Type"] = test["Type"].map({"IL":0,"FC":1,"DT":2,"MB":3} ).astype(int)
 train["City Group"] = train["City Group"].map({"Big Cities":0, "Other":1} ).astype(int)
 test["City Group"] = test["City Group"].map({"Big Cities":0, "Other":1} ).astype(int)
<categorify>
%%time
for f in train.names:
if f not in ['MachineIdentifier', 'HasDetections']:
if train[:, f].ltypes[0] == dt.ltype.str:
print('factorizing %s' % f)
 col_f = pd.concat([train[:, f].to_pandas() , test[:, f].to_pandas() ], ignore_index=True)
 encoding = col_f.groupby(f ).size()
encoding = encoding/len(col_f)
 column = col_f[f].map(encoding ).values.flatten()
del col_f, encoding
gc.collect()
train[:, f] = dt.Frame(column[:8921483])
 test[:, f] = dt.Frame(column[8921483:])
 del column
gc.collect()
train["City"] = train["City"].map({'İstanbul':0, 'Ankara':1, 'Diyarbakır':2, 'Tokat':3, 'Gaziantep':4,
'Afyonkarahisar':5, 'Edirne':6, 'Kocaeli':7, 'Bursa':8, 'İzmir':9, 'Sakarya':10,
'Elazığ':11, 'Kayseri':12, 'Eskişehir':13, 'Şanlıurfa':14, 'Samsun':15, 'Adana':16,
'Antalya':17, 'Kastamonu':18, 'Uşak':19, 'Muğla':20, 'Kırklareli':21, 'Konya':22,
'Karabük':23, 'Tekirdağ':24, 'Denizli':25, 'Balıkesir':26, 'Aydın':27, 'Amasya':28,
'Kütahya':29, 'Bolu':30, 'Trabzon':31, 'Isparta':32, 'Osmaniye':33, 'Niğde':34,
'Rize':35, 'Düzce':36, 'Hatay':37, 'Erzurum':38, 'Mersin':39, 'Zonguldak':40,
'Malatya':41, 'Çanakkale':42, 'Kars':43, 'Batman':44, 'Bilecik':45, 'Giresun':46,
'Sivas':47, 'Kırıkkale':48, 'Mardin':49, 'Erzincan':50, 'Manisa':51,
'Kahramanmaraş':52, 'Yalova':53, 'Tanımsız':54, 'Kırşehir':55, 'Aksaray':56,
'Nevşehir':57, 'Çorum':58, 'Ordu':59, 'Artvin':60, 'Siirt':61, 'Çankırı':62} ).astype(int)
 test["City"] = test["City"].map({'İstanbul':0, 'Ankara':1, 'Diyarbakır':2, 'Tokat':3, 'Gaziantep':4,
'Afyonkarahisar':5, 'Edirne':6, 'Kocaeli':7, 'Bursa':8, 'İzmir':9, 'Sakarya':10,
'Elazığ':11, 'Kayseri':12, 'Eskişehir':13, 'Şanlıurfa':14, 'Samsun':15, 'Adana':16,
'Antalya':17, 'Kastamonu':18, 'Uşak':19, 'Muğla':20, 'Kırklareli':21, 'Konya':22,
'Karabük':23, 'Tekirdağ':24, 'Denizli':25, 'Balıkesir':26, 'Aydın':27, 'Amasya':28,
'Kütahya':29, 'Bolu':30, 'Trabzon':31, 'Isparta':32, 'Osmaniye':33, 'Niğde':34,
'Rize':35, 'Düzce':36, 'Hatay':37, 'Erzurum':38, 'Mersin':39, 'Zonguldak':40,
'Malatya':41, 'Çanakkale':42, 'Kars':43, 'Batman':44, 'Bilecik':45, 'Giresun':46,
'Sivas':47, 'Kırıkkale':48, 'Mardin':49, 'Erzincan':50, 'Manisa':51,
'Kahramanmaraş':52, 'Yalova':53, 'Tanımsız':54, 'Kırşehir':55, 'Aksaray':56,
'Nevşehir':57, 'Çorum':58, 'Ordu':59, 'Artvin':60, 'Siirt':61, 'Çankırı':62} ).astype(int )<feature_engineering>
features = [f for f in train.names if f not in ['HasDetections']]
ftrl = Ftrl(nepochs=2, interactions=True)
train["revenue"] = train.revenue.apply(lambda x:np.log1p(x))<drop_column>
%%time
print('Start Fitting on ', train.shape, ' @ ', datetime.now())
 ftrl.fit(train[:, features], train[:, 'HasDetections'])
 print('Fitted complete on ', train.shape, ' @ ', datetime.now())
 print('Current loss : %.6f'
% log_loss(np.array(train[:, 'HasDetections'])[:, 0],
np.array(ftrl.predict(train[:, features]))))
train = train.drop(["Id"], axis=1)
 train = train.drop(["Open Date"], axis=1)
 train = train.drop(["Standard_date"], axis=1)
 test = test.drop(["Id"], axis=1)
 test = test.drop(["Open Date"], axis=1)
 test = test.drop(["Standard_date"], axis=1 )<prepare_x_and_y>
print('Current AUC : %.6f'
% roc_auc_score(np.array(train[:, 'HasDetections'])[:, 0],
np.array(ftrl.predict(train[:, features]))))
y = train["revenue"]
train = train.drop(["revenue"], axis=1 )<split>
preds1 = np.array(ftrl.predict(test[:, features]))
preds1 = preds1.flatten()
X_train, X_test, y_train, y_test = train_test_split(
 train, y, random_state=0, train_size=0.7,shuffle=False )<train_model>
ftrl = Ftrl(nepochs=20, interactions=False)
 ftrl.fit(train[:, features], train[:, 'HasDetections'])
 preds2 = np.array(ftrl.predict(test[:, features]))
preds2 = preds2.flatten()
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 params = {
'task' : 'train',
'boosting_type' : 'gbdt',
'objective' : 'regression',
'metric' : {'l2'},
'num_leaves' : 31,
'learning_rate' : 0.1,
'feature_fraction' : 0.9,
'bagging_fraction' : 0.8,
'bagging_freq': 5,
'verbose' : 0,
'n_jobs': 2
}
gbm = lgb.train(params,
lgb_train,
num_boost_round=100,
valid_sets=lgb_eval,
early_stopping_rounds=10)
 prediction_lgb = np.exp(gbm.predict(test))<load_from_csv>
np.save('preds1', preds1)
 np.save('preds2', preds2 )
sample =pd.read_csv('/kaggle/input/restaurant-revenue-prediction/sampleSubmission.csv' )<prepare_output>
sample_submission = pd.read_csv('.. /input/sample_submission.csv' )
sample["Prediction"] = prediction_lgb<save_to_csv>
sample_submission['HasDetections'] = 0.6*preds1+0.4*preds2
<set_options><EOS> 
sample_submission.to_csv('datatable_ftrl_submission.csv', index=False )