示例#1
0
def PrincipalComponentAnalysis(model, train_data, test_data, n_comp, configg,
                               model_nm, dataset_nm):

    model_name = f"{model_nm}_{dataset_nm}_dataset.csv"
    table = CL.ScoringTable(name=model_name, location=valid_path)

    for cross in range(10):
        start = time()
        train_matrix = CL.prepare_features(data=train_data[cross],
                                           config=configg)
        test_matrix = CL.prepare_features(data=test_data[cross],
                                          config=configg)
        labels = d.merge_labels(d.get_layer(train_data[cross], 2))

        clf = copy(model)

        scale = StandardScaler()
        train_matrix = scale.fit_transform(X=train_matrix, y=labels)
        test_matrix = scale.transform(X=test_matrix)

        pca = PCA(n_components=n_comp)
        train_matrix = pca.fit_transform(X=train_matrix)
        test_matrix = pca.transform(X=test_matrix)

        states = CL.train_and_predict(clf,
                                      train_matrix,
                                      test_matrix, [],
                                      labels,
                                      unsupervised=False,
                                      HMMmodified=False)

        [a, m, f, f_a, p, r] = CL.score(states,
                                        test_data[cross][0][2],
                                        unsupervised=False)

        kombinace = "pca"
        params = "pca"

        table.add(scores=[a, m, f, f_a, p, r],
                  n_estim=clf.n_estimators,
                  configg={
                      "Komb": kombinace,
                      "Param": params
                  })

        print(cross + 1, '. -> ', round(time() - start), "seconds")

    table.save_table()

    return table.return_table()