예제 #1
0
def normalize(X,
              y=None,
              norm='l2',
              axis=1,
              return_norm=False,
              return_norm_inv=False):
    assert (axis == 0 or axis == 1)
    assert (norm == 'l2' or norm == 'l1')
    X_T = matrix_transpose(X)

    y_norm = None
    if y != None:
        if norm == 'l2':
            y_norm = sqrt(sum(square(y)))
        elif norm == 'l1':
            y_norm = sqrt(sum(abs(y)))
    if y and y_norm == 0:
        return X

    norms = []
    if axis == 0:
        A = matrix_copy(X)

        for i in range(shape(X)[0]):
            n = 0
            if norm == 'l2':
                n = sqrt(sum(square(
                    X_T[i]))) if not y else sqrt(sum(square(X_T[i]))) / y_norm
            elif norm == 'l1':
                n = sqrt(sum(abs(
                    X_T[i]))) if not y else sqrt(sum(square(X_T[i]))) / y_norm
            if n != 0:
                A[i] = (multiply(X[i], 1 / float(n)))
            norms.append(n)
    elif axis == 1:
        A = matrix_transpose(X)
        for j in range(shape(X)[1]):
            n = 0
            if norm == 'l2':
                n = sum(square(
                    X_T[j])) if not y else sqrt(sum(square(X_T[j]))) / y_norm
            elif norm == 'l1':
                n = sum(abs(
                    X_T[j])) if not y else sqrt(sum(square(X_T[j]))) / y_norm
            if n != 0:
                A[j] = (multiply(X_T[j], 1 / float(n)))
            norms.append(n)

        A = matrix_transpose(A)

    norms_inv = [0 if x == 0 else 1 / float(x) for x in norms]
    if return_norm and return_norm_inv:
        return A, norms, norms_inv
    elif return_norm:
        return A, norms
    elif return_norm_inv:
        return A, norms_inv
    else:
        return A
예제 #2
0
def merge(ecs_logs,flavors_config,flavors_unique,training_start_time,training_end_time,predict_start_time,predict_end_time):
    predict = {}.fromkeys(flavors_unique)
    for f in flavors_unique:
        predict[f] = 0
    virtual_machine_sum = 0
    mapping_index = get_flavors_unique_mapping(flavors_unique)

    R = []
    X_trainS_raw,Y_trainS_raw,X_testS = features_building(ecs_logs,flavors_config,flavors_unique,training_start_time,training_end_time,predict_start_time,predict_end_time)
    # penalty = [1,1,1,1,0.5,0.5]
    X_trainS = fancy(X_trainS_raw,None,(0,-1),None)
    # X_trainS = X_trainS_raw
    
    Y_trainS = fancy(Y_trainS_raw,None,(0,-1))
    # Y_trainS = Y_trainS_raw

    X_valS = fancy(X_trainS_raw,None,(-1,),None)
    Y_valS = fancy(Y_trainS_raw,None,(-1,))

    #adjustable #5 Ridge Regression alpha
    clf = Ridge(alpha=1)
    from model_selection import grid_search_cv_early_stoping
    
    test = []
    train = []
    val = []
    for i in range(len(flavors_unique)):    
        X = X_trainS[i]
        y = Y_trainS[i]
        # clf = grid_search_cv(Ridge,{"alpha":[0.001,0.01,0.1,0.4,0.7,1,1.5,2]},X,y,cv=20,random_state=42,is_shuffle=True,verbose=True)
        clf = early_stoping(Ridge,{"alpha":sorted([0.01,0.02,0.1,0.4,0.7,1,1.5,2])[::-1]},X,y,X_valS[i],Y_valS[i],verbose=False)
        # clf = grid_search_cv_early_stoping(Ridge,{"alpha":sorted([0.01,0.02,0.1,0.4,0.7,1,1.5,2])[::-1]},X,y,X_valS[i],Y_valS[i],cv=10,random_state=42,is_shuffle=True,verbose=True)
        # clf = Ridge(alpha=(clf_1.alpha + clf_2.alpha))
        # clf = Ridge(alpha=1)
        # clf.fit(X,y)
        train.append(clf.predict(X))
        val.append(clf.predict(X_valS[i]))
        test.append(clf.predict(X_testS[i]))

    # print("shape(train)",shape(train))

    train = matrix_transpose(train)
    
    Y_trainS = matrix_transpose(Y_trainS)
    R.extend(test)

    print("training_score-->",official_score(train,Y_trainS))
    val = matrix_transpose(val)
    Y_valS = matrix_transpose(Y_valS)
    print("validation_score-->",official_score(val,Y_valS))
    
    result = flatten(R)
    result = [0 if r<0 else r for r in result]
    for f in flavors_unique:
        p = result[mapping_index[f]]
        predict[f] = int(round(p))
        virtual_machine_sum += int(round(p))
    return predict,virtual_machine_sum
예제 #3
0
    def fit(self, X, y):
        self._check(X, y)
        if dim(y) == 1:
            raw_X = X
            if self.fit_intercept:
                X = hstack([ones(shape(X)[0], 1), X])

            beta = zeros(shape(X)[1])  # row vector
            X_T = matrix_transpose(X)

            if self.fit_intercept:
                beta[0] = sum(minus(reshape(y, -1), dot(
                    raw_X, beta[1:]))) / (shape(X)[0])

            for _ in range(self.max_iter):
                start = 1 if self.fit_intercept else 0
                for j in range(start, len(beta)):
                    tmp_beta = [x for x in beta]
                    tmp_beta[j] = 0.0

                    r_j = minus(reshape(y, -1), dot(X, beta))
                    # r_j = minus(reshape(y,-1) , dot(X, tmp_beta))
                    arg1 = dot(X_T[j], r_j)
                    arg2 = self.alpha * shape(X)[0]

                    if sum(square(X_T[j])) != 0:
                        beta[j] = self._soft_thresholding_operator(
                            arg1, arg2) / sum(square(X_T[j]))
                    else:
                        beta[j] = 0

                    if self.fit_intercept:
                        beta[0] = sum(
                            minus(reshape(y, -1), dot(
                                raw_X, beta[1:]))) / (shape(X)[0])
                # # add whatch
                # self.beta = beta
                # self._whatch(raw_X,y)

            if self.fit_intercept:
                self.intercept_ = beta[0]
                self.coef_ = beta[1:]
            else:
                self.coef_ = beta
            self.beta = beta
            return self
        elif dim(y) == 2:
            if self.fit_intercept:
                X = hstack([ones(shape(X)[0], 1), X])
            y_t = matrix_transpose(y)
            betas = []
            for i in range(shape(y)[1]):
                betas.append(self._fit(X, y_t[i]))
            batas = matrix_transpose(betas)
            self.betas = batas
예제 #4
0
    def fit(self, X, y, weights=None):
        X, y = self._check(X, y)

        if self.fit_intercept:
            m, n = shape(X)
            bias = ones(m, 1)
            X = hstack([bias, X])

        eye = identity_matrix(shape(X)[1])
        from linalg.matrix import diag
        if not self.penalty_bias:
            eye[0][0] = 0

        # add weights
        if weights != None:
            assert (len(weights) == shape(X)[0])
            X = matrix_matmul(diag(weights), X)

        X_T = matrix_transpose(X)

        self.W = matrix_matmul(
            matrix_matmul(
                matrix_inverse(
                    plus(matrix_matmul(X_T, X),
                         multiply(eye,
                                  self.alpha * shape(X)[0]))
                    # plus(matrix_matmul(X_T,X),multiply(eye,self.alpha))
                ),
                X_T),
            y)
        self.importance_ = sum(self.W, axis=1)
        if self.fit_intercept:
            self.importance_ = self.importance_[1:]
예제 #5
0
    def _fit(self, X, y):
        self._check(X, y)
        assert (dim(y) == 1)

        beta = zeros(shape(X)[1])  # row vector
        X_T = matrix_transpose(X)

        if self.fit_intercept:
            beta[0] = sum(minus(reshape(y, -1), dot(X,
                                                    beta[1:]))) / (shape(X)[0])

        for _ in range(self.max_iter):
            print(_)
            start = 1 if self.fit_intercept else 0
            for j in range(start, len(beta)):
                tmp_beta = [x for x in beta]
                tmp_beta[j] = 0.0

                r_j = minus(reshape(y, -1), dot(X, beta))
                # r_j = minus(reshape(y,-1) , dot(X, tmp_beta))
                arg1 = dot(X_T[j], r_j)
                arg2 = self.alpha * shape(X)[0]

                if sum(square(X_T[j])) != 0:
                    beta[j] = self._soft_thresholding_operator(
                        arg1, arg2) / sum(square(X_T[j]))
                else:
                    beta[j] = 0

                if self.fit_intercept:
                    beta[0] = sum(minus(reshape(y, -1), dot(
                        X, beta[1:]))) / (shape(X)[0])
        return beta
예제 #6
0
    def predict(self, X):
        result = []
        # dim_X = dim(X)

        if dim(X) == 1:
            X = [X]
        for x in X:
            loss = sum(square(minus(self.X, x)), axis=1)

            index = argsort(loss)[:self.k]
            if self.verbose:
                print(index)

            ys = []
            for i in index:
                ys.append(self.y[i])

            k_loss_raw = sorted(loss)[:self.k]
            k_loss = [1 / l if l != 0 else 0 for l in k_loss_raw]
            k_loss_sum = sum(k_loss)
            weights = [
                l / float(k_loss_sum) if k_loss_sum != 0 else 1 for l in k_loss
            ]
            weight_m = diag(weights)
            ys = matrix_matmul(weight_m, ys)
            result.append(sum(ys, axis=0))

        if len(self.shape_Y) == 1:
            result = matrix_transpose(result)[0]

        return result
예제 #7
0
def standard_scaling(X, y=None, axis=1):
    if axis == 0:
        return matrix_transpose(standard_scaling(matrix_transpose(X), axis=1))
    R = []
    for j in range(shape(X)[1]):
        col = fancy(X, None, j)
        mean_ = mean(col)
        std = sqrt(mean(square(minus(col, mean_))))

        if y != None:
            std_y = sqrt(mean(square(minus(y, mean(y)))))

        if std == 0:
            R.append(col)
        else:
            R.append([(x - mean_) * std_y / std for x in col])
    return matrix_transpose(R)
예제 #8
0
def stdev(X):
    # X = matrix_copy(X)
    X_T = matrix_transpose(X)
    m = mean(X, axis=1)
    R = []
    for j in range(shape(X)[1]):
        R.append(sqrt(mean(square(minus(X_T[j], m[j])))))
    return R
예제 #9
0
    def fit(self, X, y):
        X, y = self._check(X, y)
        if self.fit_intercept:
            m, n = shape(X)
            bias = ones(m, 1)
            X = hstack([bias, X])

        X_T = matrix_transpose(X)
        # print matrix_matmul(X_T,X)
        self.W = matrix_matmul(
            matrix_matmul(matrix_inverse(matrix_matmul(X_T, X)), X_T), y)
예제 #10
0
def minmax_scaling(X, axis=1):
    assert (axis == 1)
    R = []
    for j in range(shape(X)[1]):
        col = fancy(X, None, j)
        max_ = max(col)
        min_ = min(col)
        mean_ = mean(col)
        if max_ - min_ == 0:
            R.append(col)
        else:
            R.append([(x - mean_) / (max_ - min_) for x in col])
    return matrix_transpose(R)
예제 #11
0
def maxabs_scaling(X, y=None, axis=1):
    assert (axis == 1)
    R = []
    for j in range(shape(X)[1]):
        col = fancy(X, None, j)
        max_ = max(abs(col))
        mean_ = mean(col)
        if max_ == 0:
            R.append(col)
        else:
            if not y:
                R.append([(x - mean_) / (max_) for x in col])
            else:
                R.append([(x - mean_) * max(y) / (max_) for x in col])

    return matrix_transpose(R)
예제 #12
0
    def fit(self, X, y):
        X, y = self._check(X, y)

        if self.fit_intercept:
            m, n = shape(X)
            bias = ones(m, 1)
            X = hstack([bias, X])

        eye = identity_matrix(shape(X)[1])
        from linalg.matrix import diag
        if self.penalty_loss:
            eye = diag(self.penalty_loss)
        X_T = matrix_transpose(X)

        self.W = matrix_matmul(
            matrix_matmul(
                matrix_inverse(
                    plus(matrix_matmul(X_T, X),
                         multiply(eye,
                                  self.alpha * shape(X)[0]))), X_T), y)
        self.importance_ = sum(self.W, axis=1)
        if self.fit_intercept:
            self.importance_ = self.importance_[1:]
import matplotlib.pyplot as plt
import math

original_x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
original_y = [math.sin(k) for k in original_x]

from linalg.matrix import matrix_inverse, matrix_transpose, matrix_mutmul
from linalg.vector import arange

print(original_x)
print(original_y)

family = [
    lambda x: 1, lambda x: x, lambda x: x**2, lambda x: math.pow(x, 3),
    lambda x: math.pow(x, 4)
]
X = [[f(k) for f in family] for k in original_x]
Y = [[k] for k in original_y]
X_T = matrix_transpose(X)

b = matrix_mutmul(matrix_mutmul(matrix_inverse(matrix_mutmul(X_T, X)), X_T), Y)

xx = arange(original_x[0], original_x[-1], 100)
yy = []
for k in xx:
    yy.append(sum([b[i][0] * family[i](k) for i in range(len(family))]))
plt.plot(xx, yy, label='fitting curve')
plt.scatter(original_x, original_y, label='original data', c='red')
plt.legend()
plt.show()