Esempio n. 1
0
    def _test_generic(self, estimator_class):
        loo_scorer = validation.LinearSmootherLeaveOneOutScorer()
        loo_scorer_alt = _LinearSmootherLeaveOneOutScorerAlternative()
        x = np.linspace(-2, 2, 5)
        fd = skfda.FDataGrid(x**2, x)

        estimator = estimator_class()

        grid = validation.SmoothingParameterSearch(estimator, [2, 3],
                                                   scoring=loo_scorer)
        grid.fit(fd)
        score = np.array(grid.cv_results_['mean_test_score'])

        grid_alt = validation.SmoothingParameterSearch(estimator, [2, 3],
                                                       scoring=loo_scorer_alt)
        grid_alt.fit(fd)
        score_alt = np.array(grid_alt.cv_results_['mean_test_score'])

        np.testing.assert_array_almost_equal(score, score_alt)
def smooth_datagrid(fd):
    '''
    Smoothes the values in the datagrid object

    Args:
        fd (datagrid): Includes data_matrix, x vector for contionous data
        representation.

    Returns:
        knn_fd (datagrid): datagrid values smoothed with an kN Kernel.

    '''
    # smooth the values with kNeighbors kernel
    param_values = np.linspace(start=2, stop=25, num=24)
    knn = val.SmoothingParameterSearch(ks.KNeighborsSmoother(), param_values)
    knn.fit(fd)
    knn_fd = knn.transform(fd)

    return knn_fd
Esempio n. 3
0
# As an example, we will smooth the first 300 curves only. In the following
# plot, the first five curves are shown.
dataset = skfda.datasets.fetch_phoneme()
fd = dataset['data'][:300]

fd[0:5].plot()

##############################################################################
# Here we show the general cross validation scores for different values of the
# parameters given to the different smoothing methods.

param_values_knn = np.arange(1, 24, 2)
param_values_others = param_values_knn / 32

# Local linear regression kernel smoothing.
llr = val.SmoothingParameterSearch(ks.LocalLinearRegressionSmoother(),
                                   param_values_others)
llr.fit(fd)
llr_fd = llr.transform(fd)

# Nadaraya-Watson kernel smoothing.
nw = val.SmoothingParameterSearch(ks.NadarayaWatsonSmoother(),
                                  param_values_others)
nw.fit(fd)
nw_fd = nw.transform(fd)

# K-nearest neighbours kernel smoothing.
knn = val.SmoothingParameterSearch(ks.KNeighborsSmoother(), param_values_knn)
knn.fit(fd)
knn_fd = knn.transform(fd)

fig = plt.figure()
Esempio n. 4
0
estimator = kde.fit(y)
estimator = np.reshape(estimator[0], df.shape[0])

plt.scatter(x, y)
plt.scatter(x, estimator, c='r')
plt.show()

# Using SKFDA

df_grid=skfda.FDataGrid(df)

bandwidth = np.arange(0.1, 5, 0.2)

llr = val.SmoothingParameterSearch(
    ks.LocalLinearRegressionSmoother(),
    bandwidth)
fit = llr.fit(df_grid)
llr_df = llr.transform(df_grid)

plt.scatter(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(x, llr_df, c='r')
plt.show()

# just K-fold cross-validation
"""
rn = range(1,26)
kf3 = sk_ms.KFold(3, shuffle=False)
for train_index, test_index in kf3.split(rn):
dataset = skfda.datasets.fetch_phoneme()
fd = dataset['data'][:300]

fd[0:5].plot()
plt.show()

n_neighbors = np.arange(1, 24)

scale_factor = ((fd.domain_range[0][1] - fd.domain_range[0][0]) /
                len(fd.grid_points[0]))

bandwidth = n_neighbors * scale_factor

# K-nearest neighbours kernel smoothing.
knn = val.SmoothingParameterSearch(
    ks.KNeighborsSmoother(),
    n_neighbors,
)
knn.fit(fd)
knn_fd = knn.transform(fd)

# Local linear regression kernel smoothing.
llr = val.SmoothingParameterSearch(
    ks.LocalLinearRegressionSmoother(),
    bandwidth,
)
llr.fit(fd)
llr_fd = llr.transform(fd)

# Nadaraya-Watson kernel smoothing.
nw = val.SmoothingParameterSearch(
    ks.NadarayaWatsonSmoother(),
Esempio n. 6
0
    def test_other(self, method='kernel'):
        import skfda
        import skfda.preprocessing.smoothing.kernel_smoothers as ks
        import skfda.preprocessing.smoothing.validation as val

        self.restore_model(self.resume_iters)

        self.G.eval()
        self.set_requires_grad(self.G, False)

        self.iteration = self.test_len - self.num_clips + 1
        self.model_save_dir = os.path.join(self.checkpoint_dir, self.model_dir)

        graph_embedding = []

        node_abnormal = []
        graph_ab_list = []

        with torch.no_grad():
            idx = 0
            self.subject = 0
            while idx < self.iteration:
                # =================================================================================== #
                #                             1. Preprocess input data(Unfinished)                    #
                # =================================================================================== #
                node_feature, edge, sensor, abnormal, graph_abnormal, idx = self.load_data_test(
                    idx)

                node_abnormal.append(abnormal)
                graph_ab_list.append(graph_abnormal.view(self.batch_size))

                # =================================================================================== #
                #                             2. Train the Model                                      #
                # =================================================================================== #
                _, _, _, E = self.G(node_feature, edge, sensor)

                # if idx == 0:
                #     graph_embedding.append(E)
                # else:
                graph_embedding.append(torch.unsqueeze(E[-1], 0))

                del E
                del node_feature, edge
                torch.cuda.empty_cache()

                idx += 1

        print("Finish NN Part!")

        graph_embedding = torch.cat(graph_embedding)
        graph_embedding = graph_embedding.view(graph_embedding.shape[0],
                                               -1).cpu()
        residual = []

        for idx in range(len(self.subject_test)):
            start = int(self.subject_test[idx, 1]) - idx * (self.num_clips - 1)
            end = int(self.subject_test[idx,
                                        2]) - (idx + 1) * (self.num_clips - 1)

            if method == 'kernel':
                fd = skfda.representation.grid.FDataGrid(
                    graph_embedding[start:end + 1])
                n_neighbors = np.arange(1, 24)
                scale_factor = (
                    (fd.domain_range[0][1] - fd.domain_range[0][0]) /
                    len(fd.grid_points[0]))
                bandwidth = n_neighbors * scale_factor

                nw = val.SmoothingParameterSearch(ks.NadarayaWatsonSmoother(),
                                                  bandwidth)
                nw.fit(fd)
                nw_fd = nw.transform(fd)
                r = self.loss_function(torch.tensor(nw_fd.data_matrix),
                                       torch.tensor(fd.data_matrix),
                                       graph=False,
                                       device=self.device)
                residual.append(r)

        residual = torch.cat(residual).view(graph_embedding.shape[0],
                                            self.num_sensor_dev, -1)
        residual = torch.mean(residual, dim=-1)
        node_abnormal = torch.cat(node_abnormal)
        print(residual.shape)
        print(node_abnormal.shape)
        predict_result(residual, node_abnormal, "sensor")
        if graph_abnormal is not None:
            graph_predict = torch.mean(residual, dim=-1)
            graph_ab_list = torch.tensor(graph_ab_list)
            predict_result(graph_predict, graph_ab_list, 'graph')