-
-
Save contropist/5a8d646ed089c0651d1d6dea6e3d71fa to your computer and use it in GitHub Desktop.
linearRegression.hhs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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