Пример #1
0
def main():
    # generate some random data with 36 features
    data1 = np.random.normal(loc=-.25, scale=0.5, size=(500, 36))
    data2 = np.random.normal(loc=.25, scale=0.5, size=(500, 36))
    data = np.vstack((data1, data2))

    som = SOM(10, 10)  # initialize the SOM
    som.fit(data, 10000, save_e=True, interval=100
            )  # fit the SOM for 10000 epochs, save the error every 100 steps
    som.plot_error_history(
        filename='images/som_error.png')  # plot the training error history

    targets = np.array(500 * [0] +
                       500 * [1])  # create some dummy target values

    # now visualize the learned representation with the class labels
    som.plot_point_map(data,
                       targets, ['Class 0', 'Class 1'],
                       filename='images/som.png')
    som.plot_class_density(data,
                           targets,
                           t=0,
                           name='Class 0',
                           filename='images/class_0.png')
    som.plot_distance_map(filename='images/distance_map.png'
                          )  # plot the distance map after training
Пример #2
0
def main():
    # generate some virtual peptide sequences
    libnum = 1000  # 1000 sequences per sublibrary
    h = Helices(seqnum=libnum)
    r = Random(seqnum=libnum)
    n = AMPngrams(seqnum=libnum, n_min=4)
    h.generate_sequences()
    r.generate_sequences(proba='AMP')
    n.generate_sequences()

    # calculate molecular descirptors for the peptides
    d = PeptideDescriptor(seqs=np.hstack(
        (h.sequences, r.sequences, n.sequences)),
                          scalename='pepcats')
    d.calculate_crosscorr(window=7)

    # train a som on the descriptors and print / plot the training error
    som = SOM(x=12, y=12)
    som.fit(data=d.descriptor, epochs=100000, decay='hill')
    print("Fit error: %.4f" % som.error)
    som.plot_error_history(filename="som_error.png")

    # load known antimicrobial peptides (AMPs) and transmembrane sequences
    dataset = load_AMPvsTM()
    d2 = PeptideDescriptor(dataset.sequences, 'pepcats')
    d2.calculate_crosscorr(7)
    targets = np.array(libnum * [0] + libnum * [1] + libnum * [2] + 206 * [3])
    names = ['Helices', 'Random', 'nGrams', 'AMP']

    # plot som maps with location of AMPs
    som.plot_point_map(np.vstack((d.descriptor, d2.descriptor[206:])),
                       targets,
                       names,
                       filename="peptidesom.png")
    som.plot_density_map(np.vstack((d.descriptor, d2.descriptor)),
                         filename="density.png")
    som.plot_distance_map(colormap='Reds', filename="distances.png")

    colormaps = ['Oranges', 'Purples', 'Greens', 'Reds']
    for i, c in enumerate(set(targets)):
        som.plot_class_density(np.vstack((d.descriptor, d2.descriptor)),
                               targets,
                               c,
                               names,
                               colormap=colormaps[i],
                               filename='class%i.png' % c)

    # get neighboring peptides (AMPs / TMs) for a sequence of interest
    my_d = PeptideDescriptor(seqs='GLFDIVKKVVGALLAG', scalename='pepcats')
    my_d.calculate_crosscorr(window=7)
    som.get_neighbors(datapoint=my_d.descriptor,
                      data=d2.descriptor,
                      labels=dataset.sequences,
                      d=0)
Пример #3
0
import numpy as np
from som import SOM

# generate some random data with 36 features
data1 = np.random.normal(loc=-.25, scale=0.5, size=(500, 36))
data2 = np.random.normal(loc=.25, scale=0.5, size=(500, 36))
data = np.vstack((data1, data2))

som = SOM(10, 10)  # initialize the SOM
som.fit(data, 2000)  # fit the SOM for 2000 epochs

targets = 500 * [0] + 500 * [1]  # create some dummy target values

# now visualize the learned representation with the class labels
som.plot_point_map(data, targets, ['class 1', 'class 2'], filename='som.png')
som.plot_class_density(data,
                       targets,
                       1, ['class 1', 'class 2'],
                       filename='class_0.png')
Пример #4
0
if __name__ == "__main__":
    # generate some random data with 36 features
    data1 = np.random.normal(loc=-.25, scale=0.5, size=(500, 36))
    data2 = np.random.normal(loc=.25, scale=0.5, size=(500, 36))
    data = np.vstack((data1, data2))

    som = SOM(10, 10)  # initialize the SOM
    som.fit(data, 1000, save_e=True, interval=100
            )  # fit the SOM for 10000 epochs, save the error every 100 steps
    som.plot_error_history(
        filename='E:\_Python\SelfOrganizingMap\images\som_error.png'
    )  # plot the training error history

    targets = np.array(500 * [0] +
                       500 * [1])  # create some dummy target values

    # now visualize the learned representation with the class labels
    som.plot_point_map(
        data,
        targets, ['Class 0', 'Class 1'],
        filename=r'E:\_Python\SelfOrganizingMap\images\som.t.png')
    som.plot_class_density(
        data,
        targets,
        t=0,
        name='Class 0',
        filename=r'E:\_Python\SelfOrganizingMap\images\class_0.t.png')
    som.plot_distance_map(
        filename='E:\_Python\SelfOrganizingMap\images\distance_map.t.png'
    )  # plot the distance map after training
Пример #5
0
import numpy as np
from som import SOM

# generate some random data with 36 features
data1 = np.random.normal(loc=-.25, scale=0.5, size=(500, 36))
data2 = np.random.normal(loc=.25, scale=0.5, size=(500, 36))
data = np.vstack((data1, data2))

som = SOM(10, 10)  # initialize the SOM
som.fit(data, 10000, save_e=True, interval=100)  # fit the SOM for 10000 epochs, save the error every 100 steps
som.plot_error_history(filename='../images/som_error.png')  # plot the training error history

targets = np.array(500 * [0] + 500 * [1])  # create some dummy target values

# now visualize the learned representation with the class labels
som.plot_point_map(data, targets, ['Class 0', 'Class 1'], filename='../images/som.png')
som.plot_class_density(data, targets, t=0, name='Class 0', filename='../images/class_0.png')
som.plot_distance_map(filename='../images/distance_map.png')  # plot the distance map after training
Пример #6
0
d = PeptideDescriptor(seqs=np.hstack((h.sequences, r.sequences, n.sequences)), scalename='pepcats')
d.calculate_crosscorr(window=7)

# train a som on the descriptors and print / plot the training error
som = SOM(x=12, y=12)
som.fit(data=d.descriptor, epochs=100000, decay='hill')
print("Fit error: %.4f" % som.error)
som.plot_error_history(filename="som_error.png")

# load known antimicrobial peptides (AMPs) and transmembrane sequences
dataset = load_AMPvsTM()
d2 = PeptideDescriptor(dataset.sequences, 'pepcats')
d2.calculate_crosscorr(7)
targets = np.array(libnum*[0] + libnum*[1] + libnum*[2] + 206*[3])
names = ['Helices', 'Random', 'nGrams', 'AMP']

# plot som maps with location of AMPs
som.plot_point_map(np.vstack((d.descriptor, d2.descriptor[206:])), targets, names, filename="peptidesom.png")
som.plot_density_map(np.vstack((d.descriptor, d2.descriptor)), filename="density.png")
som.plot_distance_map(colormap='Reds', filename="distances.png")

colormaps = ['Oranges', 'Purples', 'Greens', 'Reds']
for i, c in enumerate(set(targets)):
    som.plot_class_density(np.vstack((d.descriptor, d2.descriptor)), targets, c, names, colormap=colormaps[i],
                           filename='class%i.png' % c)

# get neighboring peptides (AMPs / TMs) for a sequence of interest
my_d = PeptideDescriptor(seqs='GLFDIVKKVVGALLAG', scalename='pepcats')
my_d.calculate_crosscorr(window=7)
som.get_neighbors(datapoint=my_d.descriptor, data=d2.descriptor, labels=dataset.sequences, d=0)
if __name__ == "__main__":
    with open('wine.data', 'r', encoding='utf-8') as f:
        data = f.read()
    data = data.split('\n')[:-1]
    X, Y = make_XY(data)
    X = feature_normalization(X)
    # exit()
    FEATURE_COUNT = len(X[0])
    CLASS_COUNT = len(list(set(Y)))
    print('FEATURE_COUNT:%d' % FEATURE_COUNT)
    print('CLASS_COUNT:%d' % CLASS_COUNT)

    som = SOM(8, 8)  # initialize the SOM
    som.fit(X, 10000, save_e=True, interval=100
            )  # fit the SOM for 10000 epochs, save the error every 100 steps
    som.plot_error_history(
        filename='images/som_error.png')  # plot the training error history

    # now visualize the learned representation with the class labels
    som.plot_point_map(X,
                       Y, ['Class %d' % (l + 1) for l in range(CLASS_COUNT)],
                       filename='images/som.png')
    for i in range(CLASS_COUNT):
        som.plot_class_density(X,
                               Y,
                               t=i,
                               name='Class %d' % (i + 1),
                               filename='images/class_%d.png' % (i + 1))
    som.plot_distance_map(filename='images/distance_map.png'
                          )  # plot the distance map after training
Пример #8
0
            targets_index.append(1)
        elif item == target_names[2]:
            targets_index.append(2)
        else:
            raise ValueError(targets)
    # now visualize the learned representation with the class labels

    som.plot_point_map(
        data,
        targets_index,
        target_names,
        filename=r'E:\_Python\SelfOrganizingMap\images\som.iris.png'
    )  # todo:have three classes
    som.plot_class_density(
        data,
        targets,
        t='Iris-setosa',
        name='setosa',
        filename=r'E:\_Python\SelfOrganizingMap\images\class_setosa.png')
    som.plot_class_density(
        data,
        targets,
        t='Iris-versicolor',
        name='versicolor',
        filename=r'E:\_Python\SelfOrganizingMap\images\class_versicolor.png')
    som.plot_class_density(
        data,
        targets,
        t='Iris-virginica',
        name='virginica',
        filename=r'E:\_Python\SelfOrganizingMap\images\class_virginica.png')
    som.plot_distance_map(