def learnerTest(naTrain, naTest): ''' @summary: Takes testing and training data and computes average error over the test set This is compared to a baseline guess which is just the average of the training set ''' llRange = range(5, 51, 5) lfRes = [] for lK in llRange: cLearn = ftu.createKnnLearner(naTrain, lKnn=lK, leafsize=100) fError = 0.0 naResult = cLearn.query(naTest[:, :-1]) naError = abs(naResult - naTest[:, -1]) lfRes.append(np.average(naError)) ''' Generate error of 'dumb' case, just assume average returns every time ''' naGuess = np.ones(naTest.shape[0]) * np.average(naTrain[:, -1]) lfGuess = [np.average(abs(naGuess - naTest[:, -1]))] * len(lfRes) fAvgRets = np.average(naTest[:, -1]) plt.clf() plt.plot(llRange, lfRes) plt.plot(llRange, lfGuess) plt.title('Average error on average returns of %.04lf' % fAvgRets) plt.legend(('Learner Predict', 'Average Return Predict')) plt.xlabel('K value') plt.ylabel('Error') plt.show() plt.savefig('FeatureTest.png', format='png')
def learnerTest( naTrain, naTest ): ''' @summary: Takes testing and training data and computes average error over the test set This is compared to a baseline guess which is just the average of the training set ''' llRange = range(5,51,5) lfRes = [] for lK in llRange: cLearn = ftu.createKnnLearner( naTrain, lKnn=lK, leafsize=100 ) fError = 0.0 naResult = cLearn.query( naTest[:,:-1] ) naError = abs( naResult - naTest[:,-1] ) lfRes.append( np.average(naError) ) ''' Generate error of 'dumb' case, just assume average returns every time ''' naGuess = np.ones( naTest.shape[0] ) * np.average( naTrain[:,-1] ) lfGuess = [ np.average( abs(naGuess - naTest[:,-1]) ) ] * len(lfRes) fAvgRets = np.average(naTest[:,-1]) plt.clf() plt.plot( llRange, lfRes ) plt.plot( llRange, lfGuess ) plt.title( 'Average error on average returns of %.04lf'%fAvgRets ) plt.legend( ('Learner Predict', 'Average Return Predict') ) plt.xlabel('K value') plt.ylabel('Error') plt.show() plt.savefig( 'FeatureTest.png', format='png' )
def learnerTest(naTrain, naTest): #create the learner with the train set with K=5 cLearn = ftu.createKnnLearner(naTrain, lKnn=5) #get the Y values predicted by the learner Ypredicted = cLearn.query(naTest[:, :-1]) #get the actual Y values Y = naTest[:, -1] #calculate the correlation coefficient corrcoef = np.corrcoef(Y, Ypredicted)[0][1] #return the corrcoef return corrcoef
def learnerTest( naTrain, naTest ): #create the learner with the train set with K=5 cLearn = ftu.createKnnLearner( naTrain, lKnn=5 ) #get the Y values predicted by the learner Ypredicted = cLearn.query( naTest[:,:-1] ) #get the actual Y values Y = naTest[:,-1] #calculate the correlation coefficient corrcoef = np.corrcoef(Y,Ypredicted)[0][1] #return the corrcoef return corrcoef