Skip to content

Instantly share code, notes, and snippets.

@lidangzzz
Last active August 18, 2020 18:12
Show Gist options
  • Save lidangzzz/a14456264724a92b5a1147036537e531 to your computer and use it in GitHub Desktop.
Save lidangzzz/a14456264724a92b5a1147036537e531 to your computer and use it in GitHub Desktop.
linearRegression.hhs
class LinearRegression {
w: Mat;
constructor() { }
//x: M-by-N matrix. M data with N dimensions. Each row is an N-dim vector
//y: M-by-1 matrix
fit(x_: Mat, y_: Mat) : LinearRegression{
let y = y_;
if (y_.rows != 1 && y_.cols == 1) {y = y_.T();} //check the dimension of y
var x = x_.resize(x_.rows, x_.cols + 1, 1); //expan x_ with one more column with 1
this.w = ( (X.T() * X)^-1 ) * X.T() * y //calculate w = (X.T() * X)^-1 * X.T() * y
return this;
}
//x: M-by-N matrix. M data with N dimensions. Each row is an N-dim vector
//
predict(x_: Mat):mat {
let x = x_.resize(x_.rows, x_.cols + 1, 1); //expan x_ with one more column with 1
return x*(this.w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment