Пример #1
0
def OSLRegression(x, y, z, degree=5, l=0.1):
    # Split into training and test
    x_train = np.random.rand(100, 1)
    y_train = np.random.rand(100, 1)
    z = FrankeFunction(x_train, y_train)

    # traning data and finding design matrix X_
    X = np.c_[x_train, y_train]
    poly = PolynomialFeatures(degree)
    X_ = poly.fit_transform(X)
    clf = linear_model.LassoCV()
    clf.fit(X_, z)
    print("X_: ", np.shape(X_))
    beta = clf.coef_

    # predicting and preparing plot
    x_, y_ = np.meshgrid(x, y)
    x = x_.reshape(-1, 1)
    y = y_.reshape(-1, 1)
    M = np.c_[x, y]
    M_ = poly.fit_transform(M)
    predict = M_.dot(beta.T)

    # plotting
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(x_,
                    y_,
                    predict.reshape(20, 20),
                    cmap=cm.coolwarm,
                    linewidth=0,
                    antialiased=False)
    plt.show()

    return beta
def LassoRegression(x, y, z, degree=5, alpha=10**(-7), verbose=False):
    # Split into training and test
    x_train = np.random.rand(100, 1)
    y_train = np.random.rand(100, 1)
    z = FrankeFunction(x_train, y_train)

    # train and find design matrix X_
    X = np.c_[x_train, y_train]
    poly = PolynomialFeatures(degree)
    X_ = poly.fit_transform(X)
    clf = linear_model.Lasso(alpha, fit_intercept=False)
    clf.fit(X_, z)
    beta = clf.coef_

    # predict
    x_, y_ = np.meshgrid(x, y)
    x = x_.reshape(-1, 1)
    y = y_.reshape(-1, 1)
    M = np.c_[x, y]
    M_ = poly.fit_transform(M)
    predict = M_.dot(beta.T)

    if verbose:
        print("x: ", np.shape(x))
        print("y: ", np.shape(y))
        print("M: ", np.shape(M))
        print("M_: ", np.shape(M_))
        print("predict: ", np.shape(predict))

    design = np.dot(np.transpose(X), X)
    # plot
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(x_,
                    y_,
                    predict.reshape(20, 20),
                    cmap=cm.coolwarm,
                    linewidth=0,
                    antialiased=False)
    plt.show()

    return beta, design
def RidgeRegression(x, y, z, degree=5, alpha=10**(-6), verbose=False):
    # Split into training and test
    x_train = np.random.rand(100, 1)
    y_train = np.random.rand(100, 1)
    z = FrankeFunction(x_train, y_train)

    # training and finding design matrix X_
    X = np.c_[x_train, y_train]
    poly = PolynomialFeatures(degree)
    X_ = poly.fit_transform(X)
    ridge = linear_model.RidgeCV(alphas=np.array([alpha]))
    ridge.fit(X_, z)
    beta = ridge.coef_
    #intercept = ridge.intercept_

    # predict data and prepare for plotting
    x_, y_ = np.meshgrid(x, y)
    x = x_.reshape(-1, 1)
    y = y_.reshape(-1, 1)
    M = np.c_[x, y]
    M_ = poly.fit_transform(M)
    predict = M_.dot(beta.T)

    if verbose:
        print("X_: ", np.shape(X_))
        print("M: ", np.shape(M))
        print("M_: ", np.shape(M_))
        print("predict: ", np.shape(predict))

    # show figure
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(x_,
                    y_,
                    predict.reshape(20, 20),
                    cmap=cm.coolwarm,
                    linewidth=0,
                    antialiased=False)
    plt.show()

    return beta
from R2 import R2
from predict import predict
from bootstrap import bootstrap
from frankeFunction import FrankeFunction
#from OrdinaryLeastSquare import ols

#Read data
#terrain_over_Norway = imread('SRTM_data_Norway_1.tif')

#Choose to lo look at a little part of the data-set:
#terrain = terrain_over_Norway[0:100,0:100]
x = np.arange(0, 1, 0.05)
y = np.arange(0, 1, 0.05)
x, y = np.meshgrid(x, y)
n = np.size(x, 0)
terrain = FrankeFunction(x, y)
"""
#plot of the original
plt.figure()
plt.title('Terrain')
plt.imshow(terrain_over_Norway, cmap='gray')
plt.xlabel('X')
plt.ylabel('Y')
plt.savefig('oiginal_subset.png')
plt.show()
"""
num_rows = len(terrain)
num_cols = len(terrain[0])
num_observations = num_rows * num_cols
X = np.zeros((num_observations, 3))
    design = np.dot(np.transpose(X), X)
    # plot
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(x_,
                    y_,
                    predict.reshape(20, 20),
                    cmap=cm.coolwarm,
                    linewidth=0,
                    antialiased=False)
    plt.show()

    return beta, design


if __name__ == '__main__':
    #terrain1 = misc.imread('data.tif',flatten=0)
    verbose = True
    x = np.arange(0, 1, 0.05).reshape((20, 1))
    y = np.arange(0, 1, 0.05).reshape((20, 1))
    z = FrankeFunction(x, y)

    beta, design = LassoRegression(x, y, z, 5, alpha=10**(-7), verbose=True)
    if verbose:
        print("beta values: ", beta)
        print("design: ", design)
    beta_list = beta.ravel().tolist()
    cofint = mean_confidence_interval(beta_list)
    if verbose: print("mean_confidence_interval: ", cofint)