Advertisement
Guest User

18.py

a guest
Jan 25th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. import numpy as np
  2.  
  3. times = 2000
  4.  
  5. def sgn(n):
  6.     return 1 if n > 0 else -1
  7.  
  8. def evaluate(w, x_data, y_data):
  9.     return 500 - np.sum( np.equal( y_data, np.sign( np.dot(x_data, w) ) ) )
  10.  
  11. content_train = open( 'hw1_18_train.dat', 'r' ).read().strip()
  12. content_test = open( 'hw1_18_test.dat', 'r' ).read().strip()
  13.  
  14. x_train = np.array( [ [1] + [ float(x) for x in line.split()[:4] ] for line in content_train.split('\n') ] )
  15. y_train = np.array( [ int(line.split()[-1]) for line in content_train.split('\n') ] )
  16. x_test = np.array( [ [1] + [ float(x) for x in line.split()[:4] ] for line in content_test.split('\n') ] )
  17. y_test = np.array( [ int(line.split()[-1]) for line in content_test.split('\n') ] )
  18.  
  19. _sum = 0
  20. for a in range(times):
  21.     if a % 100 == 0: print(a, end=' ', flush=True)
  22.     w_best = np.zeros( 5 )
  23.     w_new = np.zeros( 5 )
  24.     index = np.arange( len(x_train) )
  25.     np.random.shuffle( index )
  26.  
  27.     i, t = 0, 0
  28.     least_err = evaluate(w_best, x_train, y_train)
  29.     while t < 50:
  30.         if sgn(np.dot( x_train[index[i]], w_new )) != y_train[index[i]]:
  31.             w_new = w_new + y_train[index[i]] * x_train[index[i]]
  32.             t += 1
  33.  
  34.             tmp = evaluate(w_new, x_train, y_train)
  35.             if tmp < least_err:
  36.                 w_best = w_new[:]
  37.                 least_err = tmp
  38.            
  39.         i = (i + 1) % len(x_train)
  40.     _sum += evaluate(w_best, x_test, y_test)
  41.  
  42. print()
  43. print(f'avg_err: {_sum / times / len(y_train)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement