コード例 #1
0
def choose_hyper(sampx, order, sampy, polyx, polyy, alpha, sig2):
    sigma_star, y_prime = do_regression(grand_order(sampx, order),
                                        sampy, polyx, polyy, order, alpha,
                                        float(sig2))
    best_alpha = alpha
    least_mean_error = data_reading.MeanSquareError(y_prime, polyy)
    alphas = linspace(0.1, 11, 100)
    for alp in alphas:
        sigma_star, y_prime = do_regression(grand_order(sampx, order), sampy,
                                            polyx, polyy, order, alp,
                                            float(sig2))
        mean_error = data_reading.MeanSquareError(y_prime, polyy)
        if mean_error <= least_mean_error:
            least_mean_error = mean_error
            best_alpha = alp
    return best_alpha
コード例 #2
0
def choose_hyper(matFi, sampy, polyx, polyy, order):
    least_error = 100
    target_alpha = 1
    alphas = np.linspace(-3, 2, 1000)
    for alp in alphas:
        y_prime = grand_order(polyx, order) * do_regression(matFi, sampy, alp)
        error = data_reading.MeanSquareError(y_prime, polyy)
        if error <= least_error:
            target_alpha = alp
            least_error = error
    return target_alpha
コード例 #3
0
def choose_hyper(phi, y, polyx, polyy, order):
    w = do_regression(phi, y, lambd=10)
    y_prime = (grand_order(polyx, order) * w)

    least_error = 100
    target_alpha = 1
    alphas = np.linspace(-20, -18, 10)
    for alp in alphas:
        w = do_regression(phi, y, alp)
        y_prime = (grand_order(polyx, order) * w)
        error = data_reading.MeanSquareError(y_prime, polyy)
        if error <= least_error:
            target_alpha = alp
            least_error = error
    return target_alpha
コード例 #4
0
    X, y = grand_order(poly_data['sampx'][0], order), poly_data['sampy']
    polyx, polyy = poly_data['polyx'][0], poly_data['polyy']
    count_data, count_keys = data_reading.readMatFile("count_data.mat")
    target_alpha = choose_hyper(X, y, polyx, polyy, order)
    w = do_regression(X, y, target_alpha)
    y_prime = grand_order(polyx, order) * w

    fig = plt.figure("LASSO")
    ax = fig.add_subplot(111)
    ax.plot(poly_data['sampx'][0],
            y,
            color='r',
            linestyle='',
            marker='*',
            label="sample")
    # regression line
    # ax.plot(polyx,y_prime,color='g',linestyle='-',marker='',label="predict")
    ax.plot(polyx,
            y_prime,
            color='g',
            linestyle='-',
            label="lambda = " + str(target_alpha))
    print "least mean error: " + str(
        data_reading.MeanSquareError(y_prime,
                                     polyy)) + " at " + str(target_alpha)

    # poly points
    ax.plot(polyx, polyy, color='b', linestyle='-', marker='', label="target")
    ax.legend()
    plt.show()
コード例 #5
0
ファイル: MSE.py プロジェクト: supermt/ML_Homework
    result = np.zeros((4, 5))
    for j in range(1000):
        MSEs = []
        for percent in percents:
            try:
                mse = []
                train_length = int(percent * total_length)
                a = np.random.randint(0, total_length, size=[1,
                                                             train_length])[0]
                sampx_sub = sampx[a]
                sampy_sub = sampy[a]
                phi = grand_order(sampx_sub, order)
                # LS
                LS_y_prime = grand_order(polyx, order) * LSregression(
                    phi.T, sampy_sub, order)
                mse.append(data_reading.MeanSquareError(LS_y_prime, polyy))
                # RLS
                RLS_target_alpha = RLS_hyper(phi.T, sampy_sub, polyx, polyy,
                                             order)
                RLS_y_prime = grand_order(polyx, order) * RLSregression(
                    phi.T, sampy_sub, RLS_target_alpha)
                mse.append(data_reading.MeanSquareError(RLS_y_prime, polyy))

                #LASSO
                LASSO_target_alpha = LASSO_hyper(phi, sampy_sub, polyx, polyy,
                                                 order)
                LASSO_y_prime = grand_order(polyx, order) * LASSOregression(
                    phi, sampy_sub, LASSO_target_alpha)
                mse.append(data_reading.MeanSquareError(LASSO_y_prime, polyy))
                #RR
                RR_y_prime = grand_order(polyx, order) * RRregression(
コード例 #6
0
ファイル: RR.py プロジェクト: supermt/ML_Homework
    D = order + 1
    n = len(y)

    f = np.array(np.append(np.zeros((1,D)),np.ones((1,n))))
    In = np.eye(n)
    X = np.zeros((n+D,1))
    matA = np.vstack((np.hstack((-phiT, -In)), np.hstack((phiT, -In))))

    fun = lambda x: np.dot(f.transpose(), x)
    cons = ({'type': 'ineq', 'fun': lambda x: b-np.dot(matA, x)})
    res = op.minimize(fun, X, constraints=cons, method="COBYLA")
    return res.x[0:D]

if '__main__' == __name__:
    poly_data , poly_keys = data_reading.readMatFile("poly_data.mat")
    order = 5
    phiT, y = grand_order(poly_data['sampx'][0],order), poly_data['sampy']
    polyx, polyy= poly_data['polyx'][0], poly_data['polyy']
    w = do_regression(phiT,y,order)
    y_prime = (grand_order(polyx,order) * w)
    
    fig = plt.figure("RR")
    ax = fig.add_subplot(111)
    ax.plot(poly_data['sampx'][0],y,color='r',linestyle='',marker='*',label="sample")
    # # regression line
    ax.plot(polyx,y_prime,color='g',linestyle='-',marker='',label="predict")
    print "mean error: " + str(data_reading.MeanSquareError(y_prime,polyy))
    # poly points
    ax.plot(polyx,polyy,color='b',linestyle='-',label="target")
    ax.legend()
    plt.show()
コード例 #7
0
ファイル: LS.py プロジェクト: supermt/ML_Homework
def do_regression(phi,sampy,order=1):  
  w = ((np.linalg.inv(phi * phi.T)) * phi) * sampy
  # part of painting
  return w

if __name__ == "__main__":
  poly_data , poly_keys = data_reading.readMatFile("poly_data.mat")

  order = 5

  sampx = poly_data['sampx'][0]
  sampy = poly_data['sampy']
  polyx = poly_data['polyx'][0]
  polyy = poly_data['polyy']

  w = do_regression(grand_order(sampx,order).T,sampy,order)
  targety = grand_order(polyx,order) * w

  fig = plt.figure("LS")
  ax = fig.add_subplot(111)
  # sample points
  ax.plot(poly_data['sampx'][0],sampy,color='r',linestyle='',marker='*',label="sample")
  # regression line
  ax.plot(polyx,targety,color='g',linestyle='-',marker='',label="predict")
  # poly points
  ax.plot(polyx,polyy,color='b',linestyle='-',marker='',label="target")
  print data_reading.MeanSquareError(targety,polyy)

  ax.legend()
  plt.show()