for (x_string, label_string) in zip(fp_data, fp_labels): x_string = x_string.strip() label_string = label_string.strip() if not x_string: assert not label_string continue label = int(label_string) if label not in (-1, 1): logging.error("Unknown label: %d" % label) sys.exit(2) x_original = np.fromstring(x_string, sep=' ') # Transform the features. x = transform(x_original).flatten() if not x.shape == weights.shape: logging.error("Shapes of weight vector and transformed " "data don't match") sys.exit(3) if np.abs(np.sum(weights)) < 1e-10: logging.error("Zero vector provided") sys.exit(4) if label*np.inner(weights, x) >= 0: accuracy += 1 total += 1 print("%d" % (total)) print("%d" % (accuracy))
# So we can use the same feature vectors. from mapper import transform if __name__ == "__main__": if not len(sys.argv) == 3: logging.error("Usage: evaluate.py weights.txt test_data.txt") sys.exit(1) with open(sys.argv[1], "r") as fp_weights: weights = np.genfromtxt(fp_weights).flatten() with open(sys.argv[2], "r") as fp_data: #with open(sys.argv[3], "r") as fp_labels: for x_string in fp_data: x_string = x_string.strip() x_original = np.fromstring(x_string, sep=' ') x = transform(x_original).flatten() # Use our features. if not x.shape == weights.shape: logging.error("Shapes of weight vector and transformed " "data don't match") sys.exit(3) #if (np.inner(weights, x) < 0): # print -1 #else: # print 1 print(-1 if np.inner(weights, x) < 0 else 1)
with open(sys.argv[2], "r") as fp_data: with open(sys.argv[3], "r") as fp_labels: for (x_string, label_string) in zip(fp_data, fp_labels): x_string = x_string.strip() label_string = label_string.strip() if not x_string: assert not label_string continue label = int(label_string) if label not in (-1, 1): logging.error("Unknown label: %d" % label) sys.exit(2) x_original = np.fromstring(x_string, sep=' ') x = transform(x_original).flatten() # Use our features. if not x.shape == weights.shape: logging.error("Shapes of weight vector and transformed " "data don't match") sys.exit(3) # if (np.inner(weights, x) < 0): # print -1 # else: # print 1 if label * np.inner(weights, x) >= 0: accuracy += 1 total += 1 print("%f" % (float(accuracy) / total))
import sys import numpy as np sys.path.append(sys.argv[4]) from mapper import transform if __name__ == "__main__": with open(sys.argv[1], "r") as weights_file: weights = np.genfromtxt(weights_file).flatten() with open(sys.argv[2], "r") as data_file: with open(sys.argv[3], "w") as pred_file: for x_string in data_file: # Apply the transformation for x and then continue. # Compute the label and write it to the prediction file. x_o = np.fromstring(x_string, sep=' ') x_t = transform(x_o) print("WEIGHTS: %s"%(weights.shape)) print("VALUES: %s"%(x_t.shape)) if np.inner(weights, x_t) > 0: pred_file.write("-1\n") else: pred_file.write("+1\n")
import sys import numpy as np sys.path.append(sys.argv[4]) from mapper import transform if __name__ == "__main__": with open(sys.argv[1], "r") as weights_file: weights = np.genfromtxt(weights_file).flatten() with open(sys.argv[2], "r") as data_file: with open(sys.argv[3], "w") as pred_file: for x_string in data_file: # Apply the transformation for x and then continue. # Compute the label and write it to the prediction file. x_o = np.fromstring(x_string, sep=' ') x_t = transform(x_o) print("WEIGHTS: %s" % (weights.shape)) print("VALUES: %s" % (x_t.shape)) if np.inner(weights, x_t) > 0: pred_file.write("-1\n") else: pred_file.write("+1\n")