Last active
August 18, 2020 18:12
-
-
Save lidangzzz/a14456264724a92b5a1147036537e531 to your computer and use it in GitHub Desktop.
linearRegression.hhs
This file contains hidden or 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