Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package smltk.linearmodel
  2.  
  3. import breeze.linalg._
  4.  
  5. trait LinearModel {
  6.  
  7. /** The weights of the model. This is in fact the model*/
  8. var weights = DenseVector[Double]()
  9.  
  10. /** The number of samples in the dataset */
  11. var nSamples = 0
  12.  
  13. /** The number of features in the dataset */
  14. var nFeats = 0
  15.  
  16. /** This function computes the weights of the model
  17. * @param X the datam atrix
  18. * @param y the targets
  19. *
  20. * @return the computed weights. It is also stored as an instance variable
  21. */
  22. def fit(X: DenseMatrix[Double], y: DenseVector[Double]): DenseVector[Double]
  23.  
  24. /** This function computes the prediction of a single instance of data
  25. * @param x a row vector which is also a single instance
  26. *
  27. * @return a real valued number which is the prediction for the argument instance
  28. */
  29. def predict(x: Transpose[DenseVector[Double]]): Double
  30.  
  31. /** This function computes the predictions for multiple instances
  32. * @param X input matrix, each row is a single instance
  33. *
  34. * @return a column vector of predicted values
  35. */
  36. def predict(X: DenseMatrix[Double]): DenseVector[Double] = {
  37. val results = for (i <- 0 until X.rows) yield predict(X(i, ::))
  38. DenseVector[Double](results.toArray)
  39. }
  40.  
  41. /** This function computes the Mean Squared Error for this regressor
  42. *
  43. * @param yTrue the true values
  44. * @param yPreds the predictions
  45. *
  46. * @return the Mean Squared Error \sum_i (y_i - \hat{y_i})^2
  47. */
  48. def score(X: DenseMatrix[Double], yTrue: DenseVector[Double]): Double = {
  49. var mse = 0.0
  50. val yPreds = predict(X)
  51. for (i <- 0 until yTrue.size) mse += math.pow(yTrue(i) - yPreds(i), 2)
  52. mse
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement