Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add Regularized Linear Regression
  • Loading branch information
miladnouriezade committed Aug 24, 2020
commit 97a71157f038f895a5be307453aa72b841cac336
Binary file modified .DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

**New Update**

- Added Regularized Linear Regression .

> Further reading : [Overfitting and Regularization](https://machinelearningmedium.com/2017/09/08/overfitting-and-regularization/)

- Added [YTS Movies](https://www.kaggle.com/miladnourizade/ytsyifytorrent-movies) which is collected from official YIFY torrent movies(yts.am).

> YTS movies dataset is available just for content based recommendation .
> YTS movies dataset is available just for content based recommendation .

A recommender system in JavaScript built with NodeJs. It uses the popular [MovieLens](https://www.kaggle.com/rounakbanik/the-movies-dataset/data) database which includes information about movies and ratings of users. The recommender system implements the following recommendation strategies:

Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
23 changes: 18 additions & 5 deletions src/strategies/linearRegression.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import math from 'mathjs';
import { sortByScore } from './common';

const LEARNING_RATE = 0.03;
const LAMBDA = 0.01;
const LEARNING_ITERATIONS = 750;

function predictWithLinearRegression(X, MOVIES_IN_LIST, ratings) {
Expand Down Expand Up @@ -46,6 +47,7 @@ function predictWithLinearRegression(X, MOVIES_IN_LIST, ratings) {
training.y,
theta,
LEARNING_RATE,
LAMBDA,
LEARNING_ITERATIONS
);

Expand All @@ -62,20 +64,21 @@ function predictWithLinearRegression(X, MOVIES_IN_LIST, ratings) {
return sortByScore(predictedRatings);
}

export function gradientDescent(X, y, theta, ALPHA, ITERATIONS) {
export function gradientDescent(X, y, theta, ALPHA, LAMBDA, ITERATIONS) {
const m = y.length;

for (let i = 0; i < ITERATIONS; i++) {
theta = math.eval(`theta - ALPHA / m * ((X * theta - y)' * X)'`, {
theta = math.eval(`theta * (1 - (ALPHA * LAMBDA / m)) - ALPHA / m * ((X * theta - y)' * X)'`, {
theta,
ALPHA,
LAMBDA,
m,
X,
y,
});

if (i % 50 === 0) {
const cost = computeCost(X, y, theta);
const cost = computeCost(X, y, theta, LAMBDA);
console.log(`Cost after ${i} of trained ${ITERATIONS}: ${cost}`);
}
}
Expand All @@ -91,7 +94,7 @@ export function getPredictedRatings(theta, X) {
})
}

export function computeCost(X, y, theta) {
export function computeCost(X, y, theta, LAMBDA) {
let m = y.length;

let predictions = math.eval('X * theta', {
Expand All @@ -104,9 +107,19 @@ export function computeCost(X, y, theta) {
y,
});

let J = math.eval(`1 / (2 * m) * sum(sqrErrors)`, {
let sqrTheta = math.eval('(theta).^2', {
theta
});
let thetaZero = theta[0]
sqrTheta[0] = thetaZero


let J = math.eval(`1 / (2 * m) * (sum(sqrErrors) + LAMBDA * (sum(sqrTheta) - thetaZero))`, {
m,
sqrErrors,
LAMBDA,
sqrTheta,
thetaZero
});

return J;
Expand Down