numpy – Perceptron usage and callification accuracies with Python

I recently finished a homework on the perceptron algorithm, the given answers on the website from where this exercise was extracted are different on the top_10 word lists and on mnist accuracy test results, I must have messed up somewhere. My answers and code are on, the original code is untouched except for some matrix that was changed to np.unit8 on extract_bow_feature_vectors ( for less memory usage.
Any comments on how to improve the code on are appreciated also if I messed up changing to unit8.
Thank you for your time.

import pdb
import numpy as np
import code_for_hw3_part2 as hw3

# Auto Data

# Returns a list of dictionaries.  Keys are the column names, including mpg.
auto_data_all = hw3.load_auto_data('auto-mpg.tsv')
print (auto_data_all(0))
# The choice of feature processing for each feature, mpg is always raw and
# does not need to be specified.  Other choices are hw3.standard and hw3.one_hot.
# 'name' is not numeric and would need a different encoding.
features = (('cylinders', hw3.raw),
            ('displacement', hw3.raw),
            ('horsepower', hw3.raw),
            ('weight', hw3.raw),
            ('acceleration', hw3.raw),
            ## Drop model_year by default
            ## ('model_year', hw3.raw),
            ('origin', hw3.raw))

# Construct the standard data and label arrays
auto_data, auto_labels = hw3.auto_data_and_labels(auto_data_all, features)
print('nauto data and labels shape', auto_data.shape, auto_labels.shape)

if False:                               # set to True to see histograms
    import matplotlib.pyplot as plt
    for feat in range(auto_data.shape(0)):
        print('Feature', feat, features(feat)(0))
        # Plot histograms in one window, different colors
        plt.hist(auto_data(feat,auto_labels(0,:) > 0))
        plt.hist(auto_data(feat,auto_labels(0,:) < 0))
        # Plot histograms in two windows, different colors
        fig,(a1,a2) = plt.subplots(nrows=2)
        a1.hist(auto_data(feat,auto_labels(0,:) > 0))
        a2.hist(auto_data(feat,auto_labels(0,:) < 0))

# Analyze auto data
# Perform 10-fold cross-validation for all combinations of the two algorithms, 
# three TTT values, and the two choices of feature sets. 
# It will be worthwhile investing in a piece of code to carry out all of the evaluations, 
# in case you need to do this more than once.
# In general, you should shuffle the dataset before evaluating, 
# but for this exercise, please use hw3.xval_learning_alg, 
# which shuffles the dataset for you, so that your results match ours.

print ("nFeature sets:")
import pandas as pd
option2 = (hw3.one_hot, hw3.standard, hw3.standard, hw3.standard, hw3.standard, hw3.one_hot)
names = (element(0) for element in features)
features2 = list(zip(names, option2))
df = pd.DataFrame(((element(1).__name__ for element in f)for f in (features, features2)), columns = names)
print (df)
auto_data2, auto_labels2 = hw3.auto_data_and_labels(auto_data_all, features2)
data_list = ((auto_data, auto_labels), (auto_data2, auto_labels2))
print ("nAuto data shape:", auto_data2.shape)

def scores(Ts, data, j=0):
        tb_data = np.empty((len(Ts), 2))
        new_data, labels = data
        for i, T in enumerate(Ts):
            tb_data(i)= (hw3.xval_learning_alg(lambda new_data, labels: algo(new_data, labels, {"T": T}), new_data, labels, 10)
                            for algo in (hw3.perceptron, hw3.averaged_perceptron))
        df = pd.DataFrame(tb_data, Ts, ("Perceptron", "Averaged Perceptron"))
        print ("n10 fold accuracy Results for feature_set:", j)
        print (df)

for j, data in enumerate(data_list):
    scores((1, 10, 50), data, j)

# Construct Classifies and answer wich Feature has more impact on output prediction?
print ("nFeature with most impact on averaged_perceptron prediction:")

entries = {f:list(set((entry(f) for entry in auto_data_all))) 
               for (f, phi) in features2 if phi==hw3.one_hot}

vector_index = ()
for feature in features2:
    if feature(0) in entries.keys():    
        quant = len(entries(feature(0)))
        vector_index += (feature(0))*quant

theta, theta0 = hw3.averaged_perceptron(auto_data2, auto_labels2, {"T":10})
greates_value = max(theta.max(), theta.min(), key = abs)
print (vector_index(np.where(theta == greates_value)(0)(0)).upper(), "n")

# Review Data

# Returns lists of dictionaries.  Keys are the column names, 'sentiment' and 'text'.
# The train data has 10,000 examples
review_data = hw3.load_review_data('reviews.tsv')

# Lists texts of reviews and list of labels (1 or -1)
review_texts, review_label_list = zip(*((sample('text'), sample('sentiment')) for sample in review_data))

# The dictionary of all the words for "bag of words"
dictionary = hw3.bag_of_words(review_texts)

# The standard data arrays for the bag of words
review_bow_data = hw3.extract_bow_feature_vectors(review_texts, dictionary)
review_labels = hw3.rv(review_label_list)
print('review_bow_data and labels shape', review_bow_data.shape, review_labels.shape)

# Analyze review data

# use T =  10 and average perceptron since this was the best combination
theta, theta0 = hw3.averaged_perceptron(review_bow_data, review_labels, {"T": 10})
rev_dic = hw3.reverse_dict(dictionary)
top_10 = np.argpartition(theta, -10, axis = 0)(-10:)
bot_10 = np.argpartition(theta, 10, axis = 0)(:10)
print ("nTop 10 positive words:")
print ((rev_dic(element) for element in top_10.T(0)))
print ("nTop 10 negative words:")
print ((rev_dic(element) for element in bot_10.T(0)))

# MNIST Data

Returns a dictionary formatted as follows:
    0: {
        "images": ((m by n image), (m by n image), ...),
        "labels": (0, 0, ..., 0)
    1: {...},
Where labels range from 0 to 9 and (m, n) images are represented
by arrays of floats from 0 to 1
mnist_data_all = hw3.load_mnist_data(range(10))

print('nmnist_data_all loaded. shape of single images is', mnist_data_all(0)("images")(0).shape)

# HINT: change the (0) and (1) if you want to access different images
d0 = mnist_data_all(0)("images")
d1 = mnist_data_all(1)("images")
y0 = np.repeat(-1, len(d0)).reshape(1,-1)
y1 = np.repeat(1, len(d1)).reshape(1,-1)

# data goes into the feature computation functions
data = np.vstack((d0, d1))
# labels can directly go into the perceptron algorithm
labels = np.vstack((y0.T, y1.T)).T

def raw_mnist_features(x):
    @param x (n_samples,m,n) array with values in (0,1)
    @return (m*n,n_samples) reshaped array where each entry is preserved
    n_samples, m, n = x.shape
    return np.reshape(x, (m*n, n_samples))

def row_average_features(x):
    This should either use or modify your code from the tutor questions.

    @param x (n_samples,m,n) array with values in (0,1)
    @return (m,n_samples) array where each entry is the average of a row
    n_samples, m, _ = x.shape
    return np.reshape(np.mean(x, axis=2,keepdims=True), (m, n_samples))

def col_average_features(x):
    This should either use or modify your code from the tutor questions.

    @param x (n_samples,m,n) array with values in (0,1)
    @return (n,n_samples) array where each entry is the average of a column
    n_samples, _, n = x.shape
    return np.reshape(np.mean(x, axis=1,keepdims=True).T, (n, n_samples))

def top_bottom_features(x):
    This should either use or modify your code from the tutor questions.

    @param x (n_samples,m,n) array with values in (0,1)
    @return (2,n_samples) array where the first entry of each column is the average of the
    top half of the image = rows 0 to floor(m/2) (exclusive)
    and the second entry is the average of the bottom half of the image
    = rows floor(m/2) (inclusive) to m
    n_samples, m, _ = x.shape
    split_index = m//2
    return np.reshape(,:split_index), axis =(1,2), keepdims = True), np.mean(x(:,split_index:), axis =(1,2), keepdims = True))), (2, n_samples))

# use this function to zerosevaluate accuracy

#acc = hw3.get_classification_accuracy(raw_mnist_features(data), labels)
#print (acc)
# Analyze MNIST data

# Your code here to process the MNIST data

def data_builder(data, numbers):
    x, y = numbers
    d0 = data(x)("images")
    d1 = data(y)("images")
    y0 = np.repeat(-1, len(d0)).reshape(1, -1)
    y1 = np.repeat(1, len(d1)).reshape(1, -1)
    return np.vstack((d0, d1)), np.vstack((y0.T, y1.T)).T

l = ((0,1), (2, 4), (6,8), (9,0))
fun_list =(raw_mnist_features, row_average_features, col_average_features, top_bottom_features)
results = np.empty((len(l), len(fun_list)))
for i, numbers in enumerate(l):
    data, lables = data_builder(mnist_data_all, numbers)
    for j, fun in enumerate(fun_list):
        new = fun(data)
        results(i)(j) = (hw3.get_classification_accuracy(new, lables))
df = pd.DataFrame(results, (f"{element(0)} vs {element(1)}" for element in l), (fun.__name__ for fun in fun_list))
print ("10 fold classification accuracy results for features and numbers")
print (df)