text_plain_output_1.png
from itertools import product

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = pd.read_csv('/kaggle/input/tmnist-typeface-mnist/TMNIST_data.csv')

unique_typefaces = df.names.unique()
typeface_map = {}
for i in range(len(unique_typefaces)):
    typeface_map[unique_typefaces[i]] = i
df = df.replace({'names': typeface_map})

df = df.sample(frac=1).reset_index(drop=True)
training_percent = 0.7
validation_percent = 0.2
total_examples = df.shape[0]
train_val_breakpoint = int(training_percent * total_examples)
val_test_breakpoint = int((training_percent + validation_percent) * total_examples)
train_df = df.iloc[None:train_val_breakpoint, :]
val_df = df.iloc[train_val_breakpoint:val_test_breakpoint, :]
test_df = df.iloc[val_test_breakpoint:None, :]

typefaces_num = len(typeface_map)
to_ndarr = lambda obj: np.array(list(obj))
for df_name in ('train_df', 'val_df', 'test_df'):
    df = eval(df_name)
    diff = np.setdiff1d(to_ndarr(typeface_map.values()), to_ndarr(df.names))
    ndiff = len(diff)

def dataframeToNumpy(df):
    Xdf = df.iloc[:, 2:None]
    ydf = df.iloc[:, 1]
    zdf = df.iloc[:, 0]
    return (np.array(Xdf), np.array(ydf), np.array(zdf))
Xtrain, ytrain, ztrain = dataframeToNumpy(train_df)
Xval, yval, zval = dataframeToNumpy(val_df)
Xtest, ytest, ztest = dataframeToNumpy(test_df)
names = []
for p in product(['train', 'val', 'test'], ['X', 'y', 'z']):
    names.append(''.join([p[1], p[0]]))
for name in names:
    print(f'Shape of {name}:', eval(name).shape)