Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<create_dataframe>
import numpy as np
import pandas as pd
import matplotlib as plt
import os
pd.DataFrame({'Noise Factor': noise_factors, 'Loss(each repeat)': losses_ind, 'Losses(mean pred.) ': losses_mean, 'Losses(mean pred.adj.) ': losses_mean_adj} )<save_to_csv>
def missingvalues(df):
miss=df.isnull().sum()
misspercent=100*df.isnull().sum() /len(df)
 misvaltable=pd.concat([miss,misspercent],axis=1)
 misvaltable=misvaltable.rename(columns={0:"missing values",1:"missing percent"})
 return misvaltable
df.dtypes.value_counts()
test_preds = sub.copy()
test_preds[target_cols] = 0
for p in test_pred_all:
test_preds.loc[:,target_cols] += p[target_cols]
test_preds.loc[:,target_cols] /= len(test_pred_all)
 test_preds.loc[x_test['cp_type'] == 0, target_cols] = 0
test_preds.to_csv(f'submission.csv', index=False )<set_options>
categoryweather=df.groupby("holiday" ).nunique()
print(categoryweather)
 df1=pd.get_dummies(df['weather'])
 df1.head()
!pip install /kaggle/input/whl-datasets/iterative_stratification-0.1.6-py3-none-any.whl
warnings.simplefilter("ignore" )<load_from_csv>
l=[ 'season', 'holiday', 'workingday', 'weather', 'temp',
'atemp', 'humidity', 'windspeed']
dftarget=df[["casual","registered","count"]].copy()
dftarget.head()
trainFeatures = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 testFeatures = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 trainTargetsS = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 trainTargetsNS = pd.read_csv('/kaggle/input/lish-moa/train_targets_nonscored.csv')
 submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 for file in(trainFeatures, testFeatures, trainTargetsS, trainTargetsNS):
file.columns = file.columns.str.lower().str.strip().str.replace(' ', '_' ).str.replace('(', '' ).str.replace(')', '')
 print('Train features shape: ', trainFeatures.shape)
 print('Test features shape: ', testFeatures.shape)
 print('Train Targets(Scored)shape: ', trainTargetsS.shape)
 print('Train Targets(Non Scored)shape: ', trainTargetsNS.shape)
 print('------- Train Features view -------')
 trainFeatures.head()<count_values>
dfnew=df.copy()
dfnew.drop(["registered","casual","count"],axis=1,inplace=True)
 dfnew.head()
print('Compound Treatment', round(trainFeatures['cp_type'].value_counts() [0]/len(trainFeatures)* 100, 2), '% of the dataset')
 print('Control Perturbation Treatment', round(trainFeatures['cp_type'].value_counts() [1]/len(trainFeatures)* 100, 2), '% of the dataset' )<feature_engineering>
df2=pd.concat([dfnew,testdf])
 print(df2.shape)
 print(df.shape )
plt.figure(figsize =(20, 6))
targetTypes = []
for column in trainTargetsS.columns:
try:
targetTypes.append(column.rsplit('_', 1)[1])
 except:
targetTypes.append(column.rsplit('_', 1)[0])
 targetTypes = list(dict.fromkeys(targetTypes))
targetTypes.remove('id')
 targetTypes.remove('b')
 targets = {}
for i in targetTypes:
targets[i] = 0
for column in trainTargetsS.columns:
try:
col = column.rsplit('_', 1)[1]
if col not in ['id', 'b']:
targets[col] += 1
else: pass
except:
col = column.rsplit('_', 1)[0]
if col not in ['id', 'b']:
targets[col] += 1
else: pass
targets = pd.DataFrame.from_dict(targets, orient = 'index')
 targets = targets.reset_index()
targets.columns = ['target_type', 'types']
sns.barplot(x = 'target_type', y = 'types', data = targets)
 plt.xticks(rotation = 90)
 plt.title('Types of Target variables')
 plt.show()<categorify>
df=pd.read_csv(".. /input/train.csv")
 df1=pd.get_dummies(df['weather'])
 df1=df1.rename(columns={1:"clear",2:"misty",3:"snow",4:"heavy snow"})
 df=df.drop(["weather"],axis=1)
 df=pd.concat([df,df1],axis=1)
 df.head()
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(trainFeatures)
 test = preprocess(testFeatures)
 del trainTargetsS['sig_id']
trainTargetsS = trainTargetsS.loc[train['cp_type'] == 0].reset_index(drop = True)
 train = train.loc[train['cp_type'] == 0].reset_index(drop = True )<choose_model_class>
df1=pd.get_dummies(df2["weather"])
 df2.drop(["weather"],axis=1,inplace=True)
 df2=pd.concat([df2,df1],axis=1)
 df2.head()
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period = 10),
loss = 'binary_crossentropy', 
)
 return model<normalization>
df2=df2.rename(columns={1:"clear",2:"misty",3:"snow",4:"heavy snow"})
 df2.head()
seed = 42
def iter_shuffled(X, columns_to_shuffle=None, pre_shuffle=False,
random_state=None):
rng = check_random_state(random_state)
 if columns_to_shuffle is None:
columns_to_shuffle = range(X.shape[1])
 if pre_shuffle:
X_shuffled = X.copy()
rng.shuffle(X_shuffled)
 X_res = X.copy()
for columns in tqdm(columns_to_shuffle):
if pre_shuffle:
X_res[:, columns] = X_shuffled[:, columns]
else:
rng.shuffle(X_res[:, columns])
 yield X_res
X_res[:, columns] = X[:, columns]
def get_score_importances(
 score_func,
X,
y,
n_iter=5,
columns_to_shuffle=None,
random_state=None 
):
rng = check_random_state(random_state)
 base_score = score_func(X, y)
 scores_decreases = []
for i in range(n_iter):
scores_shuffled = _get_scores_shufled(
 score_func, X, y, columns_to_shuffle=columns_to_shuffle,
random_state=rng, base_score=base_score 
)
 scores_decreases.append(scores_shuffled)
 return base_score, scores_decreases
def _get_scores_shufled(score_func, X, y, base_score, columns_to_shuffle=None,
random_state=None):
Xs = iter_shuffled(X, columns_to_shuffle, random_state=random_state)
 res = []
for X_shuffled in Xs:
res.append(-score_func(X_shuffled, y)+ base_score)
 return res
def metric(y_true, y_pred):
metrics = []
for i in range(y_pred.shape[1]):
if y_true[:, i].sum() > 1:
metrics.append(log_loss(y_true[:, i], y_pred[:, i].astype(float)))
 return np.mean(metrics)
 perm_imp = np.zeros(train.shape[1])
 all_res = []
for n,(tr, te)in enumerate(KFold(n_splits=7, random_state=0, shuffle=True ).split(trainTargetsS)) :
print(f'Fold {n}')
 model = create_model(len(train.columns))
checkpoint_path = f'repeat:{seed}_Fold:{n}.hdf5'
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 cb_checkpt = ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 0, save_best_only = True,
save_weights_only = True, mode = 'min')
 model.fit(train.values[tr],
trainTargetsS.values[tr],
validation_data=(train.values[te], trainTargetsS.values[te]),
epochs=35, batch_size=128,
callbacks=[reduce_lr_loss, cb_checkpt], verbose=2 
)
 model.load_weights(checkpoint_path)
 def _score(X, y):
pred = model.predict(X)
 return metric(y, pred)
 base_score, local_imp = get_score_importances(_score, train.values[te], trainTargetsS.values[te], n_iter=1, random_state=0)
 all_res.append(local_imp)
 perm_imp += np.mean(local_imp, axis=0)
 print('')
 top_feats = np.argwhere(perm_imp < 0 ).flatten()
top_feats<compute_test_metric>
df1=pd.get_dummies(df2["season"])
 df3=pd.concat([df2,df1],axis=1)
 df3=df3.rename(columns={1:"spring",2:"summer",3:"fall",4:"winter"})
 df3.head()
def metric(y_true, y_pred):
metrics = []
for _target in trainTargetsS.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels = [0,1]))
return np.mean(metrics )<save_to_csv>
df.shape
df0=pd.get_dummies(df["season"])
 df4=pd.concat([df,df0],axis=1)
 df4=df4.rename(columns={1:"spring",2:"summer",3:"fall",4:"winter"})
 df4.head()
print(f'OOF Metric: {metric(trainTargetsS, res)}')
 submission.loc[test['cp_type'] == 1, trainTargetsS.columns] = 0
submission.to_csv('submission.csv', index = False)
 submission.head()<import_modules>
df4["weekend"]=[abs(1-abs(x-y)) for x,y in zip(df4["workingday"],df4["holiday"])]
df4.head()
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
import tensorflow_addons as tfa
from sklearn.model_selection import KFold
from sklearn.metrics import log_loss
from tqdm.notebook import tqdm<load_from_csv>
print(df4.groupby("weekend")["datetime"].nunique())
 print(df4.groupby("holiday")["datetime"].nunique())
 print(df4.groupby("workingday")["datetime"].nunique() )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<categorify>
df3["weekend"]=[abs(1-abs(x-y)) for x,y in zip(df3["workingday"],df3["holiday"])]
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']
train_targets = train_targets.loc[train['cp_type']==0].reset_index(drop=True)
 train = train.loc[train['cp_type']==0].reset_index(drop=True )<choose_model_class>
df4["time"]=pd.to_datetime(df4["datetime"])
 df4.head()
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10),
loss='binary_crossentropy', 
)
 return model<randomize_order>
df4["hours"]=df4["time"].dt.hour
df4.head()
<define_variables>
df3["time"]=pd.to_datetime(df3["datetime"])
 df3.head()
top_feats = [ 1, 2, 3, 4, 5, 6, 7, 9, 11, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31,
32, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, 127, 128,
129, 130, 131, 132, 133, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 156, 157,
158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183,
184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 197,
198, 199, 200, 202, 203, 204, 205, 206, 208, 209, 210, 211, 212,
213, 214, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253,
254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266,
267, 268, 269, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280,
281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 294,
295, 296, 298, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309,
310, 311, 312, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323,
324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336,
337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349,
350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362,
363, 364, 365, 366, 367, 368, 369, 370, 371, 374, 375, 376, 377,
378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 390, 391,
392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404,
405, 406, 407, 408, 409, 411, 412, 413, 414, 415, 416, 417, 418,
419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431,
432, 434, 435, 436, 437, 438, 439, 440, 442, 443, 444, 445, 446,
447, 448, 449, 450, 453, 454, 456, 457, 458, 459, 460, 461, 462,
463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475,
476, 477, 478, 479, 481, 482, 483, 484, 485, 486, 487, 488, 489,
490, 491, 492, 493, 494, 495, 496, 498, 500, 501, 502, 503, 505,
506, 507, 509, 510, 511, 512, 513, 514, 515, 518, 519, 520, 521,
522, 523, 524, 525, 526, 527, 528, 530, 531, 532, 534, 535, 536,
538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 549, 550, 551,
552, 554, 557, 559, 560, 561, 562, 565, 566, 567, 568, 569, 570,
571, 572, 573, 574, 575, 577, 578, 580, 581, 582, 583, 584, 585,
586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 599,
600, 601, 602, 606, 607, 608, 609, 611, 612, 613, 615, 616, 617,
618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630,
631, 632, 633, 634, 635, 636, 637, 638, 639, 641, 642, 643, 644,
645, 646, 647, 648, 649, 650, 651, 652, 654, 655, 656, 658, 659,
660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672,
673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685,
686, 687, 688, 689, 691, 692, 693, 694, 695, 696, 697, 699, 700,
701, 702, 704, 705, 707, 708, 709, 710, 711, 713, 714, 716, 717,
718, 720, 721, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732,
733, 734, 735, 737, 738, 739, 740, 742, 743, 744, 745, 746, 747,
748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 759, 760, 761,
762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774,
775, 776, 777, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788,
789, 790, 792, 793, 794, 795, 796, 797, 798, 800, 801, 802, 803,
804, 805, 806, 808, 809, 811, 813, 814, 815, 816, 817, 818, 819,
821, 822, 823, 825, 826, 827, 828, 829, 830, 831, 832, 834, 835,
837, 838, 839, 840, 841, 842, 845, 846, 847, 848, 850, 851, 852,
854, 855, 856, 858, 859, 860, 861, 862, 864, 866, 867, 868, 869,
870, 871, 872, 873, 874]
print(len(top_feats))<compute_test_metric>
df3["hours"]=df3["time"].dt.hour
df3.head()
def metric(y_true, y_pred):
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels=[0,1]))
return np.mean(metrics )<compute_test_metric>
df4.drop(["time"],axis=1,inplace=True)
 df3.drop(["time"],axis=1,inplace=True )
print(f'OOF Metric: {metric(train_targets, res)}' )<feature_engineering>
df4["logcasual"]=np.log(df4["casual"]+1)
 df4["logcasual"]=np.log(df4["casual"]+1)
 df4["logcasual"]=np.log(df4["casual"]+1)
 df4.head()
ss.loc[test['cp_type']==1, train_targets.columns] = 0<save_to_csv>
df4["logregistered"]=np.log(df4["registered"]+1)
 df4["logcount"]=np.log(df4["count"]+1)
 df4.head()
ss.to_csv('submission.csv', index=False )<import_modules>
df4["time"]=pd.to_datetime(df4["datetime"])
 df4.head()
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.callbacks import ReduceLROnPlateau
import tensorflow_addons as tfa
from sklearn.model_selection import KFold
from sklearn.metrics import log_loss
from tqdm.notebook import tqdm<load_from_csv>
df4["day"]=df4["time"].dt.day
df4.head(25 )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<categorify>
df4["day"]=df4["time"].dt.dayofweek
df4.head()
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']
train_targets = train_targets.loc[train['cp_type']==0].reset_index(drop=True)
 train = train.loc[train['cp_type']==0].reset_index(drop=True )<choose_model_class>
df4["day"]=df4["time"].dt.dayofweek
df4.head(25 )
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10),
loss='binary_crossentropy' 
)
 return model<compute_test_metric>
df5=df.copy()
df5.drop(["holiday","workingday","season"],axis=1,inplace=True)
 df5.corr()
def metric(y_true, y_pred):
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target], labels=[0,1]))
return np.mean(metrics )<train_model>
df4["year"]=df4['time'].dt.year
df4.head()
N_STARTS =3
tf.random.set_seed(42)
 res = train_targets.copy()
submission.loc[:, train_targets.columns] = 0
res.loc[:, train_targets.columns] = 0
for seed in range(N_STARTS):
for n,(tr, te)in enumerate(KFold(n_splits=7, random_state=seed, shuffle=True ).split(train_targets)) :
print(f'Fold Started - {n}')
 model = create_model(len(train.columns))
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, epsilon=1e-3, mode='max')
 model.fit(train.values[tr],
train_targets.values[tr],
validation_data=(train.values[te], train_targets.values[te]),
epochs=25, batch_size=64,shuffle=True,
callbacks=[reduce_lr_loss], verbose=2 
)
 test_predict = model.predict(test.values)
 val_predict = model.predict(train.values[te])
 submission.loc[:, train_targets.columns] += test_predict
res.loc[te, train_targets.columns] += val_predict
print('Fold Completed - ',n)
 submission.loc[:, train_targets.columns] /=(( n+1)* N_STARTS)
 res.loc[:, train_targets.columns] /= N_STARTS<feature_engineering>
df4.groupby("weekend")["datetime"].nunique()
submission.loc[test['cp_type']==1, train_targets.columns] = 0<save_to_csv>
df4["month"]=df4["time"].dt.month
df4.head()
submission.to_csv('submission.csv', index=False )<save_to_csv>
df4["year_bin"]="y0"
df4["year_bin"].loc[(df4["year"]==2011)&(df4["month"]<=3)]="y1"
df4["year_bin"].loc[(df4["year"]==2011)&(df4["month"]>3)&(df4["month"]<=6)]="y2"
df4["year_bin"].loc[(df4["year"]==2011)&(df4["month"]>6)&(df4["month"]<=9)]="y3"
df4["year_bin"].loc[(df4["year"]==2011)&(df4["month"]>9)&(df4["month"]<=12)]="y4"
df4["year_bin"].loc[(df4["year"]==2012)&(df4["month"]<=3)]="y5"
df4["year_bin"].loc[(df4["year"]==2012)&(df4["month"]>3)&(df4["month"]<=6)]="y6"
df4["year_bin"].loc[(df4["year"]==2012)&(df4["month"]>6)&(df4["month"]<=9)]="y7"
df4["year_bin"].loc[(df4["year"]==2012)&(df4["month"]>9)&(df4["month"]<=12)]="y8"
df4.groupby('year_bin')["datetime"].nunique()
submission.to_csv('submission.csv', index=False )<load_from_csv>
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
def load_dataset(root):
csv = pd.read_csv(os.path.join(root, "train.csv"))
data = {}
for i in csv.index:
key = csv["image_id"][i]
bbox = json.loads(csv["bbox"][i])
 bbox = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3], 0.0]
if key in data:
data[key].append(bbox)
 else:
data[key] = [bbox]
return sorted(
 [(k, os.path.join(root, "train", k + ".jpg"), v)for k, v in data.items() ],
key=lambda x: x[0] 
)
 def load_image(path):
with open(path, "rb")as f:
buf = f.read()
return mx.image.imdecode(buf)
 def get_batches(dataset, batch_size, width=512, height=512, net=None, ctx=mx.cpu()):
batches = len(dataset)// batch_size
sampler = Sampler(dataset, width, height, net)
 stack_fn = [gcv.data.batchify.Stack() ]
pad_fn = [gcv.data.batchify.Pad(pad_val=-1)]
if net is None:
batchify_fn = gcv.data.batchify.Tuple(*(stack_fn + pad_fn))
else:
batchify_fn = gcv.data.batchify.Tuple(*(stack_fn * 6 + pad_fn))
with Pool(cpu_count() * 2)as p:
for i in range(batches):
start = i * batch_size
samples = p.map(sampler, range(start, start + batch_size))
batch = batchify_fn(samples)
 yield [x.as_in_context(ctx)for x in batch]
def gauss_blur(image, level):
return cv2.blur(image,(level * 2 + 1, level * 2 + 1))
def gauss_noise(image):
for i in range(image.shape[2]):
c = image[:, :, i]
diff = 255 - c.max() ;
noise = np.random.normal(0, random.randint(1, 6), c.shape)
 noise =(noise - noise.min())/(noise.max() - noise.min())
 noise = diff * noise
image[:, :, i] = c + noise.astype(np.uint8)
 return image
class YOLO3TrainTransform:
def __init__(self, width, height, net, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225), **kwargs):
self._width = width
self._height = height
self._mean = mean
self._std = std
self._fake_x = mx.nd.zeros(( 1, 3, height, width))
net = copy.deepcopy(net)
 net.collect_params().reset_ctx(None)
 with mx.autograd.train_mode() :
_, self._anchors, self._offsets, self._feat_maps, _, _, _, _ = net(self._fake_x)
 self._target_generator = gcv.model_zoo.yolo.yolo_target.YOLOV3PrefetchTargetGenerator(
 num_class=len(net.classes), **kwargs)
 def __call__(self, img, label):
if np.random.uniform(0, 1)> 0.5:
img, expand = gcv.data.transforms.image.random_expand(img, max_ratio=1.5, fill=114, keep_ratio=False)
 bbox = gcv.data.transforms.bbox.translate(label, x_offset=expand[0], y_offset=expand[1])
 else:
img, bbox = img, label
h, w, _ = img.shape
bbox, crop = gcv.data.transforms.experimental.bbox.random_crop_with_constraints(bbox,(w, h))
x0, y0, w, h = crop
img = mx.image.fixed_crop(img, x0, y0, w, h)
 h, w, _ = img.shape
interp = np.random.randint(0, 5)
 img = gcv.data.transforms.image.imresize(img, self._width, self._height, interp=interp)
 bbox = gcv.data.transforms.bbox.resize(bbox,(w, h),(self._width, self._height))
h, w, _ = img.shape
img, flips = gcv.data.transforms.image.random_flip(img, px=0.5, py=0.5)
 bbox = gcv.data.transforms.bbox.flip(bbox,(w, h), flip_x=flips[0], flip_y=flips[1])
 img = gcv.data.transforms.experimental.image.random_color_distort(img)
 img = mx.nd.image.to_tensor(img)
 img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
 gt_bboxes = mx.nd.array(bbox[np.newaxis, :, :4])
 gt_ids = mx.nd.array(bbox[np.newaxis, :, 4:5])
 gt_mixratio = mx.nd.array(bbox[np.newaxis, :, -1:])
 objectness, center_targets, scale_targets, weights, class_targets = self._target_generator(
 self._fake_x, self._feat_maps, self._anchors, self._offsets,
gt_bboxes, gt_ids, gt_mixratio)
 return(img, objectness[0], center_targets[0], scale_targets[0], weights[0],
class_targets[0], gt_bboxes[0])
 class Sampler:
def __init__(self, dataset, width, height, net=None, **kwargs):
self._dataset = dataset
if net is None:
self._training_mode = False
self._transform = gcv.data.transforms.presets.yolo.YOLO3DefaultValTransform(width, height, **kwargs)
 else:
self._training_mode = True
self._transform = YOLO3TrainTransform(width, height, net, **kwargs)
 def __call__(self, idx):
if self._training_mode:
raw, bboxes = self._load_mixup(idx)
 raw = raw.asnumpy()
blur = random.randint(0, 3)
 if blur > 0:
raw = gauss_blur(raw, blur)
 raw = gauss_noise(raw)
 h, w, _ = raw.shape
rot = random.randint(0, 3)
 if rot > 0:
raw = np.rot90(raw, k=rot)
 if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[:, [0, 2]] = raw_bboxes[:, [1, 3]]
bboxes[:, [1, 3]] = w - raw_bboxes[:, [2, 0]]
elif rot == 2:
bboxes[:, [0, 1, 2, 3]] = np.array([[w, h, w, h]])- bboxes[:, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[:, [0, 2]] = h - raw_bboxes[:, [1, 3]]
bboxes[:, [1, 3]] = raw_bboxes[:, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[:, 0] = np.min(raw_bboxes[:, [0, 2]], axis=1)
 bboxes[:, 1] = np.min(raw_bboxes[:, [1, 3]], axis=1)
 bboxes[:, 2] = np.max(raw_bboxes[:, [0, 2]], axis=1)
 bboxes[:, 3] = np.max(raw_bboxes[:, [1, 3]], axis=1)
 raw = mx.nd.array(raw)
 else:
raw = load_image(self._dataset[idx][1])
 bboxes = np.array(self._dataset[idx][2])
 res = self._transform(raw, bboxes)
 return [mx.nd.array(x)for x in res]
def _load_mixup(self, idx1):
r = random.gauss(0.5, 0.5 / 1.96)
 if r > 0.0:
raw1 = load_image(self._dataset[idx1][1])
 bboxes1 = np.array(self._dataset[idx1][2])
 if r >= 1.0:
return raw1, np.hstack([bboxes1, np.full(( bboxes1.shape[0], 1), 1.0)])
 idx2 = random.randint(0, len(self._dataset)- 1)
 raw2 = load_image(self._dataset[idx2][1])
 bboxes2 = np.array(self._dataset[idx2][2])
 if r <= 0.0:
return raw2, np.hstack([bboxes2, np.full(( bboxes2.shape[0], 1), 1.0)])
 h = max(raw1.shape[0], raw2.shape[0])
 w = max(raw1.shape[1], raw2.shape[1])
 mix_raw = mx.nd.zeros(shape=(h, w, 3), dtype="float32")
 mix_raw[:raw1.shape[0], :raw1.shape[1], :] += raw1.astype("float32")* r
mix_raw[:raw2.shape[0], :raw2.shape[1], :] += raw2.astype("float32")*(1.0 - r)
 mix_bboxes = np.vstack([
np.hstack([bboxes1, np.full(( bboxes1.shape[0], 1), r)]),
np.hstack([bboxes2, np.full(( bboxes2.shape[0], 1), 1.0 - r)])
 ])
 return mix_raw.astype("uint8"), mix_bboxes 
<load_pretrained>
l=["hours"]
X=df4[l]
Y=df4["casual"]
dtree=DecisionTreeRegressor(max_depth=3)
 dtree.fit(X,Y )
def load_model(path, ctx=mx.cpu()):
net = gcv.model_zoo.yolo3_darknet53_custom(["wheat"], pretrained_base=False)
 net.set_nms(post_nms=150)
 net.load_parameters(path, ctx=ctx)
 return net 
<normalization>
data = export_graphviz(dtree,out_file=None,
filled=True, rounded=True,
special_characters=True)
 graph = graphviz.Source(data)
 graph
def inference(path):
raw = load_image(path)
 rh, rw, _ = raw.shape
classes_list = []
scores_list = []
bboxes_list = []
for _ in range(5):
img, flips = gcv.data.transforms.image.random_flip(raw, px=0.5, py=0.5)
 x, _ = gcv.data.transforms.presets.yolo.transform_test(img, short=img_s)
 _, _, xh, xw = x.shape
rot = random.randint(0, 3)
 if rot > 0:
x = np.rot90(x.asnumpy() , k=rot, axes=(2, 3))
classes, scores, bboxes = model(mx.nd.array(x, ctx=context))
if rot > 0:
if rot == 1:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = xh - raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = raw_bboxes[0, :, [2, 0]]
elif rot == 2:
bboxes[0, :, [0, 1, 2, 3]] = mx.nd.array([[xw], [xh], [xw], [xh]], ctx=context)- bboxes[0, :, [2, 3, 0, 1]]
elif rot == 3:
raw_bboxes = bboxes.copy()
bboxes[0, :, [0, 2]] = raw_bboxes[0, :, [1, 3]]
bboxes[0, :, [1, 3]] = xw - raw_bboxes[0, :, [2, 0]]
raw_bboxes = bboxes.copy()
bboxes[0, :, 0] = raw_bboxes[0, :, [0, 2]].min(axis=0)
 bboxes[0, :, 1] = raw_bboxes[0, :, [1, 3]].min(axis=0)
 bboxes[0, :, 2] = raw_bboxes[0, :, [0, 2]].max(axis=0)
 bboxes[0, :, 3] = raw_bboxes[0, :, [1, 3]].max(axis=0)
 bboxes[0, :, :] = gcv.data.transforms.bbox.flip(bboxes[0, :, :],(xw, xh), flip_x=flips[0], flip_y=flips[1])
 bboxes[0, :, 0::2] =(bboxes[0, :, 0::2] /(xw - 1)).clip(0.0, 1.0)
 bboxes[0, :, 1::2] =(bboxes[0, :, 1::2] /(xh - 1)).clip(0.0, 1.0)
 classes_list.append([
int(classes[0, i].asscalar())for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 scores_list.append([
scores[0, i].asscalar() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes_list.append([
bboxes[0, i].asnumpy().tolist() for i in range(classes.shape[1])
 if classes[0, i].asscalar() >= 0.0
])
 bboxes, scores, classes = weighted_boxes_fusion(bboxes_list, scores_list, classes_list)
 bboxes[:, 0::2] *= rw - 1
bboxes[:, 1::2] *= rh - 1
return bboxes, scores, classes 
<load_pretrained>
l=["hours"]
X=df4[l]
Y=df4["registered"]
dtree1=DecisionTreeRegressor(max_depth=4)
 dtree1.fit(X,Y )
max_epochs = 15
learning_rate = 0.001
batch_size = 8
img_s = 512
threshold = 0.1
context = mx.gpu()
print("Loading model...")
 model = load_model("/kaggle/input/yolo3-darknet-weights/global-wheat-yolo3-darknet53.params", ctx=context)
 print("Loading test images...")
 test_images = [ 
(os.path.join(dirname, filename), os.path.splitext(filename)[0])
 for dirname, _, filenames in os.walk('/kaggle/input/global-wheat-detection/test')for filename in filenames
]
print("Pseudo labelling...")
 pseudo_set = []
for path, image_id in test_images:
print(path)
 bboxes, scores, classes = inference(path)
 label = [
[round(x)for x in bboxes[i].tolist() ] + [0.0] for i in range(classes.shape[0])
 if model.classes[int(classes[i])] == "wheat" and scores[i] > threshold
]
if len(label)> 0:
pseudo_set.append(( image_id, path, label))
print("Loading training set...")
 dataset = load_dataset("/kaggle/input/global-wheat-detection")
 split = int(len(dataset)* 0.9)
 training_set = dataset[:split] + pseudo_set
print("Training set: ", len(training_set))
validation_set = dataset[split:]
print("Validation set: ", len(validation_set))
print("Re-training...")
 trainer = mx.gluon.Trainer(model.collect_params() , "Nadam", {
"learning_rate": learning_rate
})
 metrics = [gcv.utils.metrics.VOCMApMetric(iou_thresh=iou)for iou in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]]
best_score = 0.0
print("Inference...")
 results = []
for path, image_id in test_images:
print(path)
 bboxes, scores, classes = inference(path)
 bboxes[:, 2:4] -= bboxes[:, 0:2]
results.append({
"image_id": image_id,
"PredictionString": " ".join([
" ".join([str(x)for x in [scores[i]] + [round(x)for x in bboxes[i].tolist() ]])
 for i in range(classes.shape[0])
 if model.classes[int(classes[i])] == "wheat" and scores[i] > threshold
])
 })
 pd.DataFrame(results, columns=['image_id', 'PredictionString'] ).to_csv('submission.csv', index=False)
<install_modules>
data = export_graphviz(dtree1,out_file=None,
filled=True, rounded=True,
special_characters=True)
 graph = graphviz.Source(data)
 graph
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<categorify>
df4["daycas"]="cas0"
df4["daycas"].loc[df4["hours"]<=6.5]="cas1"
df4["daycas"].loc[(df4["hours"]>6.5)&(df4["hours"]<=7.5)]="cas2"
df4["daycas"].loc[(df4["hours"]>7.5)&(df4["hours"]<=8.5)]="cas3"
df4["daycas"].loc[(df4["hours"]>8.5)&(df4["hours"]<=9.5)]="cas4"
df4["daycas"].loc[(df4["hours"]>9.5)&(df4["hours"]<=10.5)]="cas5"
df4["daycas"].loc[(df4["hours"]>10.5)&(df4["hours"]<=19.5)]="cas6"
df4["daycas"].loc[(df4["hours"]>19.5)&(df4["hours"]<=21.5)]="cas7"
df4["daycas"].loc[df4["hours"]>21.5]="cas8"
def get_valid_transforms() :
return A.Compose([
A.Resize(height=512, width=512, p=1.0),
ToTensorV2(p=1.0),
], p=1.0 )<data_type_conversions>
df4.groupby("daycas")["datetime"].nunique()
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<load_pretrained>
df4["dayreg"]="reg0"
df4["dayreg"].loc[df4["hours"]<=0.5]="reg1"
df4["dayreg"].loc[(df4["hours"]>0.5)&(df4["hours"]<=1.5)]="reg2"
df4["dayreg"].loc[(df4["hours"]>1.5)&(df4["hours"]<=4.5)]="reg3"
df4["dayreg"].loc[(df4["hours"]>4.5)&(df4["hours"]<=5.5)]="reg4"
df4["dayreg"].loc[(df4["hours"]>5.5)&(df4["hours"]<=6.5)]="reg5"
df4["dayreg"].loc[(df4["hours"]>6.5)&(df4["hours"]<=8.5)]="reg6"
df4["dayreg"].loc[(df4["hours"]>8.5)&(df4["hours"]<=16.5)]="reg7"
df4["dayreg"].loc[(df4["hours"]>16.5)&(df4["hours"]<=18.5)]="reg8"
df4["dayreg"].loc[(df4["hours"]>18.5)&(df4["hours"]<=20.5)]="reg9"
df4["dayreg"].loc[(df4["hours"]>20.5)&(df4["hours"]<=21.5)]="reg10"
df4["dayreg"].loc[(df4["hours"]>21.5)&(df4["hours"]<=22.5)]="reg11"
df4["dayreg"].loc[df4["hours"]>22.5]="reg12"
df4.groupby("dayreg")["datetime"].nunique()
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_valid_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=4,
shuffle=False,
num_workers=2,
drop_last=False,
collate_fn=collate_fn
)<choose_model_class>
df3["time"]=pd.to_datetime(df3["datetime"])
 df3["year"]=df3['time'].dt.year
df3["month"]=df3["time"].dt.month
df3["day"]=df3["time"].dt.dayofweek
df3.head()
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()<load_pretrained>
df3["year_bin"]="y0"
df3["year_bin"].loc[(df3["year"]==2011)&(df3["month"]<=3)]="y1"
df3["year_bin"].loc[(df3["year"]==2011)&(df3["month"]>3)&(df3["month"]<=6)]="y2"
df3["year_bin"].loc[(df3["year"]==2011)&(df3["month"]>6)&(df3["month"]<=9)]="y3"
df3["year_bin"].loc[(df3["year"]==2011)&(df3["month"]>9)&(df3["month"]<=12)]="y4"
df3["year_bin"].loc[(df3["year"]==2012)&(df3["month"]<=3)]="y5"
df3["year_bin"].loc[(df3["year"]==2012)&(df3["month"]>3)&(df3["month"]<=6)]="y6"
df3["year_bin"].loc[(df3["year"]==2012)&(df3["month"]>6)&(df3["month"]<=9)]="y7"
df3["year_bin"].loc[(df3["year"]==2012)&(df3["month"]>9)&(df3["month"]<=12)]="y8"
df3["daycas"]="cas0"
df3["daycas"].loc[df3["hours"]<=6.5]="cas1"
df3["daycas"].loc[(df3["hours"]>6.5)&(df3["hours"]<=7.5)]="cas2"
df3["daycas"].loc[(df3["hours"]>7.5)&(df3["hours"]<=8.5)]="cas3"
df3["daycas"].loc[(df3["hours"]>8.5)&(df3["hours"]<=9.5)]="cas4"
df3["daycas"].loc[(df3["hours"]>9.5)&(df3["hours"]<=10.5)]="cas5"
df3["daycas"].loc[(df3["hours"]>10.5)&(df3["hours"]<=19.5)]="cas6"
df3["daycas"].loc[(df3["hours"]>19.5)&(df3["hours"]<=21.5)]="cas7"
df3["daycas"].loc[df3["hours"]>21.5]="cas8"
df3["dayreg"]="reg0"
df3["dayreg"].loc[df3["hours"]<=0.5]="reg1"
df3["dayreg"].loc[(df3["hours"]>0.5)&(df3["hours"]<=1.5)]="reg2"
df3["dayreg"].loc[(df3["hours"]>1.5)&(df3["hours"]<=4.5)]="reg3"
df3["dayreg"].loc[(df3["hours"]>4.5)&(df3["hours"]<=5.5)]="reg4"
df3["dayreg"].loc[(df3["hours"]>5.5)&(df3["hours"]<=6.5)]="reg5"
df3["dayreg"].loc[(df3["hours"]>6.5)&(df3["hours"]<=8.5)]="reg6"
df3["dayreg"].loc[(df3["hours"]>8.5)&(df3["hours"]<=16.5)]="reg7"
df3["dayreg"].loc[(df3["hours"]>16.5)&(df3["hours"]<=18.5)]="reg8"
df3["dayreg"].loc[(df3["hours"]>18.5)&(df3["hours"]<=20.5)]="reg9"
df3["dayreg"].loc[(df3["hours"]>20.5)&(df3["hours"]<=21.5)]="reg10"
df3["dayreg"].loc[(df3["hours"]>21.5)&(df3["hours"]<=22.5)]="reg11"
df3["dayreg"].loc[df3["hours"]>22.5]="reg12"
df3.head()
net = load_net('.. /input/wheat-effdet5-fold0-best-checkpoint/fold0-best-all-states.bin' )<feature_engineering>
df6=df4.copy()
df6.drop(["datetime","season","time","count","registered","casual","logcount"],axis=1,inplace=True)
 df6.head()
for images, image_ids in data_loader:
break
images = torch.stack(images ).cuda().float()
det = net(images, torch.tensor([1]*len(images)).float().cuda())
 i = 1
sample = images[i].permute(1, 2, 0 ).cpu().numpy().copy()
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > 0.3)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = boxes.astype(np.int32 ).clip(min=0, max=511)
 fig, ax = plt.subplots(1, 1, figsize=(16, 8))
for box in boxes:
cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),(1, 0, 0), 1)
 ax.set_axis_off()
ax.imshow(sample)
 plt.show()<categorify>
df4["rtemp1"]=df4["temp"]+df4["atemp"]
df4["rtemp2"]=df4["temp"]-df4["atemp"]
df4["rtemp3"]=df4["temp"]*df4["atemp"]
print(df4["temp"].corr(df4["registered"]))
print(df4["rtemp1"].corr(df4["registered"]))
print(df4["rtemp2"].corr(df4["registered"]))
print(df4["rtemp3"].corr(df4["registered"]))
df3["rtemp3"]=df3["temp"]*df3["atemp"]
def process_det(index, det, score_threshold=0.33):
boxes = det[index].detach().cpu().numpy() [:,:4]
scores = det[index].detach().cpu().numpy() [:,4]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes =(boxes ).clip(min=0, max=511 ).astype(int)
 indexes = np.where(scores>score_threshold)
 boxes = boxes[indexes]
scores = scores[indexes]
return boxes, scores<categorify>
df6["rtemp3"]=df6["temp"]*df6["atemp"]
df6.head()
class BaseWheatTTA:

 image_size = 512
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(1)
 def batch_augment(self, images):
return images.flip(2)
 def deaugment_boxes(self, boxes):
boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(2)
 def batch_augment(self, images):
return images.flip(3)
 def deaugment_boxes(self, boxes):
boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 1,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = self.image_size - boxes[:, [1,3]]
res_boxes[:, [1,3]] = boxes[:, [2,0]]
return res_boxes
class TTARotate180(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 2,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 2,(2, 3))
def deaugment_boxes(self, boxes):
boxes[:, [0,1,2,3]] = self.image_size - boxes[:, [2,3,0,1]]
return boxes
class TTARotate270(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 3,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 3,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = boxes[:, [1,3]]
res_boxes[:, [1,3]] = self.image_size - boxes[:, [2,0]]
return res_boxes
class TTACompose(BaseWheatTTA):

 def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
 return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
 return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)
 result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)
 result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)
 result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)
 return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
 return self.prepare_boxes(boxes )<concatenate>
df3.drop(["datetime","season","time"],axis=1,inplace=True )
tta_transforms = []
for tta_combination in product([TTAHorizontalFlip() , None],
[TTAVerticalFlip() , None],
[TTARotate90() , TTARotate180() , TTARotate270() , None]):
tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))<categorify>
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
def make_tta_predictions(images, score_threshold=0.33):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes'] /(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
df7=df6.logcasual
df8=df6.logregistered
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<categorify>
df6.drop(["logcasual",'logregistered'],axis=1,inplace=True)
results = []
for images, image_ids in data_loader:
predictions = make_tta_predictions(images)
 for i, image in enumerate(images):
boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).round().astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
result = {
'image_id': image_id,
'PredictionString': format_prediction_string(boxes, scores)
 }
results.append(result )<save_to_csv>
df6.drop(["atemp"],axis=1,inplace=True )
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head(10 )<define_variables>
df6.drop(["temp"],axis=1,inplace=True )
NMS_IOU_THR = 0.6
NMS_CONF_THR = 0.5
best_iou_thr = 0.6
best_skip_box_thr = 0.43
best_final_score = 0
best_score_threshold = 0
EPO = 15
WEIGHTS = '.. /input/best-yolov5x-fold0-1pt/best_yolov5x_fold0.pt'
CONFIG = '.. /input/best-yolov5x-fold0pt/yolov5x.yaml'
DATA = '.. /input/best-yolov5x-fold0pt/wheat0.yaml'
is_TEST = len(os.listdir('.. /input/global-wheat-detection/test/')) >11
is_AUG = True
is_ROT = True
VALIDATE = True
PSEUDO = True
marking = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
marking[column] = bboxs[:,i]
marking.drop(columns=['bbox'], inplace=True )<feature_engineering>
df6.drop(["rtemp3","month"],axis=1,inplace=True)
 d=pd.read_csv(".. /input/train.csv")
 df6["temp"]=d["temp"]
var=["holiday","workingday","weekend","hours"]
for v in var:
df6[v]=df6[v].astype("category")
def convertTrainLabel() :
df = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
df[column] = bboxs[:,i]
df.drop(columns=['bbox'], inplace=True)
 df['x_center'] = df['x'] + df['w']/2
df['y_center'] = df['y'] + df['h']/2
df['classes'] = 0
df = df[['image_id','x', 'y', 'w', 'h','x_center','y_center','classes']]
index = list(set(df.image_id))
source = 'train'
if True:
for fold in [0]:
val_index = index[len(index)*fold//5:len(index)*(fold+1)//5]
for name, mini in tqdm(df.groupby('image_id')) :
path2save = 'val2017/' if name in val_index else 'train2017/'
os.makedirs('convertor/fold{}/labels/'.format(fold)+path2save, exist_ok=True)
 with open('convertor/fold{}/labels/'.format(fold)+path2save+name+".txt", 'w+')as f:
row = mini[['classes','x_center','y_center','w','h']].astype(float ).values
row = row/1024
row = row.astype(str)
 for j in range(len(row)) :
text = ' '.join(row[j])
 f.write(text)
 f.write("
")
 os.makedirs('convertor/fold{}/images/{}'.format(fold,path2save), exist_ok=True)
 sh.copy(".. /input/global-wheat-detection/{}/{}.jpg".format(source,name),'convertor/fold{}/images/{}/{}.jpg'.format(fold,path2save,name)) 
<concatenate>
dfreg=df6.copy()
dfcas=df6.copy()
dfcas['year']=dfcas.year.replace({2011:0,2012:1})
 dfreg['year']=dfcas.year.replace({2011:0,2012:1})
 dfreg.drop(["daycas"],axis=1,inplace=True)
 dureg1=pd.get_dummies(dfreg["dayreg"])
 dfreg=pd.concat([dfreg,dureg1],axis=1)
 dureg2=pd.get_dummies(dfreg["year_bin"])
 dfreg=pd.concat([dfreg,dureg2],axis=1)
 dureg3=pd.get_dummies(dfreg["day"])
 dfreg=pd.concat([dfreg,dureg3],axis=1)
 dfreg.drop(["dayreg","year_bin","day"],axis=1,inplace=True)
 dfcas.drop(["dayreg"],axis=1,inplace=True)
 ducas1=pd.get_dummies(dfcas["daycas"])
 dfcas=pd.concat([dfcas,ducas1],axis=1)
 ducas2=pd.get_dummies(dfcas["year_bin"])
 dfcas=pd.concat([dfcas,ducas2],axis=1)
 ducas3=pd.get_dummies(dfcas["day"])
 dfcas=pd.concat([dfcas,ducas3],axis=1)
 dfcas.drop(["daycas","year_bin","day"],axis=1,inplace=True )
def run_wbf(boxes, scores, image_size=1024, iou_thr=0.5, skip_box_thr=0.7, weights=None):
labels = [np.zeros(score.shape[0])for score in scores]
boxes = [box/(image_size)for box in boxes]
boxes, scores, labels = weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size)
 return boxes, scores, labels
def TTAImage(image, index):
image1 = image.copy()
if index==0:
rotated_image = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image
elif index==1:
rotated_image2 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image2 = cv2.rotate(rotated_image2, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image2
elif index==2:
rotated_image3 = cv2.rotate(image1, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 rotated_image3 = cv2.rotate(rotated_image3, cv2.ROTATE_90_CLOCKWISE)
 return rotated_image3
elif index == 3:
return image1
def rotBoxes90(boxes, im_w, im_h):
ret_boxes =[]
for box in boxes:
x1, y1, x2, y2 = box
x1, y1, x2, y2 = x1-im_w//2, im_h//2 - y1, x2-im_w//2, im_h//2 - y2
x1, y1, x2, y2 = y1, -x1, y2, -x2
x1, y1, x2, y2 = int(x1+im_w//2), int(im_h//2 - y1), int(x2+im_w//2), int(im_h//2 - y2)
 x1a, y1a, x2a, y2a = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)
 ret_boxes.append([x1a, y1a, x2a, y2a])
 return np.array(ret_boxes)
 def detect1Image(img, img0, model, device, aug):
img = img.transpose(2,0,1)
 img = torch.from_numpy(img ).to(device)
 img = img.float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
 pred = model(img, augment=aug)[0]
pred = non_max_suppression(pred, NMS_CONF_THR, NMS_IOU_THR, merge=True, classes=None, agnostic=False)
 boxes = []
scores = []
for i, det in enumerate(pred):
if det is not None and len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape ).round()
for *xyxy, conf, cls in det:
boxes.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])
 scores.append(conf)
 return np.array(boxes), np.array(scores )<normalization>
x_train,x_test,y_train,y_test=train_test_split(dfreg,df7,random_state=42)
 rf=RandomForestRegressor(n_estimators=500)
 rf.fit(x_train,y_train)
 predictions=rf.predict(x_test)
 mean_squared_error(y_test, predictions )
@jit(nopython=True)
 def calculate_iou(gt, pr, form='pascal_voc')-> float:

 if form == 'coco':
gt = gt.copy()
pr = pr.copy()
gt[2] = gt[0] + gt[2]
gt[3] = gt[1] + gt[3]
pr[2] = pr[0] + pr[2]
pr[3] = pr[1] + pr[3]
dx = min(gt[2], pr[2])- max(gt[0], pr[0])+ 1
if dx < 0:
return 0.0
dy = min(gt[3], pr[3])- max(gt[1], pr[1])+ 1
if dy < 0:
return 0.0
overlap_area = dx * dy
union_area =(
(gt[2] - gt[0] + 1)*(gt[3] - gt[1] + 1)+ 
(pr[2] - pr[0] + 1)*(pr[3] - pr[1] + 1)-
overlap_area 
)
 return overlap_area / union_area
@jit(nopython=True)
 def find_best_match(gts, pred, pred_idx, threshold = 0.5, form = 'pascal_voc', ious=None)-> int:

 best_match_iou = -np.inf
best_match_idx = -1
for gt_idx in range(len(gts)) :
if gts[gt_idx][0] < 0:
continue
iou = -1 if ious is None else ious[gt_idx][pred_idx]
if iou < 0:
iou = calculate_iou(gts[gt_idx], pred, form=form)
 if ious is not None:
ious[gt_idx][pred_idx] = iou
if iou < threshold:
continue
if iou > best_match_iou:
best_match_iou = iou
best_match_idx = gt_idx
return best_match_idx
@jit(nopython=True)
 def calculate_precision(gts, preds, threshold = 0.5, form = 'coco', ious=None)-> float:

 n = len(preds)
 tp = 0
fp = 0
for pred_idx in range(n):
best_match_gt_idx = find_best_match(gts, preds[pred_idx], pred_idx,
threshold=threshold, form=form, ious=ious)
 if best_match_gt_idx >= 0:
tp += 1
gts[best_match_gt_idx] = -1
else:
fp += 1
fn =(gts.sum(axis=1)> 0 ).sum()
return tp /(tp + fp + fn)
 @jit(nopython=True)
 def calculate_image_precision(gts, preds, thresholds =(0.5,), form = 'coco')-> float:

 n_threshold = len(thresholds)
 image_precision = 0.0
ious = np.ones(( len(gts), len(preds)))* -1
for threshold in thresholds:
precision_at_threshold = calculate_precision(gts.copy() , preds, threshold=threshold,
form=form, ious=ious)
 image_precision += precision_at_threshold / n_threshold
return image_precision
iou_thresholds = numba.typed.List()
for x in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]:
iou_thresholds.append(x)
 def validate() :
source = 'convertor/fold0/images/val2017'
weights = 'weights/best.pt'
if not os.path.exists(weights):
weights = WEIGHTS
device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 results = []
for path, img, img0, vid_cap in dataset:
image_id = os.path.basename(path ).split('.')[0]
img = img.transpose(1,2,0)
 enboxes = []
enscores = []
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=is_AUG)
 enboxes.append(boxes)
 enscores.append(scores)
 records = marking[marking['image_id'] == image_id]
gtboxes = records[['x', 'y', 'w', 'h']].values
gtboxes = gtboxes.astype(np.int32 ).clip(min=0, max=1024)
 gtboxes[:, 2] = gtboxes[:, 0] + gtboxes[:, 2]
gtboxes[:, 3] = gtboxes[:, 1] + gtboxes[:, 3]
result = {
'image_id': image_id,
'pred_enboxes': enboxes,
'pred_enscores': enscores,
'gt_boxes': gtboxes,
}
results.append(result)
 return results
def calculate_final_score(all_predictions, iou_thr, skip_box_thr, score_threshold):
final_scores = []
for i in range(len(all_predictions)) :
gt_boxes = all_predictions[i]['gt_boxes'].copy()
enboxes = all_predictions[i]['pred_enboxes'].copy()
enscores = all_predictions[i]['pred_enscores'].copy()
image_id = all_predictions[i]['image_id']
pred_boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 pred_boxes = pred_boxes.astype(np.int32 ).clip(min=0, max=1024)
 indexes = np.where(scores>score_threshold)
 pred_boxes = pred_boxes[indexes]
scores = scores[indexes]
image_precision = calculate_image_precision(gt_boxes, pred_boxes,thresholds=iou_thresholds,form='pascal_voc')
 final_scores.append(image_precision)
 return np.mean(final_scores)
 def show_result(sample_id, preds, gt_boxes):
sample = cv2.imread(f'.. /input/global-wheat-detection/train/{sample_id}.jpg', cv2.IMREAD_COLOR)
 sample = cv2.cvtColor(sample, cv2.COLOR_BGR2RGB)
 fig, ax = plt.subplots(1, 1, figsize=(16, 8))
for pred_box in preds:
cv2.rectangle(
 sample, 
(pred_box[0], pred_box[1]), 
(pred_box[2], pred_box[3]), 
(220, 0, 0), 2 
)
 for gt_box in gt_boxes:
cv2.rectangle(
 sample, 
(gt_box[0], gt_box[1]), 
(gt_box[2], gt_box[3]), 
(0, 0, 220), 2 
)
 ax.set_axis_off()
ax.imshow(sample)
 ax.set_title("RED: Predicted | BLUE - Ground-truth" )<train_model>
rf1=RandomForestRegressor(n_estimators=500)
 rf1.fit(dfcas,df7)
 rf2=RandomForestRegressor(n_estimators=500)
 rf2.fit(dfreg,df8 )
def log(text):
print(text)
 def optimize(space, all_predictions, n_calls=10):
@use_named_args(space)
 def score(**params):
log('-'*10)
 log(params)
 final_score = calculate_final_score(all_predictions, **params)
 log(f'final_score = {final_score}')
 log('-'*10)
 return -final_score
return gp_minimize(func=score, dimensions=space, n_calls=n_calls )<load_pretrained>
df3.drop(["temp","atemp"],axis=1,inplace=True )
def makePseudolabel() :
source = '.. /input/global-wheat-detection/test/'
weights = WEIGHTS
imagenames = os.listdir(source)
 device = torch.device('cuda')if torch.cuda.is_available() else torch.device('cpu')
 model = torch.load(weights, map_location=device)['model'].float()
model.to(device ).eval()
dataset = LoadImages(source, img_size=1024)
 path2save = 'train2017/'
if not os.path.exists('convertor/fold0/labels/'+path2save):
os.makedirs('convertor/fold0/labels/'+path2save)
 if not os.path.exists('convertor/fold0/images/{}'.format(path2save)) :
os.makedirs('convertor/fold0/images/{}'.format(path2save))
for path, img, img0, vid_cap in dataset:
image_id = os.path.basename(path ).split('.')[0]
img = img.transpose(1,2,0)
 enboxes = []
enscores = []
if is_ROT:
for i in range(4):
img1 = TTAImage(img, i)
 boxes, scores = detect1Image(img1, img0, model, device, aug=False)
 for _ in range(3-i):
boxes = rotBoxes90(boxes, *img.shape[:2])
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores = detect1Image(img, img0, model, device, aug=is_AUG)
 enboxes.append(boxes)
 enscores.append(scores)
 boxes, scores, labels = run_wbf(enboxes, enscores, image_size=1024, iou_thr=best_iou_thr, skip_box_thr=best_skip_box_thr)
 boxes = boxes.astype(np.int32 ).clip(min=0, max=1024)
 boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
indices = scores >= best_score_threshold
boxes = boxes[indices]
scores = scores[indices]
lineo = ''
for box in boxes:
x1, y1, w, h = box
xc, yc, w, h =(x1+w/2)/1024,(y1+h/2)/1024, w/1024, h/1024
lineo += '0 %f %f %f %f
'%(xc, yc, w, h)
 fileo = open('convertor/fold0/labels/'+path2save+image_id+".txt", 'w+')
 fileo.write(lineo)
 fileo.close()
sh.copy(".. /input/global-wheat-detection/test/{}.jpg".format(image_id),'convertor/fold0/images/{}/{}.jpg'.format(path2save,image_id)) 
<categorify>
newtest.drop(["rtemp3","month"],axis=1,inplace=True)
 f=pd.read_csv(".. /input/test.csv")
 newtest['year']=newtest.year.replace({2011:0,2012:1})
 newtest["temp"]=f["temp"]
var=["holiday","workingday","weekend","hours"]
for v in var:
newtest[v]=newtest[v].astype("category")
 newtestreg=newtest.copy()
newtestcas=newtest.copy()
newtestreg.drop(["daycas"],axis=1,inplace=True)
 newreg1=pd.get_dummies(newtestreg["dayreg"])
 newtestreg=pd.concat([newtestreg,newreg1],axis=1)
 newreg2=pd.get_dummies(newtestreg["year_bin"])
 newtestreg=pd.concat([newtestreg,newreg2],axis=1)
 newreg3=pd.get_dummies(newtestreg["day"])
 newtestreg=pd.concat([newtestreg,newreg3],axis=1)
 newtestreg.drop(["dayreg","year_bin","day"],axis=1,inplace=True)
 newtestcas.drop(["dayreg"],axis=1,inplace=True)
 newcas1=pd.get_dummies(newtestcas["daycas"])
 newtestcas=pd.concat([newtestcas,newcas1],axis=1)
 newcas2=pd.get_dummies(newtestcas["year_bin"])
 newtestcas=pd.concat([newtestcas,newcas2],axis=1)
 newcas3=pd.get_dummies(newtestcas["day"])
 newtestcas=pd.concat([newtestcas,newcas3],axis=1)
 newtestcas.drop(["daycas","year_bin","day"],axis=1,inplace=True)
 newtest.head()
if PSEUDO or VALIDATE:
convertTrainLabel()<find_best_params>
predictcas=rf1.predict(newtestcas)
 predictcas=np.exp(predictcas)-1
predictreg=rf2.predict(newtestreg)
 predictreg=np.exp(predictreg)-1
if VALIDATE and is_TEST:
all_predictions = validate()

 for score_threshold in tqdm(np.arange(0, 1, 0.01), total=np.arange(0, 1, 0.01 ).shape[0]):
final_score = calculate_final_score(all_predictions, best_iou_thr, best_skip_box_thr, score_threshold)
 if final_score > best_final_score:
best_final_score = final_score
best_score_threshold = score_threshold
print('-'*30)
 print(f'[Best Score Threshold]: {best_score_threshold}')
 print(f'[OOF Score]: {best_final_score:.4f}')
 print('-'*30)
<load_pretrained>
predictcount=np.add(predictcas,predictreg)
 final=pd.DataFrame({"datetime":testdf["datetime"],"count":predictcount})
 final
<save_to_csv><EOS> 
final.to_csv("final2.csv",index=False )