Example #1
0
class MyPLS():
    def __init__(self,
                 n_components=2,
                 scale=True,
                 max_iter=500,
                 tol=1e-06,
                 copy=True):
        self.pls = PLSRegression(n_components, scale, max_iter, tol, copy)

    def fit(self, X, Y):
        self.pls.fit(X, Y)
        return self.pls

    def predict(self, X, copy=True):
        return self.pls.predict(X, copy).flatten()

    def score(self, X, Y, sample_weight=None):
        return self.pls.score(X, Y, sample_weight)

    def get_params(self, deep=True):
        return self.pls.get_params(deep)

    def set_params(self, **parameters):
        self.pls.set_params(**parameters)
        return self

    @property
    def intercept_(self):
        return 0

    @property
    def coeff_(self):
        return self.pls.coef_
Example #2
0
    def test_parameters(self):
        """ Testing parameters of Model class. """
#1.)
        #create instance of PLS model using Model class & creating instance
        #   using SKlearn libary, comparing if the parameters of both instances are equal
        pls_parameters = {"n_components": 20, "scale": False, "max_iter": 200}
        model = Model(algorithm="PlsRegression", parameters=pls_parameters)
        pls_model = PLSRegression(n_components=20, scale="svd", max_iter=200)

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(pls_model.get_params()))
#2.)
        rf_parameters = {"n_estimators": 200, "max_depth": 50,"min_samples_split": 10}
        model = Model(algorithm="RandomForest", parameters=rf_parameters)
        rf_model = RandomForestRegressor(n_estimators=200, max_depth=50, min_samples_split=10)

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(rf_model.get_params()))
#3.)
        knn_parameters = {"n_neighbors": 10, "weights": "distance", "algorithm": "ball_tree"}
        model = Model(algorithm="KNN", parameters=knn_parameters)
        knn_model = KNeighborsRegressor(n_neighbors=10, weights='distance', algorithm="kd_tree")

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(knn_model.get_params()))
#4.)
        svr_parameters = {"kernel": "poly", "degree": 5, "coef0": 1}
        model = Model(algorithm="SVR",parameters=svr_parameters)
        svr_model = SVR(kernel='poly', degree=5, coef0=1)

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(svr_model.get_params()))
#5.)
        ada_parameters = {"n_estimators": 150, "learning_rate": 1.2, "loss": "square"}
        model = Model(algorithm="AdaBoost", parameters=ada_parameters)
        ada_model = AdaBoostRegressor(n_estimators=150, learning_rate=1.2, loss="square")

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(ada_model.get_params()))
#6.)
        bagging_parameters = {"n_estimators": 50, "max_samples": 1.5, "max_features": 2}
        model = Model(algorithm="Bagging", parameters=bagging_parameters)
        bagging_model = BaggingRegressor(n_estimators=50, max_samples=1.5, max_features="square")

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(bagging_model.get_params()))
#7.)
        lasso_parameters = {"alpha": 1.5, "max_iter": 500, "tol": 0.004}
        model = Model(algorithm="lasso", parameters=lasso_parameters)
        lasso_model = Lasso(alpha=1.5, max_iter=500, tol=0.004)

        for k, v in model.model.get_params().items():
            self.assertIn(k, list(lasso_model.get_params()))
Example #3
0
def _pls_regression_train(table, feature_cols, label_cols, n_components=2, scale=True, max_iter=500, tol=1e-6):
    pls_model = PLS(n_components=n_components, scale=scale, max_iter=max_iter, tol=tol)
    _, features = check_col_type(table, feature_cols)
    _, labels = check_col_type(table, label_cols)
    pls_model.fit(features, labels)
    predict = pls_model.predict(features)
    _mean_absolute_error = mean_absolute_error(labels, predict)
    _mean_squared_error = mean_squared_error(labels, predict)
    _r2_score = r2_score(labels, predict)
    result_table = pd.DataFrame.from_items([
        ['Metric', ['Mean Absolute Error', 'Mean Squared Error', 'R2 Score']],
        ['Score', [_mean_absolute_error, _mean_squared_error, _r2_score]]
    ])
    label_name = {
        'n_components': 'Number of components',
        'scale': "Scale",
        'max_iter': 'Max iteration',
        'tol': 'Tolerance'
    }
    get_param = pls_model.get_params()
    param_table = pd.DataFrame.from_items([
        ['Parameter', list(label_name.values())],
        ['Value', [get_param[x] for x in list(label_name.keys())]]
    ])
    rb = BrtcReprBuilder()
    rb.addMD(strip_margin("""
    | ### PLS Regression Result
    | {result}
    | ### Parameters
    | {list_parameters}
    """.format(result=pandasDF2MD(result_table), list_parameters=pandasDF2MD(param_table)
               )))
    model = _model_dict('pls_regression_model')
    model['feature_cols'] = feature_cols
    model['label'] = label_cols
    model['mean_absolute_error'] = _mean_absolute_error
    model['mean_squared_error'] = _mean_squared_error
    model['r2_score'] = _r2_score
    model['max_iter'] = max_iter
    model['tol'] = tol
    model['pls_model'] = pls_model
    model['_repr_brtc_'] = rb.get()
    return {'model': model}
Example #4
0
class PLS(Model):

    # X represents the features, Y represents the labels
    X = None
    Y = None
    prediction = None
    model = None


    def __init__(self, X=None, Y=None,  n_components=2, type='regressor', cfg=False):
        self.name = 'PLS'

        if X is not None:
            self.X = X

        if Y is not None:
            self.Y = Y

        self.type = type
        self.cfg = cfg
        self.n_components = n_components
        self.model = PLSRegression(n_components=n_components)


    def fit(self, X=None, Y=None):
        if X is not None:
            self.X = X

        if Y is not None:
            self.Y = Y

        print('PLS Train started............')
        self.model.fit(self.X, self.Y)
        print('PLS completed..........')

        return self.model


    def fit_transform(self, X=None, Y=None):
        if X is not None:
            self.X = X

        if Y is not None:
            self.Y = Y

        print('PLS Train/Transform started............')
        self.X  = self.model.fit_transform(self.X)
        print('PLS completed..........')

        self.X = pd.DataFrame(self.X)
        return self.X

    def predict(self, test_features):
        print('Prediction started............')
        self.predictions = self.model.predict(test_features)
        print('Prediction completed..........')
        return self.predictions


    def save(self):
        if self.cfg:
            f = open('pls_configs.txt', 'w')
            f.write(json.dumps(self.model.get_params()))
            f.close()
        print('No models will be saved for PLS')

    def featureImportance(self):
    #    if X_headers is None:
    #        X_headers = list(self.X)
#
#        feature_importance_ = zip(self.model.coef_.reshape(1,-1)[0], X_headers)
#        feature_importance = set(feature_importance_)

        return self.model.coef_


    def getAccuracy(self, test_labels, predictions, origin=0, hitmissr=0.8):
        correct = 0
        df = pd.DataFrame(data=predictions.flatten())
        for i in range(len(df)):
            if 1 - abs(df.values[i] - test_labels.values[i])/abs(df.values[i]) >= hitmissr:
                correct = correct + 1
        return float(correct)/len(df)

    def getConfusionMatrix(self, test_labels, predictions, label_headers):
        return 'No Confusion Matrix for Regression'

    def getRSquare(self, test_labels, predictions, mode='single'):
        df = pd.DataFrame(data=predictions.flatten())
        if self.type == 'regressor':
            if mode == 'multiple':
                errors = r2_score(test_labels, df, multioutput='variance_weighted')
            else:
                errors = r2_score(test_labels, df)
            return errors
        else:
            return 'No RSquare for Classification'

    def getMSE(self, test_labels, predictions):
        df = pd.DataFrame(data=predictions.flatten())
        if self.type == 'regressor':
            errors = mean_squared_error(test_labels, df)
            return errors
        else:
            return 'No MSE for Classification'

    def getMAPE(self, test_labels, predictions):
        df = pd.DataFrame(data=predictions.flatten())
        if self.type == 'regressor':
            errors = np.mean(np.abs((test_labels - df.values) / test_labels)) * 100
            return errors.values[0]
        else:
            return 'No MAPE for Classification'

    def getRMSE(self, test_labels, predictions):
        df = pd.DataFrame(data=predictions.flatten())
        if self.type == 'regressor':
            errors = sqrt(mean_squared_error(test_labels, df))
            return errors
        else:
            return 'No RMSE for Classification'
Example #5
0
        l_p_t.append(vec_p)
        l_c_t.append(vec_c)
    j += 1

sorted_p = np.asarray(l_p)
sorted_c = np.asarray(l_c)  #Convert the input to an array

plc = PLSCanonical()
plc.fit_transform(sorted_c, sorted_p)
sorted_c, sorted_p = plc.transform(sorted_c, sorted_p)

sorted_c_test = np.asarray(l_c_t)
sorted_p_test = np.asarray(l_p_t)
sorted_c_test, sorted_p_test = plc.transform(sorted_c_test, sorted_p_test)

plr = PLSRegression()
plr.fit(sorted_c, sorted_p)
params = plr.get_params()
plr.set_params(**params)
y_score = plr.predict(sorted_c_test)
sim_count = 0

print("Test Similarity: ")
for i in range(len(y_score)):
    result_sim = 1 - spatial.distance.cosine(y_score[i], sorted_p_test[i])
    if result_sim >= 0.85:
        sim_count += 1
    print("Data " + str(i + 1) + " : " + str(result_sim))
accuracy = float(sim_count) / float(len(y_score))
print("Accuracy: " + str(accuracy))
Example #6
0
pls_HLJ = PLSRegression(n_components=2)
pls_HLJ.fit(X_PLSR_HLJ, Y_PLSR_HLJ)
# X_train_r, Y_train_r = pls_XZ.transform(X_PLSR_HLJ, Y_PLSR_HLJ)
# PLSRegression(copy=True, max_iter=500, n_components=2, scale=True, tol=1e-06)
Y_PLSpred_HLJ = pls_HLJ.predict(X_PLSR_HLJ)
Y_PLSpred_HLJ =  np.vstack((Y_PLSpred_HLJ).T)[0]
# print(Y_PLSpred_HLJ)
# print(Y_train_r)

Y_PLSpred = np.vstack((Y_PLSpred_XZ, Y_PLSpred_XJ, Y_PLSpred_HLJ))# Pls预测值
Y_PLSpred_Target = np.vstack((Y_PLSR_XZ, Y_PLSR_XJ, Y_PLSR_HLJ)) # 目标值
# print(Y_PLSpred)
# print(Y_PLSpred)

aaa = pls_HLJ.get_params(deep=True) # 获取参数
print(aaa)


# 计算均方误差
def Rmse(predictions, targets):
    return np.sqrt(np.mean((predictions - targets) ** 2))


rmse = {}
for ip in np.arange(3):
    rmse[ip] = Rmse(Y_PLSpred_Target[ip, :], Y_PLSpred[ip, :])

print(rmse)
y22 = np.zeros((21, 6))
y22_1 = np.zeros((21, 6))
Example #7
0
def main():
    args = parser.parse_args()

    # 模型选择及输入参数
    model_name = {
        1: 'PLSR',
        2: 'LS-SVR',
        3: 'GPR',
        4: 'FCN',
        5: 'LSTM',
        6: 'GCN',
        7: 'MC-GCN',
        8: 'GC-LSTM'
    }
    print(model_name)
    model_select = list(input('Select models:'))

    # 初始化结果
    results = {
        'adj': [],
        'r2': [],
        'rmse': [],
        'loss_hist': [],
        'prediction': []
    }
    # os.mkdir('Results')
    f = open('Results/params.txt', 'w+')
    f.write('Parameters setting:\n{}\n\n'.format(args.__dict__))

    # 导入数据
    data = pd.read_excel('8号机磨煤机C_正常.xlsx',
                         index_col=0,
                         header=1,
                         nrows=args.length + 5001)
    data = data.iloc[5001:, :]

    # 数据划分
    predict_variable = [3, 12, 15, 20, 23]
    y = data.iloc[:, predict_variable]
    X = data.drop(columns=y.columns)
    X_train, y_train = X.iloc[:int(args.length * args.train_size
                                   )], y.iloc[:int(args.length *
                                                   args.train_size)]
    X_test, y_test = X.iloc[int(args.length * args.train_size
                                ):], y.iloc[int(args.length *
                                                args.train_size):]

    # 导出数据
    # X_train.to_csv('Results/X_train.csv', header=False, index=False)
    # X_test.to_csv('Results/X_test.csv', header=False, index=False)
    # y_train.to_csv('Results/y_train.csv', header=False, index=False)
    # y_test.to_csv('Results/y_test.csv', header=False, index=False)

    # 设定种子
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)

    # 多次实验
    for exp in range(args.n_exp):
        print('=====Experiment({}/{})====='.format(exp + 1, args.n_exp))
        f.write('=====Experiment({}/{})=====\n'.format(exp + 1, args.n_exp))
        results['adj'].append({})
        results['r2'].append({})
        results['rmse'].append({})
        results['loss_hist'].append({})
        results['prediction'].append({})

        # PLSR
        if '1' in model_select:
            flag = 1
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = PLSRegression(args.n_components).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # LS-SVR
        if '2' in model_select:
            flag = 2
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = LssvrModel(args.c, args.sigma).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # GPR
        if '3' in model_select:
            flag = 3
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            kernel = DotProduct() * RBF(args.length_scale,
                                        (args.length_scale, args.length_scale))
            reg = GaussianProcessRegressor(kernel=kernel,
                                           alpha=args.alpha).fit(
                                               X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # FCN
        if '4' in model_select:
            flag = 4
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = FcnModel(X_train.shape[1], y_train.shape[1],
                           (1024, 256, 256, 256), args.n_epoch,
                           args.batch_size, args.lr, args.weight_decay,
                           args.step_size, args.gamma).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            results['loss_hist'][-1].update({model_name[flag]: reg.loss_hist})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # LSTM
        if '5' in model_select:
            flag = 5
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = LstmModel(X_train.shape[1], y_train.shape[1], (1024, ),
                            (256, 256, 256), args.seq_len, args.n_epoch,
                            args.batch_size, args.lr, args.weight_decay,
                            args.step_size, args.gamma).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            results['loss_hist'][-1].update({model_name[flag]: reg.loss_hist})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # GCN
        if '6' in model_select:
            flag = 6
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = GcnModel(X_train.shape[1], y_train.shape[1], (1024, ),
                           (256, 256, 256), args.graph_reg, args.self_con,
                           args.n_epoch, args.batch_size, args.lr,
                           args.weight_decay, args.step_size,
                           args.gamma).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            results['loss_hist'][-1].update({model_name[flag]: reg.loss_hist})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # MC-GCN
        if '7' in model_select:
            flag = 7
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = McgcnModel(X_train.shape[1], (1024, ), (256, ), (256, 256),
                             y_train.shape[1], args.graph_reg, args.self_con,
                             args.n_epoch, args.batch_size, args.lr,
                             args.weight_decay, args.step_size,
                             args.gamma).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['adj'][-1].update({model_name[flag]: reg.adj})
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            results['loss_hist'][-1].update({model_name[flag]: reg.loss_hist})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

        # GC-LSTM
        if '8' in model_select:
            flag = 8
            print('====={}====='.format(model_name[flag]))
            f.write('====={}=====\n'.format(model_name[flag]))

            # 训练&测试
            t1 = time.time()
            reg = GclstmModel(X_train.shape[1], (1024, ), (256, ), (256, 256),
                              y_train.shape[1], args.seq_len, args.graph_reg,
                              args.self_con, args.n_epoch, args.batch_size,
                              args.lr, args.weight_decay, args.step_size,
                              args.gamma).fit(X_train, y_train)
            t2 = time.time()
            y_pred = reg.predict(X_test)
            t3 = time.time()
            y_fit = reg.predict(X_train)
            print(reg.get_params())
            print('Time:\nFit: {:.3f}s Pred: {:.3f}s'.format(t2 - t1, t3 - t2))
            print('R2:\nFit: {} Pred: {}'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 写入文件
            f.write(str(reg.get_params()) + '\n')
            f.write('Time:\nFit: {:.3f}s Pred: {:.3f}s\n'.format(
                t2 - t1, t3 - t2))
            f.write('R2:\nFit: {} Pred: {}\n'.format(
                r2_score(y_train, y_fit, multioutput='raw_values'),
                r2_score(y_test, y_pred, multioutput='raw_values')))

            # 存储结果和模型
            index = r2_rmse(y_test, y_pred, y.columns, f)
            results['adj'][-1].update({model_name[flag]: reg.adj})
            results['r2'][-1].update({model_name[flag]: index[0]})
            results['rmse'][-1].update({model_name[flag]: index[1]})
            results['prediction'][-1].update({model_name[flag]: y_pred})
            results['loss_hist'][-1].update({model_name[flag]: reg.loss_hist})
            joblib.dump(
                reg, 'Results/{}-{}.model'.format(model_name[flag], exp + 1))

    # 存储结果
    np.save('Results/results.npy', results)
    f.close()
Example #8
0
        if len(temp4) == 600 and len(temp3) == 300:
            x_n.append(temp4)
            y_n.append(temp3)

npx = np.asarray(x, dtype=np.float64)
npy = np.asarray(y, dtype=np.float64)

npxn = np.asarray(x_n, dtype=np.float64)
npyn = np.asarray(y_n, dtype=np.float64)
cca = PLSCanonical(n_components=2)
cca.fit_transform(npx, npy)
npx, npy = cca.transform(npx, npy)
npxn, npyn = cca.transform(npxn, npyn)

pls.fit(npx, npy)
params = pls.get_params(deep=True)
print(params)
pls.set_params(**params)

y_score = pls.predict(npxn)

sim_count = 0
tol = 0.1

for index in range(len(y_score)):
    sub_result = np.subtract(y_score, npyn)
    result = 1 - spatial.distance.cosine(y_score[index], npyn[index])
    print("similarity of test example " + str(index) + " = " + str(result))
    if (1 - math.fabs(result)) <= tol:
        sim_count += 1