public void TestConvergence() { double[,] features = { { 1d, 0d }, { 1d, 1d }, { 1d, 2d }, { 1d, 3d }, { 1d, 4d } }; double[] initialTheta = { 0d, 0d }; double[] expectedTheta = { 1d, -1d }; double[] labels = { 1d, 0d, -1d, -2d, -3d }; double learningRate = .1d; int numberOfIterations = 200; double[] learnedTheta = LinearRegression.Learn(features, labels, initialTheta, learningRate, numberOfIterations); for (int index = 0; index < learnedTheta.Length; index++) { Assert.AreEqual(expectedTheta[index], learnedTheta[index], 0.01d); } }
private void TrainPostFeatures(string postId, double lambda) { var postRepository = _unitOfWork.GetRepository <Post>(); var post = postRepository.Get(p => p.Id == postId) .SingleOrDefault(); var rateRepository = _unitOfWork.GetRepository <Rate>(); var postRates = rateRepository.Get() .Where(r => r.PostId == postId) .Select(r => new { r.Value, r.User.Weights }); var values = postRates.Select(r => (double)r.Value) .ToArray(); var usersWeights = postRates.Select(r => r.Weights) .ToArray(); post.Features = LinearRegression.Learn(usersWeights, values, post.Features, lambda); _unitOfWork.SaveChanges(); }
private void TrainUserWeights(string userId, double lambda) { var userRepostiory = _unitOfWork.GetRepository <User>(); var user = userRepostiory.Get(u => u.Id == userId) .SingleOrDefault(); var rateRepository = _unitOfWork.GetRepository <Rate>(); var userRates = rateRepository.Get() .Where(r => r.UserId == userId) .Select(r => new { r.Value, r.Post.Features }) .ToList(); var values = userRates.Select(r => (double)r.Value) .ToArray(); var postsFeatures = userRates.Select(r => r.Features) .ToArray(); user.Weights = LinearRegression.Learn(postsFeatures, values, user.Weights, lambda); _unitOfWork.SaveChanges(); }