Python ile CSV Dosyalarını Okuma

Python programı ile csv dosyalarını okumak için örnek bir kod parçası ayarlanmıştır.  Örnekte okunan dosya mnist dataseti için hazırlanmıştır. Okunan satırın ilk elemanı resmin etiketi diğerleri (784) ise resmin 28×28 lik matrisinin vektöre dönüştürülmüş halidir.

Örnek Python Kodu:

def readCSV(filepath):
  images=[]
  labels=[]

  with open(filepath, 'rt', encoding='utf8') as csvfile:
    image_reader = csv.reader(csvfile)
    #csvfile.close()
    for row in image_reader:
      labels.append(row[0])
      images.append(row[1:])


  csvfile.close()
  
  return [images, labels]

[ training_images, training_labels] = readCSV(os.path.join("drive", "Colab Notebooks", "mnist_dataset", "mnist_train_100.csv"))
[ test_images, test_labels] = readCSV(os.path.join("drive", "Colab Notebooks", "mnist_dataset", "mnist_test_10.csv"))

#train
training_images = numpy.array(training_images, dtype="uint8")
training_labels = numpy.array(training_labels, dtype="uint8")

#test
test_images = numpy.array(test_images, dtype="uint8")
test_labels = numpy.array(test_labels, dtype="uint8")

Örnek veri setleri link