Kaggle competition: Freesound Audio Tagging 2019
y_pred=model(train,y,train)
 mean_absolute_error(y_pred,y )<train_model>
BaseShuffleSplit, _validate_shuffle_split
def IterativeStratification(labels, r, random_state):

 n_samples = labels.shape[0]
test_folds = np.zeros(n_samples, dtype=int)
 c_folds = r * n_samples
c_folds_labels = np.outer(r, labels.sum(axis=0))
labels_not_processed_mask = np.ones(n_samples, dtype=bool)
 while np.any(labels_not_processed_mask):
num_labels = labels[labels_not_processed_mask].sum(axis=0)
 if num_labels.sum() == 0:
sample_idxs = np.where(labels_not_processed_mask)[0]
for sample_idx in sample_idxs:
fold_idx = np.where(c_folds == c_folds.max())[0]
if fold_idx.shape[0] > 1:
fold_idx = fold_idx[random_state.choice(fold_idx.shape[0])]
test_folds[sample_idx] = fold_idx
c_folds[fold_idx] -= 1
break
label_idx = np.where(num_labels == num_labels[np.nonzero(num_labels)].min())[0]
if label_idx.shape[0] > 1:
label_idx = label_idx[random_state.choice(label_idx.shape[0])]
sample_idxs = np.where(np.logical_and(labels[:, label_idx].flatten() , labels_not_processed_mask)) [0]
for sample_idx in sample_idxs:
label_folds = c_folds_labels[:, label_idx]
fold_idx = np.where(label_folds == label_folds.max())[0]
if fold_idx.shape[0] > 1:
temp_fold_idx = np.where(c_folds[fold_idx] ==
c_folds[fold_idx].max())[0]
fold_idx = fold_idx[temp_fold_idx]
if temp_fold_idx.shape[0] > 1:
fold_idx = fold_idx[random_state.choice(temp_fold_idx.shape[0])]
test_folds[sample_idx] = fold_idx
labels_not_processed_mask[sample_idx] = False
c_folds_labels[fold_idx, labels[sample_idx]] -= 1
c_folds[fold_idx] -= 1
return test_folds
class MultilabelStratifiedKFold(_BaseKFold):

 def __init__(self, n_splits=3, shuffle=False, random_state=None):
super(MultilabelStratifiedKFold, self ).__init__(n_splits, shuffle, random_state)
 def _make_test_folds(self, X, y):
y = np.asarray(y, dtype=bool)
 type_of_target_y = type_of_target(y)
 if type_of_target_y != 'multilabel-indicator':
raise ValueError(
 'Supported target type is: multilabel-indicator.Got {!r} instead.'.format(type_of_target_y))
num_samples = y.shape[0]
rng = check_random_state(self.random_state)
 indices = np.arange(num_samples)
 if self.shuffle:
rng.shuffle(indices)
 y = y[indices]
r = np.asarray([1 / self.n_splits] * self.n_splits)
 test_folds = IterativeStratification(labels=y, r=r, random_state=rng)
 return test_folds[np.argsort(indices)]
def _iter_test_masks(self, X=None, y=None, groups=None):
test_folds = self._make_test_folds(X, y)
 for i in range(self.n_splits):
yield test_folds == i
def split(self, X, y, groups=None):

 y = check_array(y, ensure_2d=False, dtype=None)
 return super(MultilabelStratifiedKFold, self ).split(X, y, groups)
result=model(train,y,test )<save_to_csv>
def calculate_overall_lwlrap_sklearn(truth, scores):

 sample_weight = np.sum(truth > 0, axis=1)
 nonzero_weight_sample_indices = np.flatnonzero(sample_weight > 0)
 overall_lwlrap = sklearn.metrics.label_ranking_average_precision_score(
 truth[nonzero_weight_sample_indices, :] > 0,
scores[nonzero_weight_sample_indices, :],
sample_weight=sample_weight[nonzero_weight_sample_indices])
 return overall_lwlrap
res=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv',index_col=None)
 res['SalePrice']=np.floor(np.exp(result))
res.to_csv('Submission.csv',index=False )<load_from_csv>
GlobalMaxPooling2D, MaxPooling2D, concatenate, Activation, ZeroPadding2D, Dropout, add, ReLU)
 def cnn_6(lr, do=0, l2reg=0):
inp = Input(shape=(64,128,1))
x = BatchNormalization()(inp)
 x = ReLU()(x)
 x = Conv2D(filters=32, kernel_size=(5,5), strides=(1,1), kernel_regularizer=l2(l2reg), padding='same' )(x)
 x = MaxPooling2D(pool_size=(3,3), strides=(2,2), padding='same' )(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Conv2D(filters=32, kernel_size=(3,3), strides=(1,1), kernel_regularizer=l2(l2reg), padding='same' )(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Conv2D(filters=64, kernel_size=(3,3), strides=(2,2), kernel_regularizer=l2(l2reg), padding='same' )(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), kernel_regularizer=l2(l2reg), padding='same' )(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Conv2D(filters=128, kernel_size=(3,3), strides=(2,2), kernel_regularizer=l2(l2reg), padding='same' )(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), kernel_regularizer=l2(l2reg), padding='same' )(x)
 x = GlobalMaxPooling2D()(x)
 x = Dense(1024, activation='relu' )(x)
 x = Dropout(do )(x)
 out = Dense(80, activation='sigmoid' )(x)
 model = Model(inputs=inp, outputs=out)
 opt = Adam(lr=lr)
 model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=[])
 return model
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))<drop_column>
SAMPLE_DURATION = 3.0
SAMPLE_RATE = 44100
L = int(SAMPLE_RATE * SAMPLE_DURATION)
 def random_slice(samples):
begin = np.random.randint(0, len(samples)- L)
 return samples[begin : begin+L]
def pad(samples):
if len(samples)>= L:
return samples
else:
return np.pad(samples, pad_width=(L - len(samples), 0), mode='constant', constant_values=(0, 0))
training_data = []
progress = 0
for file_name in os.listdir('.. /input/freesound-audio-tagging-2019/train_curated/'):
file_data, _ = librosa.core.load('.. /input/freesound-audio-tagging-2019/train_curated/' + file_name, sr=SAMPLE_RATE)
 duration = librosa.core.get_duration(file_data, sr=SAMPLE_RATE)
 for i in range(math.ceil(duration/SAMPLE_DURATION)) :
begin = i*L
file_data_i = file_data[begin:begin+L]
if len(file_data_i)< L:
file_data_i = pad(file_data_i)
 file_array_i = librosa.feature.mfcc(file_data_i,
sr=SAMPLE_RATE,
n_fft=2560,
hop_length=347*int(SAMPLE_DURATION),
n_mels=128,
n_mfcc=64)
 training_data.append(( file_name+'_'+str(i), file_array_i, train_labels_dict[file_name]))
if progress%100 == 0:
print('processed', progress, 'files')
 progress += 1
print('train data done')
 random.Random(1001 ).shuffle(training_data)
 test = pd.read_csv(".. /input/freesound-audio-tagging-2019/sample_submission.csv")
 test_data = []
progress = 0
for file_name in test['fname'].tolist() :
file_data, _ = librosa.core.load('.. /input/freesound-audio-tagging-2019/test/' + file_name, sr=SAMPLE_RATE)
 duration = librosa.core.get_duration(file_data, sr=SAMPLE_RATE)
 for i in range(math.ceil(duration/SAMPLE_DURATION)) :
begin = i*L
file_data_i = file_data[begin:begin+L]
if len(file_data_i)< L:
file_data_i = pad(file_data_i)
 file_array_i = librosa.feature.mfcc(file_data_i,
sr=SAMPLE_RATE,
n_fft=2560,
hop_length=347*int(SAMPLE_DURATION),
n_mels=128,
n_mfcc=64)
 test_data.append(( file_name+'_'+str(i), file_array_i))
if progress%100 == 0:
print('processed', progress, 'files')
 progress += 1
print('test data done' )
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['Electrical'], axis=1, inplace=True )<feature_engineering>
def step_decay(epoch):
initial_lrate = 4e-4
drop = 0.5
epoch_cutoff = 7
epochs_drop = 3.0
if epoch<epoch_cutoff:
return initial_lrate
else:
return initial_lrate * math.pow(drop, math.floor(( 1+epoch-epoch_cutoff)/epochs_drop))
train_files = all_files
X_train, y_train = get_train(train_files)
 X_train = np.expand_dims(X_train, axis=3)
 filenames_test, X_test = get_test()
X_test = np.expand_dims(X_test, axis=3)
 model = cnn_6(lr=4e-4, do=0.5, l2reg=0)
 model.summary()
lrate = LearningRateScheduler(step_decay, verbose=1)
 model_history = model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, callbacks=[lrate])
 y_train_pred = model.predict(X_train, batch_size=128)
 train_lwlwrap = calculate_overall_lwlrap_sklearn(y_train, y_train_pred)
 print('train lwlwrap score:', train_lwlwrap)
 y_test_pred = model.predict(X_test, batch_size=128)
 filnames_test_parents, y_test_pred, _ = geometric_average_val_scores(filenames_test, y_test_pred, y_test_pred)
 print('training complete' )
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()<save_to_csv>
predictions = pd.DataFrame(data=y_test_pred)
 predictions.insert(loc=0, column='fname', value=filnames_test_parents)
 columns = ['fname']
columns.extend(mlb.classes_.tolist())
 predictions.columns = columns
predictions.to_csv('submission.csv', index=False )