std_0_variable_flags = original_x.std() == 0
x = original_x.drop(original_x.columns[std_0_variable_flags], axis=1)

variables = pd.concat([y, x], axis=1)
numbers_of_x = np.arange(numbers_of_y[-1] + 1, variables.shape[1])

# standardize x and y
autoscaled_variables = (variables - variables.mean(axis=0)) / variables.std(
    axis=0, ddof=1)
autoscaled_target_y_value = (target_y_value - variables.mean(
    axis=0)[numbers_of_y]) / variables.std(axis=0, ddof=1)[numbers_of_y]

# construct GTMR model
model = GTM(shape_of_map, shape_of_rbf_centers, variance_of_rbfs,
            lambda_in_em_algorithm, number_of_iterations, display_flag)
model.fit(autoscaled_variables)

if model.success_flag:
    # calculate of responsibilities
    responsibilities = model.responsibility(autoscaled_variables)
    means = responsibilities.dot(model.map_grids)
    modes = model.map_grids[responsibilities.argmax(axis=1), :]

    mean_of_estimated_mean_of_y, mode_of_estimated_mean_of_y, responsibilities_y, py = \
        model.gtmr_predict(autoscaled_variables.iloc[:, numbers_of_x], numbers_of_x, numbers_of_y)

    plt.rcParams['font.size'] = 18
    for index, y_number in enumerate(numbers_of_y):
        predicted_y_test = mode_of_estimated_mean_of_y[:, index] * variables.iloc[:, y_number].std(
        ) + variables.iloc[:, y_number].mean()
        # yy-plot
예제 #2
0
# optimize hyperparameter in GTMR with CV
model = GTM()
model.gtmr_cv_opt(autoscaled_variables_train, numbers_of_y,
                  candidates_of_shape_of_map,
                  candidates_of_shape_of_rbf_centers,
                  candidates_of_variance_of_rbfs,
                  candidates_of_lambda_in_em_algorithm, fold_number,
                  number_of_iterations)
model.display_flag = display_flag
print('optimized shape of map :', model.shape_of_map)
print('optimized shape of RBF centers :', model.shape_of_rbf_centers)
print('optimized variance of RBFs :', model.variance_of_rbfs)
print('optimized lambda in EM algorithm :', model.lambda_in_em_algorithm)

# construct GTMR model
model.fit(autoscaled_variables_train)
if model.success_flag:
    # calculate of responsibilities
    responsibilities = model.responsibility(autoscaled_variables_train)
    means = responsibilities.dot(model.map_grids)
    modes = model.map_grids[responsibilities.argmax(axis=1), :]

    plt.rcParams['font.size'] = 18
    for y_number in numbers_of_y:
        # plot the mean of responsibilities
        plt.scatter(means[:, 0], means[:, 1], c=variables_train[:, y_number])
        plt.colorbar()
        plt.ylim(-1.1, 1.1)
        plt.xlim(-1.1, 1.1)
        plt.xlabel('z1 (mean)')
        plt.ylabel('z2 (mean)')
예제 #3
0
# plot
plt.rcParams["font.size"] = 18
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
p = ax.scatter(original_X[:, 0], original_X[:, 1], original_X[:, 2], c=original_y)
fig.colorbar(p)
plt.show()

# autoscaling
autoscaled_X = (original_X - original_X.mean(axis=0)) / original_X.std(axis=0, ddof=1)
autoscaled_target_y_value = (target_y_value - original_y.mean(axis=0)) / original_y.std(axis=0, ddof=1)

# construct GTM model
model = GTM(shape_of_map, shape_of_rbf_centers, variance_of_rbfs, lambda_in_em_algorithm, number_of_iterations,
            display_flag)
model.fit(autoscaled_X)
if model.success_flag:
    # calculate of responsibilities
    responsibilities = model.responsibility(autoscaled_X)

    # plot the mean of responsibilities
    means = responsibilities.dot(model.map_grids)
    plt.figure()
    #    plt.figure(figsize=figure.figaspect(1))
    plt.scatter(means[:, 0], means[:, 1], c=original_y)
    plt.colorbar()
    plt.ylim(-1.1, 1.1)
    plt.xlim(-1.1, 1.1)
    plt.xlabel("z1 (mean)")
    plt.ylabel("z2 (mean)")
    plt.show()
# divide a dataset into training data and test data
Xtrain = original_X[:500, :]
ytrain = original_y[:500]
Xtest = original_X[500:, :]
ytest = original_y[500:]

# autoscaling
# autoscaled_X = (original_X - original_X.mean(axis=0)) / original_X.std(axis=0,ddof=1)
autoscaled_Xtrain = (Xtrain - Xtrain.mean(axis=0)) / Xtrain.std(axis=0, ddof=1)
# autoscaled_Xtest = (Xtest - X.mean(axis=0)) / X.std(axis=0,ddof=1)
# autoscaled_ytrain = (ytrain - ytrain.mean()) / ytrain.std(ddof=1)

# construct GTM model
model = GTM(shape_of_map, shape_of_rbf_centers, variance_of_rbfs, lambda_in_em_algorithm, number_of_iterations,
            display_flag)
model.fit(autoscaled_Xtrain)
if model.success_flag:
    # calculate of responsibilities
    responsibilities = model.responsibility(autoscaled_Xtrain)

    # plot the mean of responsibilities
    means = responsibilities.dot(model.map_grids)
    plt.figure()
    #    plt.figure(figsize=figure.figaspect(1))
    plt.scatter(means[:, 0], means[:, 1], c=ytrain)
    plt.colorbar()
    plt.ylim(-1.1, 1.1)
    plt.xlim(-1.1, 1.1)
    plt.xlabel("z1 (mean)")
    plt.ylabel("z2 (mean)")
    plt.show()
        candidates_of_variance_of_rbfs) * len(
            candidates_of_lambda_in_em_algorithm)
calculation_number = 0
for shape_of_map_grid in candidates_of_shape_of_map:
    for shape_of_rbf_centers_grid in candidates_of_shape_of_rbf_centers:
        for variance_of_rbfs_grid in candidates_of_variance_of_rbfs:
            for lambda_in_em_algorithm_grid in candidates_of_lambda_in_em_algorithm:
                calculation_number += 1
                print([calculation_number, all_calculation_numbers])
                # construct GTM model
                model = GTM(
                    [shape_of_map_grid, shape_of_map_grid],
                    [shape_of_rbf_centers_grid, shape_of_rbf_centers_grid],
                    variance_of_rbfs_grid, lambda_in_em_algorithm_grid,
                    number_of_iterations, display_flag)
                model.fit(input_dataset)
                if model.success_flag:
                    # calculate of responsibilities
                    responsibilities = model.responsibility(input_dataset)
                    # calculate the mean of responsibilities
                    means = responsibilities.dot(model.map_grids)
                    # calculate k3n-error
                    k3nerror_of_gtm = k3nerror(input_dataset, means,
                                               k_in_k3nerror)
                else:
                    k3nerror_of_gtm = 10**100
                parameters_and_k3nerror.append([
                    shape_of_map_grid, shape_of_rbf_centers_grid,
                    variance_of_rbfs_grid, lambda_in_em_algorithm_grid,
                    k3nerror_of_gtm
                ])