<jupyter_start><jupyter_text>CSV to images Convert MNIST dataset from csv to jpg<jupyter_code>import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import uuid
x_train = pd.read_csv("data/train.csv")
x_test = pd.read_csv("data/test.csv")
y_train = x_train["label"].values
x_train = x_train.drop("label", axis=1).values
x_test = x_test.values
x_test = x_test.reshape(x_test.shape[0], 28,28)
x_train = x_train.reshape(x_train.shape[0], 28,28)
print("X Train %s" % (x_train.shape,), "y train %s" % (y_train.shape), "X Test %s" % (x_test.shape,))
def draw_mnist(data, nrow = 3, ncol = 3, title=None):
    f, ax = plt.subplots(nrows=nrow, ncols=ncol, sharex=True, sharey=True)
    for i in range(nrow):
        for j in range(ncol):
            ax[i, j].imshow(data[i * ncol + j], cmap=plt.cm.binary)
            if title is None:
                ax[i, j].set_title(i * ncol + j)
            else:
                ax[i, j].set_title(title[i * ncol + j])
    plt.show()
draw_mnist(x_train, 3, 5, y_train)
draw_mnist(x_test, 3, 5)<jupyter_output><empty_output><jupyter_text>Convert to images<jupyter_code>def show_im(data):
    plt.imshow(data, cmap=plt.cm.Greys)
    plt.colorbar()
    plt.show()
show_im(x_train[0])
def save_im(data, fid, path="./train_images/"):
    fname = str(fid) + ".jpg"
    plt.imsave(arr=data, fname=path+fname, cmap=plt.cm.Greys)
    
labels = []
for i in range(x_train.shape[0]):
    uid = uuid.uuid4().hex
    save_im(x_train[i], uid)
    labels.append([uid, y_train[i]])
    
print(len(labels))
tosave = pd.DataFrame(data=labels,columns=['Imageid', "label"])
tosave.to_csv("train_labels.csv", index=False)
path = "./test_images/"
test_list = []
for i in range(x_test.shape[0]):
    uid = uuid.uuid4().hex
    save_im(x_test[i], uid, path=path)
    new_item = [i+1, uid, 0]
    test_list.append(new_item)
    if i % 100 == 0:
        print(new_item, end='\r')

print("Test dset length %d" % len(test_list))
tosave = pd.DataFrame(data = test_list, columns=["ImageId", "FileId", "Label"])
tosave.to_csv("\n\ntest_labels.csv", index=False)<jupyter_output>Test dset length 280009019e097d943c500dbb', 0]
{'fraction_non_alphanumeric': 0.16518898740083995, 'fraction_numerical': 0.021931871208586095, 'mean_word_length': 4.032863849765258, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '231372', 'license': 'MIT', 'n_tokens_mistral': 909, 'n_tokens_neox': 876, 'n_words': 176, 'path': 'csv_to_image.ipynb', 'repo_name': 'enikolaev/DigitRecognizer', 'size': '2143'}