Jun 28

Linear Regression in C++

The following is the gradient descent algorithm implemented for linear regression using C++ and the Eigen library:

    // Gradient Descent
    double numTrain = double(totalPayVec.size());
    double learnParam = .0000002;
    int numIter = 50000;
    for (int ii = 0; ii < numIter; ii++)
        // Iterate the parameter vector
        pVec = pVec - ((learnParam/(numTrain))*(((xVec * pVec) - yVec).transpose() * xVec).transpose());
        // Calculate and output the Cost function value for each iteration 
        MatrixXd sumCost = (((xVec * pVec) - yVec).transpose()) * ((xVec * pVec) - yVec);
        double costFuncVal = (1/(2.0 * numTrain)) * sumCost(0);
        std::cout << "Iteration: "<< ii << " " << "Cost Function Value: " << costFuncVal << std::endl;


  1. studienkredit gekündigt

    haha. i had to read up on singapore’s local music scene for an essay that i decided to write on my chosen major.. and i read about sth like that too. from this book that marcus lent me. haha.

    1. Bettie

      Yo tengo un poco de terror a los cortatramas desde que me pegué un tajo con uña incluida y me tuvieron que poner putos y todo :O Necesitaría unos guantes de piel de dragón para hacer los sellos con traiduilndaq. POr ahora prefiero las gubias

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>