def learn(maxRating, nUsers, nItems, X, Y, testX): assert (len(X) == len(Y)) movieMean = centerMean(X, Y, nItems) trainSize = int(len(X) * trainRatio) trainX, trainY = X[:trainSize], Y[:trainSize] crossvalX, crossvalY = X[trainSize:], Y[trainSize:] learningMethod = methodByName(methodName) recs = [ learningMethod(maxRating, nUsers, nItems, trainX, trainY, L) for L in lRange ] trainErrors = [errors.rmse(recommend(rec, trainX), trainY) for rec in recs] crossvalErrors = [ errors.rmse(recommend(rec, crossvalX), crossvalY) for rec in recs ] plotErrors(lRange, trainErrors, crossvalErrors) bestError, bestL, bestRec = min(zip(crossvalErrors, lRange, recs)) testY = recommend(bestRec, testX) for i in range(len(testX)): testY[i] += movieMean[testX[i][1]] return bestL, testY
def solveOne(setN, setName, ansName): startTime = time() maxRating, nUsers, nItems, trainX, trainY, testX, testY = loadData(setName, ansName) print("Set #{0}: read {1} users, {2} items, {3} train set ratings and {4} test set ratings".format(setN, nUsers, nItems, len(trainX), len(testX))) bestL, predictedRs = learn(maxRating, nUsers, nItems, trainX, trainY, testX) print("Set #{0}: RMSE = {1}, best λ = {2}".format(setN, errors.rmse(predictedRs, testY), bestL)) endTime = time() print("Set #{0}: Leaning done in {1} seconds".format(setN, endTime - startTime))
def learn(maxRating, nUsers, nItems, X, Y, testX): assert (len(X) == len(Y)) movieMean = centerMean(X, Y, nItems) trainSize = int(len(X) * trainRatio) trainX, trainY = X[:trainSize], Y[:trainSize] crossvalX, crossvalY = X[trainSize:], Y[trainSize:] learningMethod = methodByName(methodName) recs = [learningMethod(maxRating, nUsers, nItems, trainX, trainY, L) for L in lRange] trainErrors = [errors.rmse(recommend(rec, trainX), trainY) for rec in recs] crossvalErrors = [errors.rmse(recommend(rec, crossvalX), crossvalY) for rec in recs] plotErrors(lRange, trainErrors, crossvalErrors) bestError, bestL, bestRec = min(zip(crossvalErrors, lRange, recs)) testY = recommend(bestRec, testX) for i in range(len(testX)): testY[i] += movieMean[testX[i][1]] return bestL, testY
def solveOne(setN, setName, ansName): startTime = time() maxRating, nUsers, nItems, trainX, trainY, testX, testY = loadData( setName, ansName) print( "Set #{0}: read {1} users, {2} items, {3} train set ratings and {4} test set ratings" .format(setN, nUsers, nItems, len(trainX), len(testX))) bestL, predictedRs = learn(maxRating, nUsers, nItems, trainX, trainY, testX) print("Set #{0}: RMSE = {1}, best λ = {2}".format( setN, errors.rmse(predictedRs, testY), bestL)) endTime = time() print("Set #{0}: Leaning done in {1} seconds".format( setN, endTime - startTime))