view datasets/MNIST.py @ 470:bd937e845bbb

new stuff: algorithms/logistic_regression, datasets/MNIST
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 22 Oct 2008 15:56:53 -0400
parents
children 45b3eb429c15
line wrap: on
line source

"""
Various routines to load/access MNIST data.
"""
from __future__ import absolute_import

import numpy

from ..amat import AMat

default_path = '/u/bergstrj/pub/data/mnist.amat'
"""the location of a file containing mnist data in .amat format"""


def head(n=10, path=None):
    """Load the first MNIST examples.

    Returns two matrices: x, y.  x has N rows of 784 columns.  Each row of x represents the
    28x28 grey-scale pixels in raster order.  y is a vector of N integers.  Each element y[i]
    is the label of the i'th row of x.
    
    """
    path = path if path is not None else default_path

    dat = AMat(path=path, head=n)

    return dat.input, numpy.asarray(dat.target, dtype='int64').reshape(dat.target.shape[0])

def all(path=None):
    return head(n=None, path=path)