Kaggle competition: TensorFlow Speech Recognition Challenge
<SOS> metric: CategorizationAccuracy Kaggle data source: tensorflow-speech-recognition-challenge<train_model>
def get_score(max_leaf_nodes, train_X, val_X, train_y, val_y):
model = DecisionTreeClassifier(max_leaf_nodes=max_leaf_nodes)
 model.fit(train_X, train_y)
 print("Max leaf nodes:", max_leaf_nodes," \t \t Score:", model.score(val_X,val_y))
return()
for max_leaf_nodes in [2,3,4,5,6,7,8,9]:
get_score(max_leaf_nodes, train_X, val_X, train_y, val_y )<load_from_csv>
!pip install pyunpack
!pip install patool
os.environ["CUDA_VISIBLE_DEVICES"]='0,1'
os.system('apt-get install p7zip')
 tf.random.set_seed(9)
test_data = pd.read_csv(".. /input/titanic/test.csv")
 X_test=pd.get_dummies(test_data[features])
 imputed_test=my_imputer.fit_transform(X_test )<train_model>
tf.__version__
final_tree = DecisionTreeClassifier(max_depth=4,min_impurity_decrease=.004)
 final_tree.fit(imputed_X,y )<save_to_csv>
device_lib.list_local_devices()
predictions=final_tree.predict(imputed_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
root_path = "/kaggle"
path = '/kaggle/input/titanic/train.csv'
train_data = pd.read_csv(path)
 train_data<load_from_csv>
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/'
path = '/kaggle/input/titanic/test.csv'
test_data = pd.read_csv(path)
 test_data<load_from_csv>
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 )
path = '/kaggle/input/titanic/gender_submission.csv'
gender_submission_data = pd.read_csv(path)
 gender_submission_data<concatenate>
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 )
df = pd.concat([train_data, test_data], ignore_index=True)
 df.reset_index(drop=True, inplace=True)
 df<import_modules>
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 )
<feature_engineering>
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]
def NaN_info(df):
global null_view
try:
null_view = df[[col for col in df.columns if df[col].isna().sum() > 0]].isna().sum().sort_values(ascending = True)
 null_view = pd.DataFrame(null_view, columns=['NANs'])
 null_view[['PERCENT']] = null_view.NANs.apply(lambda x: round(( x/len(df)) *100, 2))
null_view[['TYPE']] = df.dtypes
except:
return null_view
return null_view
NaN_info(df )<count_values>
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 )
df['Survived'].value_counts(dropna=False )<data_type_conversions>
start_random = random_shift(x)
 ipd.Audio(start_random , rate=sr )
df['Survived'].replace(to_replace=0, value='no', inplace=True)
 df['Survived'].replace(to_replace=1, value='yes', inplace=True)
 df['Survived'] = df['Survived'].astype('object')
 df['Survived'].value_counts(dropna=False )<predict_on_test>
higher_speed = random_speed_up(x)
 ipd.Audio(higher_speed , rate=sr )
def NaN_predict(df,
skip_features_with_missing_data_percentage = 90,
include_features_as_predictors_where_pec_miss_data_less = 10,
apply_fast_predict_where_missing_data_less_than_percent = 1 
):
if not sys.warnoptions:
warnings.simplefilter("ignore")
 global counter_all_predicted_values
counter_all_predicted_values = 0
global numeric_features
numeric_features = []
PARAMS = {'num_leaves': np.arange(50, 1000, step=50),
'max_depth': np.arange(10, 26, step=2),
'learning_rate': [0.001, 0.01, 0.1],
'n_estimators': np.arange(500, 2000, step=100),
'subsample': [0.1, 0.2, 0.3, 0.4, 0.5],
'feature_fraction': [0.1, 0.2, 0.3, 0.4, 0.5],
'bagging_fraction': [0.1, 0.2, 0.3, 0.4, 0.5],
'bagging_seed': np.arange(1, 20, step=1),
'lambda_l1': [0.1, 0.2, 0.3],
'lambda_l2': [0.1, 0.2, 0.3],
'min_child_samples': np.arange(5, 50, step=5),
'min_split_gain': [0.00001, 0.0001, 0.001]}
def NaN_info(df):
global null_view
try:
null_view = df[[col for col in df.columns if df[col].isna().sum() > 0]].isna().sum().sort_values(ascending = True)
 null_view = pd.DataFrame(null_view, columns=['NANs'])
 null_view[['PERCENT']] = null_view.NANs.apply(lambda x: round(( x/len(df)) *100, 2))
null_view[['TYPE']] = df.dtypes
except:
return null_view
return null_view
def encoding(work_predictors, df):
feature_power = 0.5
for j in work_predictors:
el_type = df[j].dtype
if el_type == 'object':
df[j].replace(np.nan, '0', inplace=True)
 labelencoder = LabelEncoder()
df.loc[:, j] = labelencoder.fit_transform(df.loc[:, j])
 else:
df[j] = df[j]**feature_power
return df, work_predictors
def imput_missing_value_to_main_df(df, miss_indeces, pred_miss, el):
counter = 0
for idx in miss_indeces:
df.loc[idx, el] = pred_miss[counter]
counter += 1
return df
def hyperparms_tuning_regressor(X_train, X_test, y_train, y_test, n_iter_for_RandomizedSearchCV, PARAMS):
global best_params
lgbm = lgb.LGBMRegressor(n_jobs = -1)
 lgbm_randomized_mse = RandomizedSearchCV(estimator=lgbm,
param_distributions=PARAMS,
n_iter=n_iter_for_RandomizedSearchCV,
scoring='neg_mean_squared_error',
cv=3,
verbose=0,
n_jobs = -1)
 lgbm_randomized_mse.fit(X_train, y_train)
 best_params = lgbm_randomized_mse.best_params_
print(best_params)
 pred_test_lgb = lgbm_randomized_mse.predict(X_test)
 MAE = mean_absolute_error(y_test,pred_test_lgb)
 y_te = list(round(y_test[:10], 1))
y_pred = list(np.round(pred_test_lgb[:10], 1))
print(f'first 10 y_test: {y_te}')
 print(f'first 10 y_pred: {y_pred}')
 print(f'mean_absolute_error: {MAE}')
 return best_params
def predict_regressor(best_params, X, y, miss_df):
global pred_miss
lgbm = lgb.LGBMRegressor(**best_params, n_jobs = -1)
 lgbm.fit(X, y)
 pred_miss = list(lgbm.predict(miss_df))
print('-------------------------------')
 print(f"The first 100 predicted missing values:
{pred_miss[:100]}")
 return pred_miss
def hyperparms_tuning_classifier(X_train, X_test, y_train, y_test, n_iter_for_RandomizedSearchCV, PARAMS):
global best_params
lgbm = lgb.LGBMClassifier(n_jobs = -1)
 lgbm_randomized_mse = RandomizedSearchCV(estimator=lgbm,
param_distributions=PARAMS,
n_iter=n_iter_for_RandomizedSearchCV,
scoring='f1_weighted',
cv=3,
verbose=0,
n_jobs = -1)
 lgbm_randomized_mse.fit(X_train, y_train)
 best_params = lgbm_randomized_mse.best_params_
print(best_params)
 pred_test_lgb = lgbm_randomized_mse.predict(X_test)
 accuracy = accuracy_score(y_test, pred_test_lgb)
 print(f'first 10 y_test: {y_test[:10]}')
 print(f'first 10 y_pred: {pred_test_lgb[:10]}')
 f1 = f1_score(y_test, pred_test_lgb, average='weighted')
 print(f'accuracy_score: {accuracy}')
 print(f'f1_score(weighted): {f1}')
 return best_params
def predict_classifier(best_params, X, y, miss_df):
global pred_miss
lgbm = lgb.LGBMClassifier(**best_params, n_jobs = -1)
 lgbm.fit(X, y)
 pred_miss = list(lgbm.predict(miss_df))
print('-------------------------------')
 print(f"The first 100 predicted missing values:
{pred_miss[:100]}")
 return pred_miss
def light_predict_regressor(X, y, miss_df):
global pred_miss
lgbm = lgb.LGBMRegressor()
lgbm.fit(X, y)
 pred_miss = list(lgbm.predict(miss_df))
print('-------------------------------')
 print(f"The first 100 predicted missing values:
{pred_miss[:100]}")
 return pred_miss
def light_predict_classifier(X, y, miss_df):
global pred_miss
lgbm = lgb.LGBMClassifier()
lgbm.fit(X, y)
 pred_miss = list(lgbm.predict(miss_df))
print('-------------------------------')
 print(f"The first 100 predicted missing values:
{pred_miss[:100]}")
 return pred_miss
def numeric_features(df):
num_features = [feature for feature in df.columns if df[feature].dtype in ['int64', 'float64']]
return num_features
def integer_features(df):
global int_features
int_features = []
int_features = [feature for feature in df.columns if df[feature].dtype in ['int64']]
return int_features
print(NaN_info(df))
print('
')
 all_features = list(df.columns)
 df_indeces = list(df.index)
 integer_features(df)
 delete_miss_features = list(( null_view.loc[null_view['PERCENT'] > skip_features_with_missing_data_percentage] ).index)
 print(f'Exclude from the prediction, because missing data more than \
{skip_features_with_missing_data_percentage}% :
{delete_miss_features}')
 print('')
 all_miss_features = list(null_view.index)
 for delete_feature in delete_miss_features:
all_miss_features.remove(delete_feature)
 for el in all_miss_features:
print('
')
 NaN_info(df)
 lot_of_miss_features = list(( null_view.loc[null_view['PERCENT'] > include_features_as_predictors_where_pec_miss_data_less] ).index)
 now_predictors = list(set(all_features)-set(lot_of_miss_features))
work_predictors = list(set(now_predictors)- set([el]))
miss_indeces = list(( df[pd.isnull(df[el])] ).index)
 miss_df = df.iloc[miss_indeces][:]
miss_df = miss_df[work_predictors]
encoding(work_predictors, df=miss_df)
 work_indeces = list(set(df_indeces)- set(miss_indeces))
work_df = df.iloc[work_indeces][:]
encoding(work_predictors, df=work_df)
 X = work_df[work_predictors]
y = work_df[el]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
 feature_type = df[el].dtypes
percent = null_view['PERCENT'][el]
print(f'feature: {el}, type: {feature_type}, missing values: {percent}%')
 print(f'X.shape: {X.shape}, y.shape: {y.shape}')
 print(f'Unused for prediction, because missing data more than \
{include_features_as_predictors_where_pec_miss_data_less}% :')
 print(lot_of_miss_features)
 print('')
 if percent < apply_fast_predict_where_missing_data_less_than_percent:
print('light prediction without tuning hyperparameters, because missing data less than 1 %')
 if feature_type == 'object' or feature_type == 'bool':
print('FAST_classifier:')
 light_predict_classifier(X, y, miss_df)
 counter_all_predicted_values += len(miss_indeces)
 imput_missing_value_to_main_df(df, miss_indeces, pred_miss, el)
 print(f'miss_indexes:
{miss_indeces}')
 elif feature_type == 'float64' or feature_type == 'int64':
print('FAST_regressor:')
 light_predict_regressor(X, y, miss_df)
 counter_all_predicted_values += len(miss_indeces)
 imput_missing_value_to_main_df(df, miss_indeces, pred_miss, el)
 print(f'miss_indexes:
{miss_indeces}')
 else:
print(f"unprocessed feature: {el} - {feature_type} type")
 else:
n_iter_for_RandomizedSearchCV = int(300 + percent * 5)
 print(f'Iteration for RandomizedSearchCV: {n_iter_for_RandomizedSearchCV}')
 if feature_type == 'object' or feature_type == 'bool':
print('ADVANCED classifier evaluation:')
 labelencoder = LabelEncoder()
y_train = labelencoder.fit_transform(y_train)
 y_test = labelencoder.fit_transform(y_test)
 hyperparms_tuning_classifier(X_train, X_test, y_train, y_test, n_iter_for_RandomizedSearchCV, PARAMS)
 predict_classifier(best_params, X, y, miss_df)
 counter_all_predicted_values += len(miss_indeces)
 imput_missing_value_to_main_df(df, miss_indeces, pred_miss, el)
 elif feature_type == 'float64' or feature_type == 'int64':
print('ADVANCED regressor evaluation:')
 hyperparms_tuning_regressor(X_train, X_test, y_train, y_test, n_iter_for_RandomizedSearchCV, PARAMS)
 print(f'mean for {el}: {df[el].mean() }')
 predict_regressor(best_params, X, y, miss_df)
 counter_all_predicted_values += len(miss_indeces)
 imput_missing_value_to_main_df(df, miss_indeces, pred_miss, el)
 else:
print(f"unprocessed feature: {el} - {feature_type} type")
 for feature in int_features:
df[[feature]] = df[[feature]].astype('int64')
 print('
')
 print(f'These features have not been processed, because missing data more than {skip_features_with_missing_data_percentage}%')
 print(NaN_info(df))
print('
')
 print(f'{counter_all_predicted_values} values have been predicted and replaced')
 print('
')
 return df<predict_on_test>
pitch_changed = random_change_pitch(x)
 ipd.Audio(pitch_changed, rate=sr )
NaN_predict(df,
skip_features_with_missing_data_percentage = 70 )<define_variables>
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 )
target_column = ['Survived']
predictors = list(set(list(df.columns)) -set(target_column))
predictors<data_type_conversions>
test1 = np.random.randint(10, 100, 10)
 validation_set[test1],[inv_map[int(i)] for i in validation_labels[test1]]
df['Age'] = df['Age'].astype('int64')
 df['Fare'] = df['Fare'].astype('int64' )<count_values>
out2.join(out, lsuffix='_left', rsuffix='_right')[:5]
for el in predictors:
print(f'======================= {el} =======================')
 print(df[el].value_counts(dropna=False))<data_type_conversions>
np.allclose(out.iloc[:,0].values, out2.iloc[:,0].values, atol=0.01 )
df['AgeCategory'] = df['Age']/5
df['AgeCategory'] = df['AgeCategory'].astype('int64')
 df['AgeCategory']<define_variables>
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()
indices_1 = list(np.where([df['Ticket'].str.contains(r'^\w.+\s', regex=True)])[1])
 indices_2 = list(np.where([df['Ticket'].str.contains(r'^\w.\s', regex=True)])[1])
 indices_3 = list(np.where([df['Ticket'].str.contains(r'^\w\s', regex=True)])[1])
 indices = list(set(indices_1 + indices_2 + indices_3))
print(len(indices))
print(indices )<filter>
for el in indices[:20]:
ticket = df.loc[el, 'Ticket']
print(ticket )<feature_engineering>
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 )
for el in indices:
ticket = df.loc[el, 'Ticket']
ticket = ticket.split(sep=' ', maxsplit=2)
 if len(ticket)== 3:
ticket[0] = ticket[0] + ticket[1]
ticket[1] = ticket[2]
del ticket[2]
print(f'{ticket[0]} {ticket[1]}')
 df.loc[el, 'Ticket'] = ticket[1]
df.loc[el, 'TicketSeries'] = ticket[0] 
<data_type_conversions>
shutil.rmtree(train_path)
 gc.collect()
df['TicketSeries'].replace(np.nan, 'without', inplace=True)
 df['Ticket'] = df['Ticket'].astype('int64' )<count_values>
if not os.path.exists(root_path + '/working/test/'):
os.makedirs(root_path + '/working/test/')
 Archive(root_path + '/input/tensorflow-speech-recognition-challenge/test.7z' ).extractall(root_path + '/working')
 test_path = root_path + '/working/test'
values = df['Ticket'].value_counts()
print(values[:40] )<filter>
test_data,test_labels,_ = get_image_list(test_path)
 test_data = test_data[0]
test_labels = test_labels[0]
test_datagen = data_generator(test_data, test_labels, batch_size, None)
 test_size = len(test_data)
 test_steps = np.ceil(test_size /(batch_size))
y_pred = model.predict_generator(test_datagen, steps = test_steps, verbose=1 )
LINE = df.loc[df['Ticket'] == 'LINE']
indeses = list(LINE.index)
 indeses<feature_engineering>
y_labs = np.argmax(y_pred, axis=1 )
for el in indeses:
print(el, 'LINE')
 df.loc[el, 'Ticket'] = 1000
df.loc[el, 'TicketSeries'] = 'LINE'<data_type_conversions>
inv_map = inv_map = {v: k for k, v in classes_map.items() }
df['TicketSeries'].replace(np.nan, 'without', inplace=True)
 df['Ticket'] = df['Ticket'].astype('int64' )<feature_engineering>
train_audio_sample = test_data[16]
x,sr = librosa.load(train_audio_sample, sr = 16000)
 ipd.Audio(x, rate=sr )
indexes = list(df.index)
 lastName = []
for el in indexes:
last_name = df.loc[el, 'Name']
last_name = last_name.split(sep=',', maxsplit=1)
 last_name = last_name[0]
lastName.append(last_name)
 df.loc[el, 'LastName'] = last_name
lastName = list(set(lastName))
print(f'Total lastName: {len(lastName)}')
 print('')
 print(f'{lastName[:10]}...' )<count_values>
train_audio_sample = test_data[100]
x,sr = librosa.load(train_audio_sample, sr = 16000)
 ipd.Audio(x, rate=sr )
LastName = df['LastName'].value_counts()
LastName[:20]<count_values>
train_audio_sample = test_data[1001]
x,sr = librosa.load(train_audio_sample, sr = 16000)
 ipd.Audio(x, rate=sr )
b = df['Cabin'].value_counts()
b[:10]<feature_engineering>
inv_map[y_labs[1001]]
indexes = list(df.index)
 for el in indexes:
Cabin = df.loc[el, 'Cabin']
level = Cabin[0]
df.loc[el, 'CabinLevel'] = level<count_values>
shutil.rmtree(test_path )
<define_variables><EOS> 
my_submission = pd.DataFrame({'fname': test_data, 'label': [inv_map[x] for x in y_labs]})
 my_submission.to_csv('submission.csv', index=False )