We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 10 Days of Statistics
  4. Day 9: Multiple Linear Regression
  5. Discussions

Day 9: Multiple Linear Regression

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 102 Discussions, By:

recency

Please Login in order to post a comment

  • lucasopoka
    4 months ago+ 0 comments
    from sklearn import linear_model
    from numpy import multiply as mult
    m,n = map(int, input().split())
    lst_features, lst_y = [], []
    
    for _ in range(n):
        *features, y = map(float, input().split())
        lst_features.append(features)
        lst_y.append(y)
    
    lm = linear_model.LinearRegression()
    lm.fit(lst_features, lst_y)
    a = lm.intercept_
    b = lm.coef_
    
    for _ in range(int(input())):
        feature_set = list(map(float, input().split()))
        mult_sum = sum(mult(b, feature_set))
        print(round(a + mult_sum, 2))
    
    0|
    Permalink
  • sabsfilho
    9 months ago+ 0 comments

    c# version

        static void Main(String[] args) {
            var xs = System.Console.ReadLine().Trim().Split(' ');
            int m = int.Parse(xs[0]) + 1;
            int n = int.Parse(xs[1]);
            var ws = new double[n,m]; /* row,col */
            for(int i = 0; i < n; i++)
            {
                var zs = System.Console.ReadLine().Trim().Split(' ');
                for (int k=0;k<m;k++)
                {
                    ws[i,k] = double.Parse(zs[k]);
                }
            }
            var bs = GetBs(ws, n, m);
            var q = int.Parse(System.Console.ReadLine().Trim());
            for(int i = 0; i < q; i++)
            {
                var zs = System.Console.ReadLine().Trim().Split(' ');
                var qs = new double[1,m];
                qs[0,0] = 1;
                for (int k=0;k<(m-1);k++)
                {
                    qs[0,k+1] = double.Parse(zs[k]);
                }
                var qsbs = Product(qs, bs);
                System.Console.WriteLine(qsbs[0,0].ToString("f2"));
            }
        }
    
        static double[,] GetBs(double[,] ws, int row, int col)
        {
            var xs = new double[row, col];
            var ys = new double[row, 1];
            int coly = col - 1;
            for(int i = 0; i < row; i++)
            {
                xs[i, 0] = 1;
                ys[i, 0] = ws[i, coly];
                for(int j = 0; j < coly; j++)
                {
                    xs[i, j + 1] = ws[i, j];            
                }
            }
            var txs = Transpose(xs);
            var txsxs = Product(txs, xs);
            var txsxsInv = Inverse(txsxs);
            var txsxsInvtxs = Product(txsxsInv, txs);
            var txsxsInvtxsys = Product(txsxsInvtxs, ys);
            return txsxsInvtxsys;
        }
    	/* optimization cache */
        static Dictionary<string, double> DeterminantDic = new Dictionary<string, double>();
        static double Determinant(double[,] xs)
        {
            var pkey = MatrixKey(xs);
            double mx;
            if (DeterminantDic.TryGetValue(pkey, out mx))
            {
                return mx;
            }
            double det = 0;
            int r = xs.GetUpperBound(0) + 1;
            if (r == 2)
            {  
                det = xs[0,0] * xs[1,1] - xs[0,1] * xs[1,0];
            }
            else
            {
                int c = xs.GetUpperBound(1) + 1;
                for(int i=0;i<c;i++)
                {
                    det += ((double)(-1.0 * (i % 2 == 0 ? -1.0 : 1.0)) * xs[0, i] * Determinant(SubMatrix(xs, 0, i)));
                }
            }
            DeterminantDic.Add(pkey, det);
            return det;
        }
        static double[,] SubMatrix(double[,] xs, int row, int col)
        {
            int r = xs.GetUpperBound(0);
            int c = xs.GetUpperBound(1);
            var ds = new double[r, c];
            int i2 = 0;
            for(int i = 0; i <= r; i++)
            {
                if (i != row)
                {
                    int j2 = 0;
                    for(int j = 0; j <= c; j++)
                    {
                        if (j != col)
                        {
                            ds[i2, j2] = xs[i,j];
                            j2++;
                        }
                    }
                    i2++;
                }
            }
            return ds;
        }
        static double[,] Inverse(double[,] xs)
        {
            var k = 1.0/Determinant(xs);
            
            int r = xs.GetUpperBound(0) + 1;
            int c = xs.GetUpperBound(1) + 1;
            var ds = new double[r, c];
            for (int i=0; i < r; i++)
            {
                for(int j=0; j < c; j++)
                {
                    ds[i, j] =
                        (-1.0 * (((j + i) % 2 == 0) ? -1.0 : 1.0)) * 
                        Determinant(SubMatrix(xs, i, j)) *
                        k;
                }
            }
            
            return ds;
        }
        static double[,] Product(double[,] xs, double[,] ys)
        {
            int row = xs.GetUpperBound(0) + 1;
            int colx = xs.GetUpperBound(1) + 1;
            int col = ys.GetUpperBound(1) + 1;
            var ds = new double[row, col];
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    double r = 0;
                    for(int k = 0; k < colx; k++)
                    {
                        r += xs[i,k] * ys[k,j];
                    }
                    ds[i,j] = r;            
                }
            }
            return ds;
        }
        static double[,] Transpose(double[,] xs)
        {
            int row = xs.GetUpperBound(0) + 1;
            int col = xs.GetUpperBound(1) + 1;
            var ds = new double[col, row];
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    ds[j, i] = xs[i, j];
                }
            }
            return ds;
        }
        static string MatrixKey(double[,] xs)
        {
            string k = string.Empty;
            int row = xs.GetUpperBound(0) + 1;
            int col = xs.GetUpperBound(1) + 1;
            for(int i = 0; i < row; i++)
            {
                for(int j = 0; j < col; j++)
                {
                    k = string.Concat(k, k.Length == 0 ? string.Empty : "#", xs[i, j].ToString("f5"));
                }
            }
            return k;
        }
    
    0|
    Permalink
  • SergeyA_Misyura
    1 year ago+ 0 comments

    Solution with without runtime error.

    from sklearn import linear_model
    import pandas as pd
    
    def main():
        m, n = map(int, input().strip().split())
        
        #upackage x, y train
        df_list= [list(map(float, input().split())) for i in range(n)]
        df_train = pd.DataFrame(df_list)
    
        #input number of x_pred feature 
        q = int(input())
        
        #x_pred feature 
        df_list_pred = [list(map(float, input().split())) for i in range(q)]  
        df_pred = pd.DataFrame(df_list_pred)
    
        #train test split
        X_train, y_train = df_train.iloc[:, :m], df_train.iloc[:, m]
        #train
        model = linear_model.LinearRegression().fit(X_train, y_train)
        #predict
        y_pred = model.predict(df_pred)
    
        for num in y_pred:
            print(round(num, 2))
    
    if __name__ == "__main__":
        main()
    
    0|
    Permalink
  • jaroslav_prucha
    2 years ago+ 0 comments

    C++ implementation. This has been a great learning process for me as I'm new to C++ but if you just wish to practice statistics using C++ and not spend two evenings on this problem, I recommend using the Matrix class, which was 90% of my time spent on this problem.

    I calculated the matrix inverse using LU decompozition (which is faster than Gauss elimination method), it's the 'invertLU' method.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    
    class Matrix
    {
    public:
        Matrix(size_t rows, size_t cols);
        double& operator()(size_t i, size_t j);
        double operator()(size_t i, size_t j) const;
        Matrix transpose();
        bool printMatrix();
        vector<Matrix> decompLU();
        int determinant(size_t ignoreCol, size_t ignoreRow);
        Matrix matrixMul(Matrix m);
        Matrix invertLU(Matrix L, Matrix U);
        Matrix invertLU();
        size_t get_mRows();
        size_t get_mCols();
    
    private:
        size_t mRows;
        size_t mCols;
        vector<double> mData;
    };
    
    Matrix::Matrix(size_t rows, size_t cols)
        : mRows(rows), mCols(cols), mData(rows* cols)
    {
    }
    
    size_t Matrix::get_mRows() {
        return mRows;
    }
    size_t Matrix::get_mCols() {
        return mCols;
    }
    
    double& Matrix::operator()(size_t i, size_t j) {
        return mData.at(i * mCols + j);
    }
    
    double Matrix::operator()(size_t i, size_t j) const {
        return mData[i * mCols + j];
    }
    
    Matrix Matrix::transpose() {
        Matrix tM(mCols, mRows);
        for (size_t i = 0; i < mRows; i++) {
            for (size_t j = 0; j < mCols; j++) {
                tM(j, i) = (*this)(i, j);
            }
        }
        return tM;
    }
    
    int determinant(size_t ignoreCol, size_t ignoreRow) {
        return 0;
    }
    
    vector<Matrix> Matrix::decompLU() {
        vector<Matrix> matrixes;
        Matrix l(mRows, mCols);
        Matrix u(mRows, mCols);
        if (mRows != mCols) {
            cout << "Matrix is not square. Can't do LU decompozition" << endl;
            return matrixes;
        }
        size_t i = 0, j = 0, k = 0;
        for (i = 0; i < mRows; i++) {
            for (j = 0; j < mCols; j++) {
                if (j < i)
                    l(j, i) = 0;
                else {
                    l(j, i) = (*this)(j, i);
                    for (k = 0; k < i; k++) {
                        l(j, i) = l(j, i) - l(j, k) * u(k, i);
                    }
                }
            }
            for (j = 0; j < mRows; j++) {
                if (j < i)
                    u(i, j) = 0;
                else if (j == i)
                    u(i, j) = 1;
                else {
                    u(i, j) = (*this)(i, j) / l(i, i);
                    for (k = 0; k < i; k++) {
                        u(i, j) = u(i, j) - ((l(i, k) * u(k, j)) / l(i, i));
                    }
                }
            }
        }
        matrixes.push_back(l);
        matrixes.push_back(u);
    
        return matrixes;
    }
    
    Matrix Matrix::matrixMul(Matrix m) {
        double sum = 0;
        Matrix mulRes(mRows, m.get_mCols());
        for (size_t i = 0; i < mRows; i++) {
            for (size_t j = 0; j < m.get_mCols(); j++) {
                for (size_t k = 0; k < mCols; k++) {
                    sum += (*this)(i, k) * m(k, j);
                }
                mulRes(i, j) = sum;
                sum = 0;
            }
        }
        return mulRes;
    }
    
    bool Matrix::printMatrix() {
        for (size_t i = 0; i < mRows; i++) {
            for (size_t j = 0; j < mCols; j++) {
                std::cout << mData.at(i * mCols + j) << " ";
            }
            std::cout << std::endl;
        }
        return true;
    }
    
    struct LU_matrix
    {
        Matrix L;
        Matrix U;
    };
    
    Matrix Matrix::invertLU(Matrix L, Matrix U) {
        Matrix eigenMatrix(L.get_mRows(), L.get_mCols());
        for (size_t i = 0; i < eigenMatrix.get_mRows(); i++) {
            for (size_t j = 0; j < eigenMatrix.get_mCols(); j++) {
                if (i == j) {
                    eigenMatrix(i, j) = 1;
                }
                else {
                    eigenMatrix(i, j) = 0;
                }
            }
        }
    
        // calculate [L][Z] = [I] -> get three vectors
        Matrix Z(L.get_mRows(), L.get_mCols());
        double s;
        for (size_t Z_col = 0; Z_col < L.get_mCols(); Z_col++) {
            // Iterate for each element (row) in Z
            for (size_t row = 0; row < L.get_mRows(); row++) {
                s = 0;
                // On each row, iterate through existing Z elements
                for (size_t j = 0; j < row; j++) {
                    s += Z(j, Z_col) * L(row, j);
                }
                Z(row, Z_col) = (eigenMatrix(row, Z_col) - s) / L(row, row);
            }
        }
        // calculate [U][X] = [Z] -> get three vectors X, X = inv(A)
        Matrix X(U.get_mRows(), U.get_mCols());
        for (size_t X_col = 0; X_col < U.get_mCols(); X_col++) {
            // Iterate for each element (row) in Z, go from bottom up
            size_t row = 0;
            for (size_t temp_row = 0; temp_row < U.get_mRows(); temp_row++) {
                row = U.get_mRows() - temp_row -1;
            //for (size_t row = U.get_mRows() - 1; row >= 0; row--) {
                s = 0;
                // On each row, iterate through existing Z elements
                for (size_t j = row; j < U.get_mRows(); j++) {
                    s += X(j, X_col) * U(row, j);
                }
                X(row, X_col) = (Z(row, X_col) - s) / U(row, row);
            }
        }
    
        return X;
     }
    
    Matrix Matrix::invertLU() {
        vector<Matrix> LU = (*this).decompLU();
        auto L = LU.at(0);
        auto U = LU.at(1);
        Matrix eigenMatrix(L.get_mRows(), L.get_mCols());
        for (size_t i = 0; i < eigenMatrix.get_mRows(); i++) {
            for (size_t j = 0; j < eigenMatrix.get_mCols(); j++) {
                if (i == j) {
                    eigenMatrix(i, j) = 1;
                }
                else {
                    eigenMatrix(i, j) = 0;
                }
            }
        }
    
        // calculate [L][Z] = [I] -> get three vectors
        Matrix Z(L.get_mRows(), L.get_mCols());
        double s;
        for (size_t Z_col = 0; Z_col < L.get_mCols(); Z_col++) {
            // Iterate for each element (row) in Z
            for (size_t row = 0; row < L.get_mRows(); row++) {
                s = 0;
                // On each row, iterate through existing Z elements
                for (size_t j = 0; j < row; j++) {
                    s += Z(j, Z_col) * L(row, j);
                }
                Z(row, Z_col) = (eigenMatrix(row, Z_col) - s) / L(row, row);
            }
        }
        // calculate [U][X] = [Z] -> get three vectors X, X = inv(A)
        Matrix X(U.get_mRows(), U.get_mCols());
        for (size_t X_col = 0; X_col < U.get_mCols(); X_col++) {
            // Iterate for each element (row) in Z, go from bottom up
            size_t row = 0;
            for (size_t temp_row = 0; temp_row < U.get_mRows(); temp_row++) {
                row = U.get_mRows() - temp_row - 1;
                //for (size_t row = U.get_mRows() - 1; row >= 0; row--) {
                s = 0;
                // On each row, iterate through existing Z elements
                for (size_t j = row; j < U.get_mRows(); j++) {
                    s += X(j, X_col) * U(row, j);
                }
                X(row, X_col) = (Z(row, X_col) - s) / U(row, row);
            }
        }
        return X;
    }
    
            int main() {
                bool p_intercept = true;
                size_t m, n;
                cin >> n >> m;
                double tmp;
                
                if (p_intercept) n++;
    
                Matrix X(m, n);
                Matrix Y(m, 1);
                //load matrix
                for (size_t i = 0; i < m; i++) {
                    for (size_t j = 0; j < n; j++) {
                        if (p_intercept && j == 0) {
                            X(i, j) = 1.0;
                        }
                        else {
                            cin >> tmp;
                            X(i, j) = tmp;
                        }
                    }
                    cin >> tmp;
                    Y(i,0) = tmp;
                }
    
                cin >> m;
                Matrix X_test(m, n);
    
                for (size_t i = 0; i < m; i++) {
                    for (size_t j = 0; j < n; j++) {
                        if (p_intercept && j == 0) {
                            X_test(i, j) = 1.0;
                        }
                        else {
                            cin >> tmp;
                            X_test(i, j) = tmp;
                        }
                    }
                }
    
                auto regressionWeights = ((X.transpose()).matrixMul(X).invertLU()).matrixMul(X.transpose()).matrixMul(Y);
                //cout << "Testing regression weights" << endl;
                //regressionWeights.printMatrix();
    
                auto regressionPredict = X_test.matrixMul(regressionWeights);
    
                //cout << "Testing fitting" << endl;
                regressionPredict.printMatrix();
    
            }
    
    1|
    Permalink
  • zhuohuixin
    2 years ago+ 0 comments

    My solution in python3:

    from sklearn import linear_model
    
    first_input=input()
    m = int(first_input.split()[0])
    n = int(first_input.split()[1])
    
    x_s = []
    y_s = []
    for i in range(n):
        x_n_y = input().split()      
        x_s.append([float(x_n_y[j]) for j in range(m)])
        y_s.append(float(x_n_y[-1]))
    
    lm = linear_model.LinearRegression()
    lm.fit(x_s, y_s)
    a = lm.intercept_
    b = lm.coef_
    b_s = [b[k] for k in range(m)]
    
    q = int(input())
    for l in range(q):
        tests = input().split()
        summed = a 
        for i in range(m):
            summed += b_s[i]*float(tests[i])
        print(summed)
    
    0|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy