import numpy as np def sgn(x): return np.where( x > 0 , 1 , -1 ) def theta(s): return 1 / ( 1 + np.exp( -s ) ) content_train = open( 'hw3_train.dat', 'r' ).read().strip() content_test = open( 'hw3_test.dat', 'r' ).read().strip() x_train = np.array( [ [1] + [ float(x) for x in line.split()[:-1] ] for line in content_train.split('\n') ] ) y_train = np.array( [ int(line.split()[-1]) for line in content_train.split('\n') ] ) x_test = np.array( [ [1] + [ float(x) for x in line.split()[:-1] ] for line in content_test.split('\n') ] ) y_test = np.array( [ int(line.split()[-1]) for line in content_test.split('\n') ] ) N_train = len(y_train) N_test = len(y_test) T = 2000 Eta = 0.001 w = np.zeros( len( x_train[0] ) ) for _ in range(T): grad = np.sum( ( theta( y_train * np.dot( x_train, w ) ) * -y_train ).reshape((N_train, 1)) * x_train, axis=0 ) / N_train w = w - Eta * grad Eout_avg = np.sum( np.not_equal( y_test, sgn( np.dot( x_test , w ) ) ) ) / N_test print(f'Eout_avg = {Eout_avg}')