Accuracy with 938 al leat here the code: import numpy as np import matplotlib.pyplot as plt def to categorical (y, nun_c
Posted: Fri Jul 01, 2022 5:53 am
question, we'll be classifying handwritten Arabic letters (all 28 of then) using whatever neural network you want to build. Start by loading in your training + testing datasets using the load_datasets function. The load_datasets function takes a path as its argument. In the folder located by the path, Python should find four CSV files with their original names. train, train_labels, test, teat_labels = load_datazeta( "path/to//your/dataset") Always make sure the shape of your dataset is correct! Your output should look like this: (13440, 32, 32, 1) (13440, 1) (****) (3360, 32, 32, 1) (3360, 1) print([x.shape for x in [train, train_labels, text, text_label]], sep="\n") What does our dataset look like? The plot randonly function will do some matplotlib magic to randomly diaplay 4 images from the training set, and 4 images from the testing aet. Above each image, you will see the index of that image, the set it was taken from (training or testing), and most importantly the class of the set. Our alphabet will be sorted by the "llija'1" sorting, ao "alef" will come first and "yeh" will come last. THE plot randomly(train, train_labels, text, text_labels) For reference, here's the naming we will use for each letter. The class numbers are therefore the ww indices of this list. For example, "alef" belongs to clasa 0, and "yah" belongs to clasz 27. arabic labels = [ 'alef', 'beh', 'tah', 'theh', 'joen', 'hah', 'khah', 'dal', 'thal', 'rah', 'zah', 'seen', 'sheen', 'ad', 'dad', 'tah', 'zah', 'ain', ghain', 'fah', 'qat', 'kaf", "lan', 'en', 'noon', 'hah', 'waw', 'yah', More basic exploratory analysis of our datasets. Let's count the number of unique train/test labels. No should get exactly 28. This is useful later when we build the output layer of our neural network. unique_elassen list( set(train_labels.aqueeze().toliat() teat_labels.aqueeze().toliat())) number_of_outputs len(unique_classes) assert number_of_outpata - 20 + One last important step: converting our output into a big binary vector. Remember that for classification problema, we will output the probability that an input belongs to one specific class. When we have multiple classes, we need an output for each class probability. The to_categorical function simply takes each label and converts it into a binary vector, something like [0, 0, 1, 0, 01. The 1 in that vector is in the same index as the corresponding label is in the arabic_labels list. You can check that out for yourself by printing train_labels[0]. train_labels, test_labels to categorical( train_labels, number of outputa), to categorical (teat_labels, number_of_outputs) Bere's sonne example: import pathlib Show me what you got! I've left the train_nodel function empty for you, so you can experiment with the train set, build your neural network, and try it out on the test set. Our only condition is that you use the accuracy(y_true, y pred) function to measure your accuracy on the train/teat sets. Check that function out to see what it takes an input and what it returna as output. Happy coding! import numpy as np. import pandas as pd from aklearn import metrics, model_selection, tree DATA_DIR= pathlib.Path(../input/a....") #training data contains both features and labela train_df = pd.read_cav (DATA_DIR / "train.cav") #split training data into features and target train features train_df.drop("labels", axia-1) train_target train_df.losti, "labels") #teating data contains only features text_features = pd.read_cav[DATA_DIR / "teat.cav
Accuracy with 938 al leat here the code: import numpy as np import matplotlib.pyplot as plt def to categorical (y, nun_classes-one): y np.array(y, dtype='int').ravel() if not nun classes: categorical = np.zeros((n, nun_classes)) categorical(np.arange(n), y15 return categorical def load_datasets (files_path: str): from pandan import read cav train_x = read_cav{"{}cavTrainImages 13440x1024.cav.format( files_path), header-Mone).valuea.antype('float32').reshape([-1, 32, 32, 11 / 255.0 train_y= read_cav("{}cavTrainLabel 13440x1.cav".format( files_path), header-Mone).values.astype('int32") - 1 text_x= read_cav("{}cavText Images 3360x1024.cav.format( num_classes np.max(y) +1 y.shape[0 files_path), header-Mone).values.astype('float32').reshape(1-1, 32, 32, 11) 255.0 text_y= read_cav{"{}cavTextLabel 3360x1.cav.format( files_path), header-Mone).valuea.antype('int32") - 1 return train_x, train_y, text_x, teat_y def accuracy ty_true: np.ndarray, y_pred: np.ndarray): - Just some sanity checks assert isinstance(y_true, np.ndarray) assert isinstance(y_pred, np.ndarray) azzert len(y_true, shape) 2 assert len(y_pred.shape) 2 azzert y true.shape[0] --20 == assert y_pred.shape[0] 28 assert y_true.shape[1] ==y_pred.shape [1] ------- All systems are go!‒‒‒‒‒‒‒‒‒‒‒ results = np.zeros((20)) for i in range (y_true.shape[1])= resulta + np.logical_and (y_true[:, i] > 0.5, y_pred[:, i] > 0.5) auration = np.aunty_true, axia-1) assert surmation.shape[0] 28 accuracy_per_class (resulta/nurmation) 100.0 overall_accuracy np.mean(accuracy_per_class) return overall accuracy, accuracy_per_class def plot randomly(training_set: np.ndarray, training_labela: np.ndarray, testing set: np.ndarray, testing_labels: np.ndarray): from randon isport randint arabic_labels ( 'alef', 'beh', 'tah', 'theh', 'jean', 'hah', 'khah', 'dal', 'thal', 'rah', 'zah', 'asen', 'sheen', zad', 'dad', 'tah', 'zah', 'ain', 'ghain', 'fah', 'gaf', 'kaf', 'lam', 'meen", 'meen', 'noon', 'heh', 'waw', 'yah", 1 f, axarr = plt.aubplota(4,4) aubplots (1 for i in range (axarr.shape [0]): subplota.append(axarri, 31) for ap_index, aubplot in enumerate(subplota): if ap index int(lan(subplota) / 2)= subplot_dataset - "training" randon_index randint(0, training set.shape[0] 1) for j in zange (axarr.shape[1]): aubplot_image=training set[random_index] aubplot class training_labela[randon_index][0] alas: TE aubplot datazet "teating" randon_index=randint(0, testing_set.shape[0] 1) paxx subplot_image=testing_set[random_index] subplot_class testing_labels[random_index][0] subplot_title = "Image #{}, {} aet, class {}".format( randon_index, subplot_dataset, arabic_label=[subplot_class]) subplot.imahow (subplot_image.aqueeze().T, cap-gray) aubplot.axia('off') plt.show() aubplot.net_title(subplot_title, size) def train_model(training_set: np.ndarray, training_labels: np.ndarray, teating_set: np.ndarray, testing_labels: np.ndarray): 1 if __nara___ == "__main__": In this