<commit_before>package breeze.stats.regression

import breeze.generic.UFunc
import breeze.linalg._
import org.netlib.util.intW
import com.github.fommil.netlib.LAPACK.{getInstance=>lapack}
import java.util.Arrays
import spire.implicits.cfor

private case class LassoCalculator(data: DenseMatrix[Double], outputs: DenseVector[Double], lambda: Double, workArray: Array[Double], MAX_ITER: Int=100, IMPROVE_THRESHOLD: Double=1e-8) {
  /*
   * The main purpose of this complicated calculator object is to recycle all the assorted work arrays.
   * If we didn't write it this way, we'd have to manually thread all the work arrays
   * throughout a slew of functions.
   */
  require(data.rows == outputs.size)
  require(data.rows > data.cols)
  require(data.rows == outputs.size)
  require(workArray.size >= 2*data.rows*data.cols)

  private val outputCopy = new DenseVector[Double](new Array[Double](outputs.size))
  private val singleColumnMatrix = new DenseMatrix[Double](data.rows, 1)
  private val resultVec = new DenseVector[Double](new Array[Double](data.cols))

  lazy val result: LassoResult = {

    var improvedResult = true
    var iter = 0

    while (improvedResult && (iter < MAX_ITER)) {
      iter += 1
      improvedResult = false
      cfor(0)(i => i<data.cols, i=>i+1)(i => {
        val eoc = estimateOneColumn(i)
        val oldCoefficient = resultVec.unsafeValueAt(i)
        resultVec.unsafeUpdate(i, shrink(eoc.coefficients(0)))
        if (oldCoefficient != resultVec.unsafeValueAt(i)) {
          improvedResult = true
        }
      })
    }

    LassoResult(resultVec, computeRsquared, lambda)
  }

  private def shrink(x: Double): Double = {
    // Soft thresholding
    val sb = math.signum(x)
    val ab = sb*x
    if (ab > lambda) {
      sb*(ab-lambda)
    } else {
      0.0<commit_msg>idiomatic style<commit_after>package breeze.stats.regression

import breeze.generic.UFunc
import breeze.linalg._
import org.netlib.util.intW
import com.github.fommil.netlib.LAPACK.{getInstance=>lapack}
import java.util.Arrays
import spire.implicits.cfor

private case class LassoCalculator(data: DenseMatrix[Double], outputs: DenseVector[Double], lambda: Double, workArray: Array[Double], MAX_ITER: Int=100, IMPROVE_THRESHOLD: Double=1e-8) {
  /*
   * The main purpose of this complicated calculator object is to recycle all the assorted work arrays.
   * If we didn't write it this way, we'd have to manually thread all the work arrays
   * throughout a slew of functions.
   */
  require(data.rows == outputs.size)
  require(data.rows > data.cols)
  require(data.rows == outputs.size)
  require(workArray.size >= 2*data.rows*data.cols)

  private val outputCopy = DenseVector.zeros[Double](outputs.size)
  private val singleColumnMatrix = new DenseMatrix[Double](data.rows, 1)
  private val resultVec = DenseVector.zeros[Double](data.cols)

  lazy val result: LassoResult = {

    var improvedResult = true
    var iter = 0

    while (improvedResult && (iter < MAX_ITER)) {
      iter += 1
      improvedResult = false
      cfor(0)(i => i<data.cols, i=>i+1)(i => {
        val eoc = estimateOneColumn(i)
        val oldCoefficient = resultVec.unsafeValueAt(i)
        resultVec.unsafeUpdate(i, shrink(eoc.coefficients(0)))
        if (oldCoefficient != resultVec.unsafeValueAt(i)) {
          improvedResult = true
        }
      })
    }

    LassoResult(resultVec, computeRsquared, lambda)
  }

  private def shrink(x: Double): Double = {
    // Soft thresholding
    val sb = math.signum(x)
    val ab = sb*x
    if (ab > lambda) {
      sb*(ab-lambda)
    } else {
      0.0
{'fraction_non_alphanumeric': 0.11172873818788216, 'fraction_numerical': 0.007782101167315175, 'mean_word_length': 3.465260545905707, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6198803', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1142, 'n_words': 292}