Kaggle competition: Digit Recognizer
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_type_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'type_code'], how='left')
 matrix['date_shop_type_avg_item_cnt'] = matrix['date_shop_type_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_type_avg_item_cnt')
 matrix.drop(['date_shop_type_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
data_dir='/kaggle/input/digit-recognizer/'
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix['date_shop_subtype_avg_item_cnt'] = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_subtype_avg_item_cnt')
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
train=pd.read_csv(data_dir+'train.csv')
 test=pd.read_csv(data_dir+'test.csv' )
ts = time.time()
group = matrix.groupby(['date_block_num', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'city_code'], how='left')
 matrix['date_city_avg_item_cnt'] = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_city_avg_item_cnt')
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
train['label'].value_counts()
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'city_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'city_code'], how='left')
 matrix['date_item_city_avg_item_cnt'] = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_item_city_avg_item_cnt')
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
train.isnull().sum()
ts = time.time()
group = matrix.groupby(['date_block_num', 'type_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_type_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'type_code'], how='left')
 matrix['date_type_avg_item_cnt'] = matrix['date_type_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_type_avg_item_cnt')
 matrix.drop(['date_type_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<merge>
train.isnull().sum()
ts = time.time()
group = matrix.groupby(['date_block_num', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_subtype_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'subtype_code'], how='left')
 matrix['date_subtype_avg_item_cnt'] = matrix['date_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_subtype_avg_item_cnt')
 matrix.drop(['date_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<data_type_conversions>
features=[i for i in train.columns]
k=0
for feature in features:
if train[feature].isnull().sum() ==0:
k=k+1
else:
print('{0} has {1} null values'.format(feature,train[feature].isnull().sum()))
if(k==train.shape[1]):
print('no nan's in the train dataset,so proceed')
ts = time.time()
group = train.groupby(['item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['item_id'], how='left')
 matrix['item_avg_item_price'] = matrix['item_avg_item_price'].astype(np.float16)
 group = train.groupby(['date_block_num','item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['date_item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_price'] = matrix['date_item_avg_item_price'].astype(np.float16)
 lags = [1,2,3,4,5,6]
matrix = lag_feature(matrix, lags, 'date_item_avg_item_price')
 for i in lags:
matrix['delta_price_lag_'+str(i)] = \ 
(matrix['date_item_avg_item_price_lag_'+str(i)] - matrix['item_avg_item_price'])/ matrix['item_avg_item_price']
def select_trend(row):
for i in lags:
if row['delta_price_lag_'+str(i)]:
return row['delta_price_lag_'+str(i)]
return 0
matrix['delta_price_lag'] = matrix.apply(select_trend, axis=1)
 matrix['delta_price_lag'] = matrix['delta_price_lag'].astype(np.float16)
 matrix['delta_price_lag'].fillna(0, inplace=True)
 fetures_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
fetures_to_drop += ['date_item_avg_item_price_lag_'+str(i)]
fetures_to_drop += ['delta_price_lag_'+str(i)]
matrix.drop(fetures_to_drop, axis=1, inplace=True)
 time.time() - ts<merge>
features=[i for i in test.columns]
k=0
for feature in features:
if test[feature].isnull().sum() ==0:
k=k+1
else:
print('{0} has {1} null values'.format(feature,test[feature].isnull().sum()))
if(k==test.shape[1]):
print('no nan's in the test dataset,so proceed' )
ts = time.time()
group = train.groupby(['date_block_num','shop_id'] ).agg({'revenue': ['sum']})
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(['shop_id'] ).agg({'date_shop_revenue': ['mean']})
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['shop_id'], how='left')
 matrix['shop_avg_revenue'] = matrix['shop_avg_revenue'].astype(np.float32)
 matrix['delta_revenue'] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix['delta_revenue'] = matrix['delta_revenue'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'delta_revenue')
 matrix.drop(['date_shop_revenue','shop_avg_revenue','delta_revenue'], axis=1, inplace=True)
 time.time() - ts<feature_engineering>
train_y=train['label']
train_x=train.drop('label',axis=1 )
matrix['month'] = matrix['date_block_num'] % 12<categorify>
def image_printer(i,train_x):
idx=i
grid_data=train_x.iloc[idx].to_numpy().reshape(28,28 ).astype('uint8')
 plt.imshow(grid_data)
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8 )<data_type_conversions>
label.value_counts()
ts = time.time()
cache = {}
matrix['item_shop_last_sale'] = -1
matrix['item_shop_last_sale'] = matrix['item_shop_last_sale'].astype(np.int8)
 for idx, row in matrix.iterrows() :
key = str(row.item_id)+' '+str(row.shop_id)
 if key not in cache:
if row.item_cnt_month!=0:
cache[key] = row.date_block_num
else:
last_date_block_num = cache[key]
matrix.at[idx, 'item_shop_last_sale'] = row.date_block_num - last_date_block_num
cache[key] = row.date_block_num
time.time() - ts<data_type_conversions>
std_data=StandardScaler().fit_transform(data)
 print(np.mean(std_data))
print(np.std(std_data))
ts = time.time()
cache = {}
matrix['item_last_sale'] = -1
matrix['item_last_sale'] = matrix['item_last_sale'].astype(np.int8)
 for idx, row in matrix.iterrows() :
key = row.item_id
if key not in cache:
if row.item_cnt_month!=0:
cache[key] = row.date_block_num
else:
last_date_block_num = cache[key]
if row.date_block_num>last_date_block_num:
matrix.at[idx, 'item_last_sale'] = row.date_block_num - last_date_block_num
cache[key] = row.date_block_num
time.time() - ts<feature_engineering>
temp_data=std_data
covar_matrix=np.matmul(temp_data.T,temp_data)
 print(f'shape of covar matrix is {covar_matrix.shape}')
 print(f'shape of my data is {temp_data.shape}' )
ts = time.time()
matrix['item_shop_first_sale'] = matrix['date_block_num'] - matrix.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 matrix['item_first_sale'] = matrix['date_block_num'] - matrix.groupby('item_id')['date_block_num'].transform('min')
 time.time() - ts<filter>
eigh_values,eigh_vectors=eigh(covar_matrix,eigvals=(782,783))
print(f'shape of eigen vectors {eigh_vectors.shape}' )
ts = time.time()
matrix = matrix[matrix.date_block_num > 11]
time.time() - ts<correct_missing_values>
pca_points=np.matmul(temp_data,eigh_vectors)
 print(f'shape of my pca points is {pca_points.shape}' )
ts = time.time()
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
matrix = fill_na(matrix)
 time.time() - ts<load_pretrained>
pca_data=np.vstack(( pca_points.T,label)).T
print(f'shape of my pca data is {pca_data.shape}')
 pca_dataframe=pd.DataFrame(data=pca_data,columns=('1st Principal comp','2nd Principal comp','label'))
print(pca_dataframe.head(10))
matrix.to_pickle('data.pkl')
 del matrix
del cache
del group
del items
del shops
del cats
del train
gc.collect() ;<load_pretrained>
pca=decomposition.PCA()
data = pd.read_pickle('data.pkl' )<drop_column>
from sklearn.manifold import TSNE
data = data[[
'date_block_num',
'shop_id',
'item_id',
'item_cnt_month',
'city_code',
'item_category_id',
'type_code',
'subtype_code',
'item_cnt_month_lag_1',
'item_cnt_month_lag_2',
'item_cnt_month_lag_3',
'item_cnt_month_lag_6',
'item_cnt_month_lag_12',
'date_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_2',
'date_item_avg_item_cnt_lag_3',
'date_item_avg_item_cnt_lag_6',
'date_item_avg_item_cnt_lag_12',
'date_shop_avg_item_cnt_lag_1',
'date_shop_avg_item_cnt_lag_2',
'date_shop_avg_item_cnt_lag_3',
'date_shop_avg_item_cnt_lag_6',
'date_shop_avg_item_cnt_lag_12',
'date_cat_avg_item_cnt_lag_1',
'date_shop_cat_avg_item_cnt_lag_1',
'date_city_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1',
'delta_price_lag',
'month',
'days',
'item_shop_last_sale',
'item_last_sale',
'item_shop_first_sale',
'item_first_sale',
]]<prepare_x_and_y>
temp_data1=std_data
pca.n_components=450
pca_data=pca.fit_transform(temp_data1)
 pca_data_new=np.vstack(( pca_data1.T,label)).T
new_data=pca_data
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<set_options>
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import cross_val_score,cross_val_predict
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
from scipy.stats import uniform,truncnorm,randint
del data
gc.collect() ;<train_model>
clf=LogisticRegression(multi_class='multinomial',n_jobs=-1)
 cv_scores=cross_val_score(clf,new_data,label,cv=10)
 print(f'we can expect accuracy between {cv_scores.min() } and {cv_scores.max() } with avg accuracy of {cv_scores.mean() }' )
ts = time.time()
model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 time.time() - ts<save_to_csv>
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accLR=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accLR}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))
pickle.dump(Y_test, open('xgb_test.pickle', 'wb'))<define_variables>
for kernel in('poly','rbf'):
clf=svm.SVC(kernel=kernel)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of {kernel} kernel varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }')
<set_options>
for C in(1,10,100):
clf=svm.SVC(kernel='rbf',C=C)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of rbf kernel with C={C} varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }')
!pip install pyunpack
!pip install patool
os.environ["CUDA_VISIBLE_DEVICES"]='0,1'
os.system('apt-get install p7zip')
 tf.random.set_seed(9)
<import_modules>
for gamma in(0.001,0.01):
clf=svm.SVC(kernel='rbf',C=10,gamma=gamma)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of rbf kernel with C=10 and gamma={gamma} varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }')
tf.__version__<set_options>
clf=svm.SVC(kernel='rbf',C=10,gamma=0.001)
 cv_scores=cross_val_score(clf,new_data,label,n_jobs=-1,cv=10)
 print(f'accuracy of rbf kernel with C=10 and gamma= 0.001 varies between {cv_scores.min() } and {cv_scores.max() } with mean {cv_scores.mean() }' )
device_lib.list_local_devices()<define_variables>
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accSVM=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accSVM}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))
root_path = "/kaggle"<load_pretrained>
clf=KNeighborsClassifier(n_neighbors=5,n_jobs=-1)
 cv_score=cross_val_score(clf,new_data,label,cv=10)
 print(f' we can expect accuracy bwetween {cv_score.min() } and {cv_score.max() } with a mean of {cv_score.mean() }')
if not os.path.exists(root_path + '/working/train/'):
os.makedirs(root_path + '/working/train/')
 Archive(root_path + '/input/tensorflow-speech-recognition-challenge/train.7z' ).extractall(root_path + '/working')
 train_path = root_path + '/working/train/audio/'<load_pretrained>
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accKNN=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accKNN}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))
train_audio_sample = os.path.join(train_path, "yes/0a7c2a8d_nohash_0.wav")
 x,sr = librosa.load(train_audio_sample, sr = 16000)
 ipd.Audio(x, rate=sr )<normalization>
clf=DecisionTreeClassifier(max_depth=best_n,random_state=0)
 cv_score=cross_val_score(clf,new_data,label,cv=10,scoring='accuracy',n_jobs=-1)
 print(f' with {best_n} as depth of trees we can expect accuracy bwetween {cv_score.min() } and {cv_score.max() } with a mean of {cv_score.mean() }' )
mfccs = librosa.feature.mfcc(x, sr=sr, n_mfcc=40)
 scaler = StandardScaler()
ee= scaler.fit_transform(mfccs.T)
 plt.figure(figsize=(14, 5))
librosa.display.specshow(ee.T )<categorify>
clf=DecisionTreeClassifier(max_depth=best_n,random_state=0)
 clf.fit(new_data,label )
def pad_audio(samples, L):
if len(samples)>= L:
return samples
else:
return np.pad(samples, pad_width=(L - len(samples), 0), mode='constant', constant_values=(0, 0))
def chop_audio(samples, L=16000):
while True:
beg = np.random.randint(0, len(samples)- L)
 yield samples[beg: beg + L]
def choose_background_generator(sound, backgrounds, max_alpha = 0.7):
if backgrounds is None:
return sound
my_gen = backgrounds[np.random.randint(len(backgrounds)) ]
background = next(my_gen)* np.random.uniform(0, max_alpha)
 augmented_data = sound + background
augmented_data = augmented_data.astype(type(sound[0]))
return augmented_data
def random_shift(sound, shift_max = 0.2, sampling_rate = 16000):
shift = np.random.randint(sampling_rate * shift_max)
 out = np.roll(sound, shift)
 if shift > 0:
out[:shift] = 0
else:
out[shift:] = 0
return out
def random_change_pitch(x, sr=16000):
pitch_factor = np.random.randint(1, 4)
 out = librosa.effects.pitch_shift(x, sr, pitch_factor)
 return out
def random_speed_up(x):
where = ["start", "end"][np.random.randint(0, 1)]
speed_factor = np.random.uniform(0, 0.5)
 up = librosa.effects.time_stretch(x, 1 + speed_factor)
 up_len = up.shape[0]
if where == "end":
up = np.concatenate(( up, np.zeros(( x.shape[0] - up_len,))))
else:
up = np.concatenate(( np.zeros(( x.shape[0] - up_len,)) , up))
return up
def get_image_list(train_audio_path):
classes = os.listdir(train_audio_path)
 classes = [thisclass for thisclass in classes if thisclass != '_background_noise_']
index = [i for i,j in enumerate(classes)]
outlist = []
labels = []
for thisindex,thisclass in zip(index, classes):
filelist = [f for f in os.listdir(os.path.join(train_audio_path, thisclass)) if f.endswith('.wav')]
filelist = [os.path.join(train_audio_path, thisclass, x)for x in filelist]
outlist.append(filelist)
 labels.append(np.full(len(filelist), fill_value= thisindex))
return outlist,labels,dict(zip(classes,index))
def split_train_test_stratified_shuffle(images_list, labels, train_size = 0.9):
classes_size = [len(x)for x in images_list]
classes_vector = [np.arange(x)for x in classes_size]
total = np.sum(classes_size)
 total_train = [int(train_size * total * x)for x in classes_size / total]
train_index = [np.random.choice(x,y,replace=False)for x,y in zip(classes_size,total_train)]
validation_index = [np.setdiff1d(i,j)for i,j in zip(classes_vector,train_index)]
train_set = [np.array(x)[idx] for x,idx in zip(images_list,train_index)]
validation_set = [np.array(x)[idx] for x,idx in zip(images_list,validation_index)]
train_labels = [np.array(x)[idx] for x,idx in zip(labels,train_index)]
validation_labels = [np.array(x)[idx] for x,idx in zip(labels,validation_index)]
train_set = np.array([element for array in train_set for element in array])
 validation_set = np.array([element for array in validation_set for element in array])
 train_labels = np.array([element for array in train_labels for element in array])
 validation_labels = np.array([element for array in validation_labels for element in array])
 train_shuffle = np.random.permutation(len(train_set))
validation_shuffle = np.random.permutation(len(validation_set))
train_set = train_set[train_shuffle]
validation_set = validation_set[validation_shuffle]
train_labels = train_labels[train_shuffle]
validation_labels = validation_labels[validation_shuffle]
return train_set,train_labels,validation_set,validation_labels
def preprocess_data(file, background_generator, target_sr = 16000, n_mfcc = 40, threshold = 0.7):
x,sr = librosa.load(file, sr = target_sr)
 x = pad_audio(x, sr)
 if np.random.uniform(0, 1)> threshold:
x = choose_background_generator(x, background_generator)
 if np.random.uniform(0, 1)> threshold:
x = random_shift(x)
 if np.random.uniform(0, 1)> threshold:
x = random_change_pitch(x)
 if np.random.uniform(0, 1)> threshold:
x = random_speed_up(x)
 mfccs = librosa.feature.mfcc(x, sr=sr, n_mfcc=n_mfcc)
 mfccs = np.moveaxis(mfccs, 1, 0)
 scaler = StandardScaler()
mfccs_scaled = scaler.fit_transform(mfccs)
 return mfccs_scaled.reshape(mfccs_scaled.shape[0], mfccs_scaled.shape[1], 1)
 class data_generator(Sequence):
def __init__(self, x_set, y_set, batch_size, background_generator):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
self.background_generator = background_generator
def __len__(self):
return math.ceil(len(self.x)/ self.batch_size)
 def __getitem__(self, idx):
idx_from = idx * self.batch_size
idx_to =(idx + 1)* self.batch_size
batch_x = self.x[idx_from:idx_to]
batch_y = self.y[idx_from:idx_to]
x = [preprocess_data(elem, self.background_generator)for elem in batch_x]
y = batch_y
return np.array(x), np.array(y)
 def build_model(n_classes, input_shape):
model_input = keras.Input(shape=input_shape)
 img_1 = layers.Convolution2D(filters = 32, kernel_size =(3,3), padding = "same", activation=activations.relu )(model_input)
 img_1 = layers.MaxPooling2D(pool_size=(2, 2))(img_1)
 img_1 = layers.Convolution2D(filters = 64, kernel_size =(3,3), padding = "same", activation=activations.relu )(img_1)
 img_1 = layers.MaxPooling2D(pool_size=(2, 2))(img_1)
 img_1 = layers.Convolution2D(filters = 128, kernel_size =(3,3), padding = "same", activation=activations.relu )(img_1)
 img_1 = layers.MaxPooling2D(pool_size=(2, 2))(img_1)
 img_1 = layers.Convolution2D(filters = 256, kernel_size =(3,3), padding = "same", activation=activations.relu )(img_1)
 img_1 = layers.MaxPooling2D(pool_size=(2, 2))(img_1)
 img_1 = layers.Dropout(rate=0.25 )(img_1)
 img_1 = layers.Flatten()(img_1)
 img_1 = layers.Dense(128, activation=activations.relu )(img_1)
 img_1 = layers.Dropout(rate=0.5 )(img_1)
 model_output = layers.Dense(n_classes, activation=activations.softmax )(img_1)
 model = keras.Model(model_input, model_output)
 return model
def multiclass_roc(y_test, y_pred, average="macro"):
lb = LabelBinarizer()
lb.fit(y_test)
 y_test = lb.transform(y_test)
 y_pred = lb.transform(y_pred)
 all_labels = np.unique(y_test)
 for(idx, c_label)in enumerate(all_labels):
fpr, tpr, thresholds = roc_curve(y_test[:,idx].astype(int), y_pred[:,idx])
 c_ax.plot(fpr, tpr, label = '%s(AUC:%0.2f)' %(c_label, auc(fpr, tpr)))
 c_ax.plot(fpr, fpr, 'b-', label = 'Random Guessing')
 return roc_auc_score(y_test, y_pred, average=average )<load_pretrained>
cn=['0','1','2','3','4','5','6','7','8','9']
dot_data = tree.export_graphviz(clf, out_file=None,
class_names=cn,
filled=True)
 graph = graphviz.Source(dot_data,format="png")
 graph
wavfiles = glob.glob(os.path.join(train_path, "_background_noise_/*wav"))
wavfiles = [librosa.load(elem, sr = 16000)[0] for elem in wavfiles]
background_generator = [chop_audio(x)for x in wavfiles]<split>
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accDT=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accDT}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))
images_list,labels,classes_map = get_image_list(train_path)
 train_set,train_labels,validation_set,validation_labels = split_train_test_stratified_shuffle(images_list,labels)
 train_datagen = data_generator(train_set, train_labels, 40, background_generator)
 validation_datagen = data_generator(validation_set, validation_labels,40, None )<normalization>
clf=RandomForestClassifier(n_estimators=600,max_depth=best_n,random_state=0,n_jobs=-1)
 cv_score=cross_val_score(clf,new_data,label,cv=10,scoring='accuracy',n_jobs=-1)
 print(f' with {best_n} as depth of trees and {600} as no of trees we can expect accuracy bwetween {cv_score.min() } and {cv_score.max() } with a mean of {cv_score.mean() }' )
start_random = random_shift(x)
 ipd.Audio(start_random , rate=sr )<normalization>
class_pred=cross_val_predict(clf,new_data,label,cv=10)
 accRF=accuracy_score(label,class_pred)
 print(f'accuracy of our model is {accRF}')
 print(f' confusion matrix is ')
 print(confusion_matrix(label,class_pred))
print(f'classification report is ')
 print(classification_report(label,class_pred))
higher_speed = random_speed_up(x)
 ipd.Audio(higher_speed , rate=sr )<normalization>
import tensorflow as tf
import keras
from keras import backend as k
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation, BatchNormalization
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
from keras.callbacks import EarlyStopping
pitch_changed = random_change_pitch(x)
 ipd.Audio(pitch_changed, rate=sr )<define_variables>
test_x=test
inv_map = {v: k for k, v in classes_map.items() }
any_present=[i in validation_set for i in train_set]
np.any(any_present )<count_unique_values>
img_cols=28
img_rows=28
test1 = np.random.randint(10, 100, 10)
 validation_set[test1],[inv_map[int(i)] for i in validation_labels[test1]]<merge>
if k.image_data_format=='channels_first':
train_x = train_x.values.reshape(train_x.shape[0], 1,img_cols,img_rows)
 test = test.values.reshape(test.shape[0], 1,img_cols,img_rows)
 train_x=train_x/255.0
test=test/255.0
input_shape =(1,img_cols,img_rows)
 else:
train_x=train_x.values.reshape(train_x.shape[0],img_cols,img_rows,1)
 test=test.values.reshape(test.shape[0],img_cols,img_rows,1)
 train_x=train_x/255.0
test=test/255.0
input_shape =(img_cols,img_rows,1 )
out2.join(out, lsuffix='_left', rsuffix='_right')[:5]<compute_test_metric>
earlystopping = EarlyStopping(monitor ="val_accuracy",
mode = 'auto', patience = 10,
restore_best_weights = True)
 modelacc = []
nfilters = [32, 64, 128,256]
conv_layers = [1, 2, 3,4]
dense_layers = [0, 1, 2,3]
dp=0.5
for filters in nfilters:
for conv_layer in conv_layers:
for dense_layer in dense_layers:
cnnsays = 'No of filters/feature maps: {} conv_layers: {} dense_layers: {} dropout: {}'.format(filters, conv_layer, dense_layer, dp)
 print(cnnsays)
 model = Sequential()
model.add(Conv2D(filters,(3, 3), input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(dp))
for i in range(conv_layer-1):
model.add(Conv2D(filters,(3, 3),padding="same"))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(dp))
model.add(Flatten())
 for i in range(dense_layer):
model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(dp))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 EPOCHS = 30
history = model.fit(train_x, train_y, epochs=EPOCHS, batch_size=32, validation_split=0.2, callbacks=[earlystopping])
 modelacc.append([round(100*max(history.history['val_accuracy']), 2), cnnsays])
np.allclose(out.iloc[:,0].values, out2.iloc[:,0].values, atol=0.01 )<define_variables>
print("Highest Validation Accuracy so far : {}%".format(round(100*max(history.history['val_accuracy']), 2)) )
rows = 32
columns = 40
batch_size = 100
epochs = 50
base_path = root_path + "/working/models"
if not os.path.exists(base_path):
os.makedirs(base_path)
 train_size = train_set.shape[0]
validation_size = validation_set.shape[0]
steps_per_epoch = train_size//batch_size
lr = 1e-3
tensorboard_dir=base_path + "/logs"
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=tensorboard_dir)
 checkpoint_filepath = os.path.join(base_path, 'cp-{epoch:04d}.ckpt')
 checkpoint_callback = keras.callbacks.ModelCheckpoint(
 filepath= checkpoint_filepath,
save_best_only=True,
save_weights_only=True,
monitor='val_acc',
mode='max',
verbose=1)
 reduce_lr_callback = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=3, min_lr=1e-5, vebose=1)
 earlystop_callback = keras.callbacks.EarlyStopping(
 monitor="val_loss",
min_delta=1e-3,
patience=5,
verbose=1)
 optimizer = keras.optimizers.Adam(learning_rate = lr)
 loss_fn = keras.losses.SparseCategoricalCrossentropy()
acc_metric = keras.metrics.SparseCategoricalAccuracy()
model = build_model(len(classes_map),(rows, columns, 1))
model.compile(optimizer = optimizer, loss = loss_fn, metrics= [acc_metric])
 model.summary()<define_variables>
modelacc.sort(reverse=True)
 modelacc
<train_model>
pred=model.predict([test])
 soln=[]
for i in range(len(pred)) :
soln.append(np.argmax(pred[i]))
history = model.fit(train_datagen,
steps_per_epoch= steps_per_epoch,
epochs = epochs,
validation_data = validation_datagen,
validation_steps = validation_size//batch_size,
callbacks=[earlystop_callback, reduce_lr_callback, checkpoint_callback, tensorboard_callback],
use_multiprocessing=True )<set_options>
final = pd.DataFrame()
final['ImageId']=[i+1 for i in test_x.index]
final['Label']=soln
final.to_csv('mnistcnn.csv', index=False )