Example #1
0
def run(F, X, y):
    # Your code goes here
    S = []
    J = [i for i in range(0, len(X[0]))]  # no of features
    for f in range(1, F + 1):
        # get list of features to iterate on.
        f_iter = []
        for t in J:
            if not (t in S):
                f_iter.append(t)
        best_j = None
        min_error = float("inf")
        for j in f_iter:
            # get X_ = x_{t,sUj}
            S_t = [i for i in S]
            S_t.append(j)
            X_ = X[:, S_t]
            theta_s_u_j = linreg.run(X_, y)

            # use theta_s_u_j to get training error

            error = (0.5) * np.sum((y - np.dot(X_, theta_s_u_j))**2)
            if error < min_error:
                min_error = error
                best_j = j

        S.append(best_j)

    #thetaS
    X_ = X[:, S]
    thetaS = linreg.run(X_, y)
    return (np.asarray(S).reshape(F, 1), thetaS)
def run(F,X,y):
    n=len(X)
    d=len(X[0])
    S=[]
    for f in range(1, F+1):
        MP=dict()
        for j in range(d):
            if j not in S:
                tmpS=S+[j]
                thetaS=linreg.run(X[:, tmpS], y)
                J=SUM(X[:, tmpS], y, thetaS)
                MP.setdefault(J, j)
        S+=[MP[min(MP.keys())]]
    return (np.asarray(S).reshape((F, 1)), linreg.run(X[:, S], y))
Example #3
0
def run(B, X, y):
    n = len(X)
    d = len(X[0])
    z = np.zeros((B, 1))
    for i in range(0, B):
        u = [0] * n
        S = []
        for j in range(0, n):
            k = np.random.randint(n)
            u[j] = k
            if not (k in S):
                S.append(k)
        Se = []
        for k in range(0, n):
            Se.append(k)
        T = list(set(Se) - set(S))
        thetahat = linreg.run(X[u, :], y[u, :])
        thetahat = thetahat.T
        thetahat = np.reshape(thetahat, (np.product(thetahat.shape),))
        sumi = 0
        for t in T:
            sumi = sumi + (y[t][0] - np.dot(thetahat, X[t])) ** 2
        z[i][0] = sumi / len(T)

    return z
Example #4
0
def run(F, X, y):
    n = len(X)
    d = len(X[0])
    S = []
    thetaS = np.zeros((F, 1))
    tT = np.zeros((d, 1))
    for f in range(0, F):
        z = np.zeros(n)
        tx = []
        for j in S:
            tx.append(j)
        for t in range(0, n):
            z[t] = y[t] - np.dot(np.squeeze(tT[tx]), np.squeeze(X[t, tx]))
        DJ = np.zeros(d)
        for j in range(0, d):
            if j not in S:
                for t in range(0, n):
                    DJ[j] -= z[t] * X[t][j]
        jp = 0
        tmpS = 0
        for i in range(0, d):
            if (jp < abs(DJ[i]) and i not in S):
                jp = abs(DJ[i])
                tmpS = i
        tT[tmpS] = linreg.run(X[:, [tmpS]], z)
        S.append(tmpS)
    for i in range(0, F):
        thetaS[i] = tT[S[i]]
    return (S, thetaS)
def run(B, X, y):
    n, d = X.shape
    z = np.zeros((B, 1))
    for i in range(B):
        u = [0] * n
        S = set()
        for j in range(n):
            k = np.random.randint(n)
            u[j] = k
            S.add(k)

        yU = np.zeros((n, 1))
        XU = np.zeros((n, d))
        for j in range(n):
            yU[j] = y[u[j]]
            XU[j] = X[u[j]]

        T = set(range(n)) - S
        thetaHat = linreg.run(XU, yU)

        summ = 0
        for t in T:
            summ += (y[t] - np.dot(X[t], thetaHat))**2
        z[i] = (1.0 / len(T)) * summ

    return z
def run(F, X, y):
    n = len(X)
    d = len(X[0])
    S = []
    thetaS = np.zeros((F, 1))
    tT = np.zeros((d, 1))
    for f in range(0, F):
        J = np.ones((d, 1))
        J = -J
        z = np.zeros(n)
        tx = []
        for i in S:
            tx.append(i)
        for t in range(0, n):
            z[t] = y[t] - np.dot(np.squeeze(tT[tx]), np.squeeze(X[t, tx]))
        for j in range(0, d):
            if j not in S: tT[j] = linreg.run(X[:, [j]], z)
            for t in range(0, n):
                J[j] += (z[t] - np.dot(tT[j], X[t, j]))**2 / 2
        jp = 100000
        tmpS = 0
        for i in range(0, d):
            if (J[i] > 0 and jp > J[i]):
                jp = J[i]
                tmpS = i
        S.append(tmpS)
    for i in range(0, F):
        thetaS[i] = tT[S[i]]
    return (S, thetaS)
Example #7
0
def predict():
    x = float(request.args.get('x')) / 100
    y = float(request.args.get('y')) / 100
    z = float(request.args.get('z')) / 100
    year = request.args.get('year')  # TODO

    X = np.array([x, y, z])

    # Round to three decimal places, convert to string, and return
    return str(round(linreg.run(X, year), 3))
Example #8
0
def run(F, X, y):
    setS = set()
    finalS = []
    n, d = X.shape
    completeSet = set(range(d))

    for f in range(1, F + 1):

        J = {}
        for j in completeSet - setS:
            # Current set to test: S with j
            SJ = setS.copy()
            SJ.add(j)
            # Build matrix X with only features in set SJ
            # This will let us do the linear regression with ony those features
            tempX = np.delete(X, list(completeSet - SJ), 1)
            # Obtain beta vector (argmin)
            thetaSJ = linreg.run(tempX, y)
            # Build summatory for the beta vector
            # This will result in min, as the beta vector is the argmin
            summ = 0
            for t in range(n):
                summ += (y[t] - np.dot(tempX[t], thetaSJ))**2
            # Store minimization for this current set
            J[j] = 0.5 * summ
        # Obtain j with bext minimization
        jHat, _ = min(J.items(), key=lambda x: x[1])
        # Adds J to final set S
        setS.add(jHat)
        finalS.append(jHat)
    # Build matrix X with only features in final set S
    # This will let us do the linear regression with ony those features
    tempX = np.delete(X, list(completeSet - setS), 1)
    # Compute linear regresion for final set
    thetaS = linreg.run(tempX, y)
    # Change S format to numpy vector
    S = np.reshape(finalS, (F, 1))
    return (S, thetaS)
def run(F,X,y):
    n, d = X.shape
    S = set()
    finalS = []
    completeSet = set(range(d))
    thetaS = np.array([])
    z = np.zeros((n, 1))
    for _ in range(F):
        # Build X matrix for only the features in set S => delete values from complete set - S
        XS = np.delete(X,list(completeSet-S),1)
        for t in range(n):
            dot = np.dot(XS[t], thetaS)
            z[t] = y[t] - dot
        
        J = {}
        for j in completeSet-S:
            # Obtain beta vector (argmin).
            # Convert vector of features j into a proper two dimensional array
            thetaJ =  linreg.run(np.reshape(X[:,j], (n,1)), z)
            # Build summatory for the beta vector
            # This will result in min, as the beta vector is the argmin
            summ = 0
            for t in range(n):
                summ += (z[t] - thetaJ*X[t][j])**2
            # Store minimization for this current set
            J[j] = (0.5 * summ[0][0], thetaJ)
            #print "thetaJ candidate", thetaJ
        
        # Obtain j with bext minimization (And its corresponding thetaJ)
        jHat, (_, thetaJ) = min(J.items(), key=lambda x:x[1][0])
        # print "MIN", jHat
        # print "minTheta", thetaJ
        # Constructs final thetaS
        if len(thetaS) == 0:
            thetaS = thetaJ
        else:
            thetaS = np.block([
                [thetaS],
                [thetaJ]
            ])
        #print "thetaS", thetaS
        # Adds J to final set S
        S.add(jHat)
        finalS.append(jHat)
        
    # Reshape set as numpy list
    S = np.reshape(finalS, (F, 1))
    return (S, thetaS)
Example #10
0
def run(F,X,y):
    n, d = X.shape
    S = set()
    finalS = []
    completeSet = set(range(d))
    thetaS = np.array([])
    z = np.zeros((n, 1))
    for _ in range(F):
        # Build X matrix for only the features in set S => delete values from complete set - S
        XS = np.delete(X,list(completeSet-S),1)
        for t in range(n):

            dot = np.dot(XS[t], thetaS)
            z[t] = y[t] - dot
        
        J = {}
        for j in completeSet-S:
            summ = 0
            for t in range(n):
                summ += (z[t]*X[t][j])
            # Store minimization for this current set
            J[j] = abs(-summ[0])
            #print "j candidate", j, "->", -summ[0]
        
        # Obtain j with bext minimization (And its corresponding thetaJ)
        jHat, _ = max(J.items(), key=lambda x:x[1])
        # print "MAX ", jHat
        # Compute weight for selected jHat
        thetaJ = linreg.run(np.reshape(X[:,jHat], (n,1)), z)
        # print "maxTheta", thetaJ
        # Constructs final thetaS
        if len(thetaS) == 0:
            thetaS = thetaJ
        else:
            thetaS = np.block([
                [thetaS],
                [thetaJ]
            ])
        #print "thetaS", thetaS
        # Adds J to final set S
        S.add(jHat)
        finalS.append(jHat)
        
    # Reshape set as numpy list
    S = np.reshape(finalS, (F, 1))
    return (S, thetaS)
Example #11
0
def run(F, X, y):
    # Your code goes here
    S = []
    theta_s = np.empty((0, 0))
    # print theta_s
    J = [i for i in range(0, len(X[0]))]  # no of features
    for f in range(1, F + 1):
        # calculate z
        X_s = X[:, S]
        if len(S) == 0:
            z = np.copy(y)
        else:
            z = y - np.dot(X_s, theta_s)

        # get list of features to iterate on.
        f_iter = []
        for t in J:
            if not (t in S):
                f_iter.append(t)

        best_j = None
        max_effect = float("-inf")
        best_theta_j = None
        for j in f_iter:
            # get X_ = x_{t,j}
            X_j_temp = X[:, j]
            X_j = X_j_temp.reshape(len(X_j_temp), 1)

            effect = (-1) * np.asscalar(np.dot(z.T, X_j))
            effect = abs(effect)
            if (effect > max_effect):
                max_effect = effect
                best_j = j
                best_X_j = X_j

        best_X_j_temp = X[:, best_j]
        best_X_j = best_X_j_temp.reshape(len(best_X_j_temp), 1)
        best_theta_j = linreg.run(best_X_j, z)
        S.append(best_j)
        # update thetaS with concatenation of best_theta_j
        theta_s = np.append(theta_s, best_theta_j)
        theta_s = theta_s.reshape(len(theta_s), 1)

    return (np.asarray(S).reshape(F, 1), theta_s)
Example #12
0
def run(F, X, y):
    # Your code goes here
    S = []
    theta_s = np.empty((0, 0))
    # print theta_s
    J = [i for i in range(0, len(X[0]))]  # no of features
    for f in range(1, F + 1):
        # calculate z
        X_s = X[:, S]
        if len(S) == 0:
            z = np.copy(y)
        else:
            z = y - np.dot(X_s, theta_s)

        # get list of features to iterate on.
        f_iter = []
        for t in J:
            if not (t in S):
                f_iter.append(t)
        best_j = None
        min_error = float("inf")
        best_theta_j = None
        for j in f_iter:
            # get X_ = x_{t,j}
            X_j_temp = X[:, j]
            X_j = X_j_temp.reshape(len(X_j_temp), 1)

            theta_j = linreg.run(X_j, z)

            # use theta_j to get training error

            error = (1 / 2.0) * np.sum((z - np.dot(X_j, theta_j))**2)
            if error < min_error:
                min_error = error
                best_j = j
                best_theta_j = theta_j

        S.append(best_j)
        # update thetaS with concatenation of best_theta_j
        theta_s = np.append(theta_s, best_theta_j)
        theta_s = theta_s.reshape(len(theta_s), 1)

    #thetaS
    return (np.asarray(S).reshape(F, 1), theta_s)
def run(k, X, y):
    n, _ = X.shape
    z = np.zeros((k, 1))
    for i in range(k):
        T = set(
            range(int(np.floor((n * i) / k)),
                  int(np.floor(((n * (i + 1)) / k) - 1)) + 1))
        S = set(range(0, n)) - T

        tList = list(T)
        tList.sort()
        SX = np.delete(X, tList, 0)
        Sy = np.delete(y, tList, 0)
        thetaHat = linreg.run(SX, Sy)

        summ = 0
        for t in T:
            summ += (y[t] - np.dot(X[t], thetaHat))**2
        z[i] = (1.0 / len(T)) * summ
    return z