support/parse_data.py
def toyClass():
    '''
    Data preparation function, for a small toy set of data,
    designed for classification using a multi-class logistic
    regression model.
    '''
    dataset = "toyClass"
    dinfo = classes.DataInfo()
    dinfo.mname = "LgstReg" # hard-coded model name.

    print("Preparation (", dataset, ")...")

    n = 25 # training set size
    m = 20 # testing set size
    d = 3 # number of inputs
    nc = 4 # number of classes.

    # Hand-prepared weights (assuming 4 classes).
    w_0 = np.array( [3.1415, 1.4142, 2.7182] ).reshape((d,1))
    w_1 = np.array( [3.1415, -1.4142, 2.7182] ).reshape((d,1))
    w_2 = np.array( [-3.1415, 1.4142, -2.7182] ).reshape((d,1))

    # Put the weights into a (d x nc-1) matrix (note the transpose).
    W_true = np.transpose(np.concatenate( (w_0,w_1,w_2) ).reshape((nc-1,d)))

    # Randomly generated inputs.
    X_tr = np.random.normal(loc=0, scale=1.0, size=n*d).reshape((n,d))
    X_te = np.random.normal(loc=0, scale=1.0, size=m*d).reshape((m,d))

    # Probabilities based on true underlying model (training).
    A = np.zeros(n*nc).reshape((nc,n))
    A[:-1,:] = np.dot(W_true, np.transpose(X_tr)) # leave last row as zeros.
    P = np.exp(A) / np.sum(np.exp(A), axis=0) # (nc x n)

    # Labels (training).
    y_tr = np.zeros(n, dtype=np.uint8).reshape((n,1))
    for i in range(n):
        probs = P[:,i]
        y_tr[i,0] = np.random.choice(nc, size=1, replace=True, p=probs)

    # Probabilities based on true underlying model (testing).
    A = np.zeros(m*nc).reshape((nc,m))
    A[:-1,:] = np.dot(W_true, np.transpose(X_te)) # leave last row as zeros.
    P = np.exp(A) / np.sum(np.exp(A), axis=0) # (nc x m)

    # Labels (testing).
    y_te = np.zeros(m, dtype=np.uint8).reshape((m,1))
    for i in range(m):
        probs = P[:,i]
        y_te[i,0] = np.random.choice(nc, size=1, replace=True, p=probs)

    # Write inputs (training).
    towrite = os.path.join("data", dataset, ("X_tr" + ".dat"))
    data_arr = X_tr
    dinfo.X_tr["shape"] = data_arr.shape
    dinfo.X_tr["path"] = towrite
    dinfo.X_tr["dtype"] = data_arr.dtype
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)

    # Write inputs (testing).
    towrite = os.path.join("data", dataset, ("X_te" + ".dat"))
    data_arr = X_te
    dinfo.X_te["shape"] = data_arr.shape
    dinfo.X_te["path"] = towrite
    dinfo.X_te["dtype"] = data_arr.dtype    
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)

    # Write outputs (training).
    towrite = os.path.join("data", dataset, ("y_tr" + ".dat"))
    data_arr = y_tr
    dinfo.y_tr["shape"] = data_arr.shape
    dinfo.y_tr["path"] = towrite
    dinfo.y_tr["dtype"] = data_arr.dtype
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)

    # Write outputs (testing).
    towrite = os.path.join("data", dataset, ("y_te" + ".dat"))
    data_arr = y_te
    dinfo.y_te["shape"] = data_arr.shape
    dinfo.y_te["path"] = towrite
    dinfo.y_te["dtype"] = data_arr.dtype
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)        

    # Save the dinfo dictionary for future use (so we don't have to read
    # the original data every time).

    towrite = os.path.join("data", dataset, "info.dat")
    with open(towrite, mode="wb") as f:
        pickle.dump(dinfo, f)

    # Finally, return the dinfo dict.
    return dinfo

    Data preparation function, for a small toy set of data,
    designed for classification using a multi-class logistic
    regression model.
    
Data preparation function, for a small toy set of data,
designed for classification using a multi-class logistic
regression model.
Data preparation function for a small toy set of data designed for classification using a multi - class logistic regression model .
def toyClass():
    dataset = "toyClass"
    dinfo = classes.DataInfo()
    dinfo.mname = "LgstReg" 
    print("Preparation (", dataset, ")...")
    n = 25 
    m = 20 
    d = 3 
    nc = 4 
    w_0 = np.array( [3.1415, 1.4142, 2.7182] ).reshape((d,1))
    w_1 = np.array( [3.1415, -1.4142, 2.7182] ).reshape((d,1))
    w_2 = np.array( [-3.1415, 1.4142, -2.7182] ).reshape((d,1))
    W_true = np.transpose(np.concatenate( (w_0,w_1,w_2) ).reshape((nc-1,d)))
    X_tr = np.random.normal(loc=0, scale=1.0, size=n*d).reshape((n,d))
    X_te = np.random.normal(loc=0, scale=1.0, size=m*d).reshape((m,d))
    A = np.zeros(n*nc).reshape((nc,n))
    A[:-1,:] = np.dot(W_true, np.transpose(X_tr)) 
    P = np.exp(A) / np.sum(np.exp(A), axis=0) 
    y_tr = np.zeros(n, dtype=np.uint8).reshape((n,1))
    for i in range(n):
        probs = P[:,i]
        y_tr[i,0] = np.random.choice(nc, size=1, replace=True, p=probs)
    A = np.zeros(m*nc).reshape((nc,m))
    A[:-1,:] = np.dot(W_true, np.transpose(X_te)) 
    P = np.exp(A) / np.sum(np.exp(A), axis=0) 
    y_te = np.zeros(m, dtype=np.uint8).reshape((m,1))
    for i in range(m):
        probs = P[:,i]
        y_te[i,0] = np.random.choice(nc, size=1, replace=True, p=probs)
    towrite = os.path.join("data", dataset, ("X_tr" + ".dat"))
    data_arr = X_tr
    dinfo.X_tr["shape"] = data_arr.shape
    dinfo.X_tr["path"] = towrite
    dinfo.X_tr["dtype"] = data_arr.dtype
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)
    towrite = os.path.join("data", dataset, ("X_te" + ".dat"))
    data_arr = X_te
    dinfo.X_te["shape"] = data_arr.shape
    dinfo.X_te["path"] = towrite
    dinfo.X_te["dtype"] = data_arr.dtype    
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)
    towrite = os.path.join("data", dataset, ("y_tr" + ".dat"))
    data_arr = y_tr
    dinfo.y_tr["shape"] = data_arr.shape
    dinfo.y_tr["path"] = towrite
    dinfo.y_tr["dtype"] = data_arr.dtype
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)
    towrite = os.path.join("data", dataset, ("y_te" + ".dat"))
    data_arr = y_te
    dinfo.y_te["shape"] = data_arr.shape
    dinfo.y_te["path"] = towrite
    dinfo.y_te["dtype"] = data_arr.dtype
    with open(towrite, mode="bw") as g_bin:
        data_arr.tofile(g_bin)        
    towrite = os.path.join("data", dataset, "info.dat")
    with open(towrite, mode="wb") as f:
        pickle.dump(dinfo, f)
    return dinfo
def toyClass ( ) : dataset = "toyClass" dinfo = classes . DataInfo ( ) dinfo . mname = "LgstReg" print ( "Preparation (" , dataset , ")..." ) n = 25 m = 20 d = 3 nc = 4 w_0 = np . array ( [ 3.1415 , 1.4142 , 2.7182 ] ) . reshape ( ( d , 1 ) ) w_1 = np . array ( [ 3.1415 , - 1.4142 , 2.7182 ] ) . reshape ( ( d , 1 ) ) w_2 = np . array ( [ - 3.1415 , 1.4142 , - 2.7182 ] ) . reshape ( ( d , 1 ) ) W_true = np . transpose ( np . concatenate ( ( w_0 , w_1 , w_2 ) ) . reshape ( ( nc - 1 , d ) ) ) X_tr = np . random . normal ( loc = 0 , scale = 1.0 , size = n * d ) . reshape ( ( n , d ) ) X_te = np . random . normal ( loc = 0 , scale = 1.0 , size = m * d ) . reshape ( ( m , d ) ) A = np . zeros ( n * nc ) . reshape ( ( nc , n ) ) A [ : - 1 , : ] = np . dot ( W_true , np . transpose ( X_tr ) ) P = np . exp ( A ) / np . sum ( np . exp ( A ) , axis = 0 ) y_tr = np . zeros ( n , dtype = np . uint8 ) . reshape ( ( n , 1 ) ) for i in range ( n ) : probs = P [ : , i ] y_tr [ i , 0 ] = np . random . choice ( nc , size = 1 , replace = True , p = probs ) A = np . zeros ( m * nc ) . reshape ( ( nc , m ) ) A [ : - 1 , : ] = np . dot ( W_true , np . transpose ( X_te ) ) P = np . exp ( A ) / np . sum ( np . exp ( A ) , axis = 0 ) y_te = np . zeros ( m , dtype = np . uint8 ) . reshape ( ( m , 1 ) ) for i in range ( m ) : probs = P [ : , i ] y_te [ i , 0 ] = np . random . choice ( nc , size = 1 , replace = True , p = probs ) towrite = os . path . join ( "data" , dataset , ( "X_tr" + ".dat" ) ) data_arr = X_tr dinfo . X_tr [ "shape" ] = data_arr . shape dinfo . X_tr [ "path" ] = towrite dinfo . X_tr [ "dtype" ] = data_arr . dtype with open ( towrite , mode = "bw" ) as g_bin : data_arr . tofile ( g_bin ) towrite = os . path . join ( "data" , dataset , ( "X_te" + ".dat" ) ) data_arr = X_te dinfo . X_te [ "shape" ] = data_arr . shape dinfo . X_te [ "path" ] = towrite dinfo . X_te [ "dtype" ] = data_arr . dtype with open ( towrite , mode = "bw" ) as g_bin : data_arr . tofile ( g_bin ) towrite = os . path . join ( "data" , dataset , ( "y_tr" + ".dat" ) ) data_arr = y_tr dinfo . y_tr [ "shape" ] = data_arr . shape dinfo . y_tr [ "path" ] = towrite dinfo . y_tr [ "dtype" ] = data_arr . dtype with open ( towrite , mode = "bw" ) as g_bin : data_arr . tofile ( g_bin ) towrite = os . path . join ( "data" , dataset , ( "y_te" + ".dat" ) ) data_arr = y_te dinfo . y_te [ "shape" ] = data_arr . shape dinfo . y_te [ "path" ] = towrite dinfo . y_te [ "dtype" ] = data_arr . dtype with open ( towrite , mode = "bw" ) as g_bin : data_arr . tofile ( g_bin ) towrite = os . path . join ( "data" , dataset , "info.dat" ) with open ( towrite , mode = "wb" ) as f : pickle . dump ( dinfo , f ) return dinfo
Data preparation function, for a small toy set of data,
designed for classification using a multi-class logistic
regression model.
Data preparation function for a small toy set of data designed for classification using a multi - class logistic regression model .
'''
    Data preparation function, for a small toy set of data,
    designed for classification using a multi-class logistic
    regression model.
    ''' # hard-coded model name. # training set size # testing set size # number of inputs # number of classes. # Hand-prepared weights (assuming 4 classes). # Put the weights into a (d x nc-1) matrix (note the transpose). # Randomly generated inputs. # Probabilities based on true underlying model (training). # leave last row as zeros. # (nc x n) # Labels (training). # Probabilities based on true underlying model (testing). # leave last row as zeros. # (nc x m) # Labels (testing). # Write inputs (training). # Write inputs (testing). # Write outputs (training). # Write outputs (testing). # Save the dinfo dictionary for future use (so we don't have to read # the original data every time). # Finally, return the dinfo dict.