svdX = [] svdY = [] u, s, vT = scipy.linalg.svd(train) for k in xrange(1, 100): low_s = [s[i] for i in xrange(k)] # + (min(u.shape[0], vT.shape[1]) - k) * [0] print 'Exact SVD with low-rank approximation {}'.format(k) svdX.append(k) svdY.append(get_error(u, np.diag(low_s), vT, train, test)) plt.plot(svdX, svdY, label="SVD", color='black', linewidth='2', linestyle='--') """ print print 'Testing incremental SVD' for num in xrange(400, 1001, 300): print '... with block size of {}'.format(num) X, Y = [], [] for k in xrange(1, 91, 10): print k u, s, vT = incremental_SVD(train, k, num) X.append(k) Y.append(get_error(u, s, vT, train, test, prod_avg)) plt.plot(X, Y, label='iSVD u={}'.format(num)) ## plt.title('Recommendation system RMSE on {}x{} matrix'.format(*train.shape)) plt.xlabel('Low rank approximation (k)') plt.ylabel('Root Mean Squared Error') #plt.ylim(0, max(svdY)) plt.legend(loc='best') plt.savefig('recommend_rmse_{}x{}.pdf'.format(*train.shape)) plt.show(block=True)
svdY = [] u, s, vT = scipy.linalg.svd(train) for k in xrange(1, 100): low_s = [s[i] for i in xrange(k)] # + (min(u.shape[0], vT.shape[1]) - k) * [0] print 'Exact SVD with low-rank approximation {}'.format(k) svdX.append(k) svdY.append(get_error(u, np.diag(low_s), vT, train, test)) plt.plot(svdX, svdY, label="SVD", color='black', linewidth='2', linestyle='--') """ print print 'Testing incremental SVD' for num in xrange(400, 1001, 300): print '... with block size of {}'.format(num) X, Y = [], [] for k in xrange(1, 91, 10): print k u, s, vT = incremental_SVD(train, k, num) X.append(k) Y.append(get_error(u, s, vT, train, test, prod_avg)) plt.plot(X, Y, label='iSVD u={}'.format(num)) ## plt.title( 'Recommendation system RMSE on {}x{} matrix'.format(*train.shape)) plt.xlabel('Low rank approximation (k)') plt.ylabel('Root Mean Squared Error') #plt.ylim(0, max(svdY)) plt.legend(loc='best') plt.savefig('recommend_rmse_{}x{}.pdf'.format(*train.shape)) plt.show(block=True)
#print err #print svdX.append(k) svdY.append(err) orthoX.append(k) orthoY.append(check_orthogonality(u)) plt.plot(svdX, svdY, label="SVD", color='black', linewidth='2', linestyle='--') print print 'Testing incremental SVD' incr_ortho = [] for num in xrange(100, 1001, 300): print '... with block size of {}'.format(num) X, Y = [], [] incr_orthoY = [] uL, sL, vTL = incremental_SVD(train, range(1, 101), num) for i in xrange(len(uL)): reconstruct = uL[i].dot(sL[i].dot(vTL[i])) err = np.linalg.norm(train - reconstruct, 'fro') X.append(i + 1) Y.append(err) incr_orthoY.append(check_orthogonality(uL[i])) incr_ortho.append(['iSVD u={}'.format(num), X, incr_orthoY]) plt.plot(X, Y, label='iSVD u={}'.format(num)) """ print 'Testing raw SVD => exact reconstruction' svT = scipy.linalg.diagsvd(s, u.shape[0], vT.shape[1]).dot(vT) for y in xrange(train.shape[0]): for x in xrange(train.shape[1]): colU = u[y, :] rowV = svT[:, x]