SHARE
TWEET

Untitled

a guest Oct 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import random
  3. from matplotlib import pyplot as plt
  4.  
  5. def main(extend_attribute):
  6.     vectors, answers = loadData('hw3_dataset.txt', extend_attribute)
  7.     row, col = vectors.shape
  8.     weights = np.array([random.random() for i in range(col + 1)])
  9.     # weights = np.array([0.3334, 0.3334, 0.3334, 0.3334, 0.3334, -1.0001])
  10.    
  11.     epoch = 0
  12.    
  13.     while(True):
  14.         # print(epoch)
  15.         # print(weights)
  16.         predicts = classification(vectors, weights)
  17.  
  18.         # print(predicts == answers)
  19.         if not np.all(predicts == answers):    
  20.             # print(predicts)
  21.             # print(answers)
  22.             weights = update(predicts, answers, weights, vectors)
  23.             epoch += 1
  24.  
  25.         else:
  26.             epoch += 1
  27.             break
  28.        
  29.         # input()
  30.     # print(epoch)
  31.     # print(weights)
  32.     return epoch
  33.  
  34.  
  35. def classification(vectors, weights):
  36.     row, col = vectors.shape
  37.     ones = np.ones((row, 1), dtype=np.int64)
  38.     vectors = np.append(vectors, ones, axis=-1)
  39.  
  40.     weights = np.tile(weights, (row, 1))
  41.     weighted = np.multiply(vectors, weights)
  42.     output = np.array([w.sum() for w in weighted])
  43.  
  44.     # print(output)
  45.    
  46.     return output > 0
  47.  
  48. def update(predicts, labels, weights, vectors):
  49.     row, col = vectors.shape
  50.     ones = np.ones((row, 1), dtype=np.int64)
  51.     vectors = np.append(vectors, ones, axis=-1)
  52.    
  53.    
  54.     predicts = np.where(predicts == True, 1, 0)
  55.     labels = np.where(labels == True, 1, 0)
  56.  
  57.     # print('loss = %d' % np.abs(predicts - labels).sum())
  58.  
  59.     for i in range(row):
  60.         # print(learning_rate * (labels[i] - predicts[i]) * weights)
  61.         weights += learning_rate * (labels[i] - predicts[i]) * vectors[i]
  62.  
  63.     return weights
  64.  
  65.  
  66. def loadData(filename, N):
  67.     data = np.genfromtxt(filename, dtype='str')
  68.     vectors = np.array([list(bstring) for bstring in data[:, 1]]).astype(np.int64)
  69.     row, col = vectors.shape
  70.     extended = np.random.randint(2, size=(row, N))
  71.     vectors = np.concatenate((vectors, extended), axis=1)
  72.     answers = np.array([vector.sum() >= 3 for vector in vectors])
  73.  
  74.  
  75.     return vectors, answers
  76.  
  77.  
  78. if __name__ == '__main__':
  79.  
  80.     learning_rate = 0.002
  81.     epochs = []
  82.     extend_attributes = []
  83.     for i in range(20):
  84.         epochnum = main(i + 1)
  85.         extend_attributes.append(i + 1)
  86.         epochs.append(epochnum)
  87.         # print('learning_rate = %.3f, epoch = %d' %(learning_rate, epochnum))
  88.     print(epochs)
  89.  
  90.     plt.plot(extend_attributes, epochs)
  91.     plt.xlabel('extended attributes')
  92.     plt.ylabel('epoch')
  93.     plt.savefig('hw3_2.jpg')
  94.  
  95.  
  96.  
  97.     # learning_rate = 0.001
  98.     # print(main())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top