Sort by

recency

|

105 Discussions

|

  • + 0 comments

    Roh come on ..i didnt know i could use :

    from sklearn import linear_model
    or
    import numpy as np
    

    We couldnt do it in previous challenge ... ...

    def transpose(mat):
        return [list(row) for row in zip(*mat)]
    
    def inverse(mat):
        size = len(mat)
        identity = [[1 if i == j else 0 for j in range(size)] for i in range(size)]
        augmented = [row[:] + identity_row for row, identity_row in zip(mat, identity)]
        for i in range(size):
            pivot = augmented[i][i]
            for j in range(size * 2):
                augmented[i][j] /= pivot
            for k in range(size):
                if k != i:
                    factor = augmented[k][i]
                    for j in range(size * 2):
                        augmented[k][j] -= factor * augmented[i][j]
        return [row[size:] for row in augmented]
    
    def matrix(X, Y):
        return [[sum(x * y for x, y in zip(X_row, Y_col)) for Y_col in zip(*Y)] for X_row in X]
    
    def linear_reg(X, Y):
        tx = transpose(X)
        xty = matrix(tx, Y)
        xtx = matrix(tx, X)
        inv_xtx = inverse(xtx)
        return [item for sublist in matrix(inv_xtx, xty) for item in sublist]
    
    m, n = map(int, input().split(" "))
    XY = [list(map(float, input().split(" "))) for _ in range(n)]
    X = [[1] + x[:m] for x in XY]
    Y = [[x[m]] for x in XY]
    q = int(input())
    qX = [list(map(float, input().split(" "))) for _ in range(q)]
    NX = [[1] + x[:m] for x in qX]
    
    b = linear_reg(X, Y)
    
    for nx in NX:
        y = sum(bi * fi for bi, fi in zip(b, nx))
        print(f"{y:.2f}")
    
  • + 0 comments
    from sklearn import linear_model
    
    X = []
    y = []
    shape = list(map(int, input().split()))
    new_X = []
    
    for _ in range(shape[1]):
        parts = list(map(float, input().split()))
        X.append(parts[:shape[0]])
        y.append(parts[-1])
        
    for _ in range(int(input())):
        new_X.append(list(map(float, input().split())))
        
    lm = linear_model.LinearRegression()
    lm.fit(X, y)
    y_hat = lm.predict(new_X)
    for y in y_hat:
        print(f'{y:.2f}')
    
  • + 0 comments
    #python
    import numpy as np
    m, n=map(int, input().split())
    Y, X, res=[], [], []
    for _ in range(n):
        temp=list(map(float, input().split()))
        Y.append(temp[-1])
        temp.pop()
        X.append([1.0] + temp)
    Xtranspose=np.transpose(X)
    coeffset=np.dot(np.dot(np.linalg.inv(np.dot(Xtranspose, X)), Xtranspose), Y)
    q=int(input())
    for x in range(q):
        s=coeffset[0]
        temp=list(map(float, input().split()))
        for i in range(m):
            s+=temp[i]*coeffset[i+1]
        res.append(s)
    print(*res, sep='\n')
    
  • + 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 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;
        }