Skip to content

Instantly share code, notes, and snippets.

@skarlekar
Last active January 23, 2023 06:56
Show Gist options
  • Save skarlekar/31a235d3c0bd80c40dba36dee0f9a7dc to your computer and use it in GitHub Desktop.
Save skarlekar/31a235d3c0bd80c40dba36dee0f9a7dc to your computer and use it in GitHub Desktop.
Sign-language MNist Data CSV Conversion: Convert 28x28 pixel image data passed as a label plus 784 field CSV record file into a Numpy array of shape (length, 28, 28) and a label array of (length,) as output
import numpy as np
# You will need to write code that will read the file passed
# into this function. The first line contains the column headers
# so you should ignore it
# Each successive line contians 785 comma separated values between 0 and 255
# The first value is the label
# The rest are the pixel values for that picture
# The function will return 2 np.array types. One with all the labels
# One with all the images
#
# Tips:
# If you read a full line (as 'row') then row[0] has the label
# and row[1:785] has the 784 pixel values
# Take a look at np.array_split to turn the 784 pixels into 28x28
# You are reading in strings, but need the values to be floats
# Check out np.array().astype for a conversion
def get_data(filename):
with open(filename) as data_file:
# Your code starts here
headers = data_file.readline()
a = np.loadtxt(data_file, delimiter=',')
labels = a[:,0]
a = a[:,1:]
images = np.reshape(a, (-1, 28, 28))
images = images.astype(float)
return images, labels
path_sample_csv = f"/Users/skarlekar/Downloads/csvsample.csv"
training_images, training_labels = get_data3(path_sample_csv)
print("training_images.shape: ", training_images.shape)
print("training_labels.shape: ", training_labels.shape)
# Expected: (10, 28, 28)
@hzlnqodrey
Copy link

this helps me soo much, thanks sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment