«

»

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;
    }

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>