Esempio n. 1
0
def projectEnsemble(form_values, X, labels, control_points, dataset_name):
    #Normalize weights
    weight1 = float(form_values['LAMP_weight']) / (
        float(form_values['LAMP_weight']) + float(form_values['LSP_weight']) +
        float(form_values['PLMP_weight']))
    weight2 = float(form_values['LSP_weight']) / (
        float(form_values['LAMP_weight']) + float(form_values['LSP_weight']) +
        float(form_values['PLMP_weight']))
    weight3 = float(form_values['PLMP_weight']) / (
        float(form_values['LAMP_weight']) + float(form_values['LSP_weight']) +
        float(form_values['PLMP_weight']))

    form_values['LAMP_weight'] = weight1
    form_values['LSP_weight'] = weight2
    form_values['PLMP_weight'] = weight3

    projection_json = {}
    ensemble = np.zeros((X.shape[0], 2))

    # X = min_max_scaler.fit_transform(X) # min_max

    if float(form_values['LAMP_weight']) != 0.0:
        lamp_proj = Lamp(Xdata=X,
                         control_points=control_points[:, :2],
                         label=False)
        x_projected = lamp_proj.fit()
        # x_projected = min_max_scaler.fit_transform(x_projected) # min_max
        np.savetxt("../../datasets/" + dataset_name + '/' + dataset_name +
                   "_projected_LAMP.csv",
                   x_projected,
                   delimiter=",")

        projection_json['LAMP'] = {
            'x': x_projected[:, 0].tolist(),
            'y': x_projected[:, 1].tolist(),
            'labels': labels.reshape(-1).tolist()
        }
        ensemble += x_projected * float(form_values['LAMP_weight'])

    if float(form_values['LSP_weight']) != 0.0:
        os.system("./runLSP.m " + dataset_name)
        x_projected = pd.read_csv("../../datasets/" + dataset_name + '/' +
                                  dataset_name + '_projected_octave.csv',
                                  header=None).values
        # x_projected = min_max_scaler.fit_transform(x_projected) # min_max
        np.savetxt("../../datasets/" + dataset_name + '/' + dataset_name +
                   "_projected_LSP.csv",
                   x_projected,
                   delimiter=",")

        projection_json['LSP'] = {
            'x': x_projected[:, 0].tolist(),
            'y': x_projected[:, 1].tolist(),
            'labels': labels.reshape(-1).tolist()
        }
        ensemble += x_projected * float(form_values['LSP_weight'])

    if float(form_values['PLMP_weight']) != 0.0:
        plmp = PLMP(X, labels.reshape(-1),
                    control_points[:, 2].reshape(-1).astype(int),
                    control_points[:, :2])
        plmp.project()
        x_projected = plmp.get_projection()

        # x_projected = min_max_scaler.fit_transform(x_projected) # min_max
        np.savetxt("../../datasets/" + dataset_name + '/' + dataset_name +
                   "_projected_PLMP.csv",
                   x_projected,
                   delimiter=",")

        projection_json['PLMP'] = {
            'x': x_projected[:, 0].tolist(),
            'y': x_projected[:, 1].tolist(),
            'labels': labels.reshape(-1).tolist()
        }
        ensemble += x_projected * float(form_values['PLMP_weight'])

    projection_json['Ensemble'] = {
        'x': ensemble[:, 0].tolist(),
        'y': ensemble[:, 1].tolist(),
        'labels': labels.reshape(-1).tolist()
    }

    # print("evaluating metrics...")
    metrics = getMetricsForAllProjections(X, projection_json, labels, 8)
    # print("evaluating metrics finished")

    data = {
        'projections': projection_json,
        'metrics': metrics
    }  #{'NP': [], 'T': [], 'NH': []}

    return data
Esempio n. 2
0
from scipy.stats.mstats import zscore
import matplotlib.pyplot as plt

from lamp import Lamp

iris = datasets.load_iris()
x = iris.data
y = iris.target

# randomly chosing the control points
sample_size = 15
samples = np.random.randint(0, high=x.shape[0], size=(sample_size, ))

##### projecting control points with MDS #####
ctp_mds = MDS(n_components=2)
ctp_samples = ctp_mds.fit_transform(x[samples] - np.average(x[samples]))

# including ids of control points as the last column of the projected control points
ctp_samples = np.hstack((ctp_samples, samples.reshape(sample_size, 1)))

# including labels as the last column
data = np.hstack((x, y.reshape(y.shape[0], 1)))

##### using Lamp
lamp_proj = Lamp(Xdata=data, control_points=ctp_samples, label=True)
data_proj = lamp_proj.fit()

plt.scatter(data_proj[:, 0], data_proj[:, 1], c=y)
plt.scatter(ctp_samples[:, 0], ctp_samples[:, 1], c='r', s=2)
plt.show()
Esempio n. 3
0
######## Generating the Control Points #############

##### projecting control points with MDS #####
ctp_mds = MDS(n_components=2)
ctp_samples = ctp_mds.fit_transform(x[samples] - np.average(x[samples]))

# including ids of control points as the last column of the projected control points
ctp_samples = np.hstack((ctp_samples, samples.reshape(sample_size, 1)))

######## Performing the projections in two stages #############

#####  Instantiating Lamp
lamp_proj = Lamp(Xdata=x[:num_points], control_points=ctp_samples, label=True)

#####  projecting first half of the data
data_proj = lamp_proj.fit()

plt.title('First Half')
plt.scatter(data_proj[:, 0], data_proj[:, 1], c=data_proj[:, -1])
plt.scatter(ctp_samples[:, 0], ctp_samples[:, 1], c='r', s=2)

#####  projecting second half of the data
data_proj = lamp_proj.fit(Xdata=x[num_points:])

plt.title('Second Half')
plt.scatter(data_proj[:, 0],
            data_proj[:, 1],
            marker='o',
            edgecolors='r',
            c=data_proj[:, -1])
plt.show()