Aan de slagGa gratis aan de slag

Using NumPy to import flat files

In this exercise, you're now going to load the MNIST digit recognition dataset using the numpy function loadtxt() and see just how easy it can be:

  • The first argument will be the filename.
  • The second will be the delimiter which, in this case, is a comma.

The MNIST dataset is a collection of handwritten digits from 0 to 9, frequently used in the field of machine learning. It serves as a benchmark for evaluating algorithm performance in recognizing and classifying these numbers.

Deze oefening maakt deel uit van de cursus

Introduction to Importing Data in Python

Cursus bekijken

Oefeninstructies

  • Fill in the arguments of np.loadtxt() by passing file and a comma ',' for the delimiter.
  • Fill in the argument of print() to print the type of the object digits. Use the function type().
  • Execute the rest of the code to visualize one of the rows of the data.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Import packages
import numpy as np

# Assign filename to variable: file
file = 'digits.csv'

# Load file as array: digits
digits = np.loadtxt(____, delimiter='____')

# Print datatype of digits
print(____)

# Select and reshape a row
im = digits[21, 1:]
im_sq = np.reshape(im, (28, 28))

# Plot reshaped data (matplotlib.pyplot already loaded as plt)
plt.imshow(im_sq, cmap='Greys', interpolation='nearest')
plt.show()
Code bewerken en uitvoeren