Skip to content

Instantly share code, notes, and snippets.

@contropist
Forked from lidangzzz/linearRegression.hhs
Created August 18, 2020 18:12
Show Gist options
  • Save contropist/5a8d646ed089c0651d1d6dea6e3d71fa to your computer and use it in GitHub Desktop.
Save contropist/5a8d646ed089c0651d1d6dea6e3d71fa 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