n = 50 X, Y = make_classification(n_samples = n, n_features=2, n_redundant=0, n_informative=2, flip_y=0) Y = Y*2-1# convert initial 0/1 values into -1/1 X = X.astype(np.float32); Y = Y.astype(np.int32) # features - float, label - int
# Split the dataset into training and test train_x, test_x = np.split(X, [ n*8//10]) train_labels, test_labels = np.split(Y, [n*8//10]) print(train_x.shape, train_labels.shape) print("Features:\n",train_x[0:4]) print("Labels:\n",train_labels[0:4])
deftrain(positive_examples, negative_examples, num_iterations = 100): num_dims = positive_examples.shape[1] # Initialize weights. # We initialize with 0 for simplicity, but random initialization is also a good idea weights = np.zeros((num_dims,1)) pos_count = positive_examples.shape[0] neg_count = negative_examples.shape[0] report_frequency = 10 for i inrange(num_iterations): # Pick one positive and one negative example pos = random.choice(positive_examples) neg = random.choice(negative_examples)
z = np.dot(pos, weights) if z < 0: # 正值标签获得了负值输出 weights = weights + pos.reshape(weights.shape)
z = np.dot(neg, weights) if z >= 0: # 负值标签获得了正值输出 weights = weights - neg.reshape(weights.shape) # Periodically, print out the current accuracy on all examples if i % report_frequency == 0: pos_out = np.dot(positive_examples, weights) neg_out = np.dot(negative_examples, weights) pos_correct = (pos_out >= 0).sum() / float(pos_count) neg_correct = (neg_out < 0).sum() / float(neg_count) print("Iteration={}, pos correct={}, neg correct={}".format(i,pos_correct,neg_correct))
defset_mnist_pos_neg(positive_label, negative_label): positive_indices = [i for i, j inenumerate(MNIST['Train']['Labels']) if j == positive_label] negative_indices = [i for i, j inenumerate(MNIST['Train']['Labels']) if j == negative_label]