当前位置: 首页>>代码示例>>Python>>正文


Python MiniSom.quantization方法代码示例

本文整理汇总了Python中minisom.MiniSom.quantization方法的典型用法代码示例。如果您正苦于以下问题:Python MiniSom.quantization方法的具体用法?Python MiniSom.quantization怎么用?Python MiniSom.quantization使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在minisom.MiniSom的用法示例。


在下文中一共展示了MiniSom.quantization方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: imread

# 需要导入模块: from minisom import MiniSom [as 别名]
# 或者: from minisom.MiniSom import quantization [as 别名]
# read the image
img = imread('tree.jpg')

# reshaping the pixels matrix
pixels = reshape(img,(img.shape[0]*img.shape[1],3))

# SOM initialization and training
print('training...')
som = MiniSom(3,3,3,sigma=0.1,learning_rate=0.2) # 3x3 = 9 final colors
som.random_weights_init(pixels)
starting_weights = som.weights.copy() # saving the starting weights
som.train_random(pixels,100)

print('quantization...')
qnt = som.quantization(pixels) # quantize each pixels of the image
print('building new image...')
clustered = zeros(img.shape)
for i,q in enumerate(qnt): # place the quantized values into a new image
	clustered[unravel_index(i,dims=(img.shape[0],img.shape[1]))] = q
print('done.')

# show the result
figure(1)
subplot(221)
title('original')
imshow(flipud(img))
subplot(222)
title('result')
imshow(flipud(clustered))
开发者ID:JaySquare87,项目名称:minisom,代码行数:31,代码来源:example_color.py

示例2: setUp

# 需要导入模块: from minisom import MiniSom [as 别名]
# 或者: from minisom.MiniSom import quantization [as 别名]
class TestMinisom:

    def setUp(self):
        self.som = MiniSom(5, 5, 1)
        for w in self.som.weights:  # checking weights normalization
            assert_almost_equal(1.0, np.linalg.norm(w))
        self.som.weights = np.zeros((5, 5))  # fake weights
        self.som.weights[2, 3] = 5.0
        self.som.weights[1, 1] = 2.0

    def test_fast_norm(self):
        assert minisom.fast_norm(np.array([1, 3])) == sqrt(1 + 9)

    def test_gaussian(self):
        bell = minisom.gaussian((2, 2), 1, self.som.neigx, self.som.neigy)
        assert bell.max() == 1.0
        assert bell.argmax() == 12  # unravel(12) = (2,2)

    def test_win_map(self):
        winners = self.som.win_map([5.0, 2.0])
        assert winners[(2, 3)][0] == 5.0
        assert winners[(1, 1)][0] == 2.0

    def test_activation_reponse(self):
        response = self.som.activation_response([5.0, 2.0])
        assert response[2, 3] == 1
        assert response[1, 1] == 1

    def test_activate(self):
        assert self.som.activate(5.0).argmin() == 13.0  # unravel(13) = (2,3)

    def test_quantization_error(self):
        self.som.quantization_error([5, 2]) == 0.0
        self.som.quantization_error([4, 1]) == 0.5

    def test_quantization(self):
        q = self.som.quantization(np.array([4, 2]))
        assert q[0] == 5.0
        assert q[1] == 2.0

    # def test_random_seed(self):
    #     som1 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
    #     som2 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
    #     # same initialization
    #     assert_array_almost_equal(som1.weights, som2.weights)
    #     data = np.random.rand(100, 2)
    #     som1 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
    #     som1.train_random(data, 10)
    #     som2 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
    #     som2.train_random(data, 10)
    #     # same state after training
    #     assert_array_almost_equal(som1.weights, som2.weights)

    def test_train_batch(self):
        som = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
        data = np.array([[4, 2], [3, 1]])
        q1 = som.quantization_error(data)
        som.train_batch(data, 10)
        assert q1 > som.quantization_error(data)

    # def test_train_random(self):
    #     som = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
    #     data = np.array([[4, 2], [3, 1]])
    #     q1 = som.quantization_error(data)
    #     som.train_random(data, 10)
    #     assert q1 > som.quantization_error(data)

    def test_random_weights_init(self):
        som = MiniSom(2, 2, 2, sigma=0.1, random_seed=1)
        som.random_weights_init(np.array([[1.0, .0]]))
        for w in som.weights:
            assert_array_equal(w[0], np.array([1.0, .0]))
开发者ID:vitorhirota,项目名称:minisom,代码行数:74,代码来源:test.py

示例3: self_organizing_map

# 需要导入模块: from minisom import MiniSom [as 别名]
# 或者: from minisom.MiniSom import quantization [as 别名]
def self_organizing_map(image=None, images=None, weights=None, weights_max_value=1, n_colors=64,
                        dim=None, num_training=1000, std_multiple=0, threshold=False, show_plot=False, show_color_space_assignment=False):
    """
    Given an image or stack of images (list of np arrays), cluster with an SOM and return a list of the color centers
    num_training is the number of pixels used to train
    weights is a list of rgb values to initialize the algorithm with
    weights_max_value is a number representing the maximum possible value so that weights can be normalized to a 0-1 scale
    n_colors is the number of clusters
    dim is the dimensions of the nodes in the SOM. Total number of nodes should be the same as the number specified
    threshold is a boolean whether to consider the black pixels or not
    std_multiple is the standard deviation multiple used in thresholding
    show_plot is a boolean whether to show the quantized image or not
    show_color_space_assignment is a boolean whether to show the way the color space is clustered or not

    """
    neuron_pixels, non_neuron_pixels, pixels, image = sample_data(image, images, std_multiple)

    if dim is None and weights is not None:
        # normalize weights
        weights = (np.array(weights) / weights_max_value).tolist()

        # figure out a way to spread out the nodes of the som and find the int factor closest to the square root
        factor = get_factor_closest_to_sqrt(len(weights))

        # it's prime if the factor is 1
        if factor == 1:
            # add a random weight to make the number of nodes even
            weights = np.vstack((weights, np.random.random(3)))

        # should be fine now
        factor = get_factor_closest_to_sqrt(len(weights))
        dim = (factor, len(weights) / factor)
        weights = np.reshape(weights, (dim[0], dim[1], 3))

    else:
        # there are no weights to initialize
        if n_colors == 2 or n_colors == 3:
            dim = (1, n_colors)
        else:
            factor = get_factor_closest_to_sqrt(n_colors)
            # it's prime if the factor is 1
            if factor == 1:
                # increase the number of colors by one
                n_colors += 1
            # should be fine now
            factor = get_factor_closest_to_sqrt(n_colors)
            dim = (factor, n_colors / factor)

    # determine the dimensions
    som = MiniSom(dim[0], dim[1], 3, weights=weights, sigma=0.1, learning_rate=0.2)
    if weights is None:
        if threshold:
            som.random_weights_init(neuron_pixels)
        else:
            som.random_weights_init(pixels)

    if threshold:
        # get mostly bright pixels with a bit of background
        som.train_random(neuron_pixels, num_training)
    else:
        som.train_random(pixels, num_training)

    if show_plot:
        qnt = som.quantization(pixels)  # quantize each pixels of the image
        clustered = np.zeros(image.shape)
        for i, q in enumerate(qnt):
            clustered[np.unravel_index(i, dims=(image.shape[0], image.shape[1]))] = q

        fig = plt.figure()
        ax = fig.add_subplot(1, 2, 1)
        plt.imshow(image)
        ax.set_title('Original')
        ax = fig.add_subplot(1, 2, 2)
        plt.imshow(clustered)
        ax.set_title('After SOM Clustering')
        plt.show()

    if show_color_space_assignment:
        for intensity in [.1, .2, .4, .6, .8]:
            visualize_color_space(som=som, intensity=intensity)

    return np.reshape(som.weights, (som.weights.shape[0] * som.weights.shape[1], 3))
开发者ID:Lawrence-Moore,项目名称:NeuronTracing,代码行数:84,代码来源:clustering.py

示例4: MiniSom

# 需要导入模块: from minisom import MiniSom [as 别名]
# 或者: from minisom.MiniSom import quantization [as 别名]
model = Doc2Vec.load("../model/doc_model.mod")
doc_labels = random.sample(model.docvecs.doctags.keys(), 4000)

#### selection 
doc_vecs = []
for label in doc_labels:
	doc_vecs += [model.docvecs[label]]
doc_vecs = np.array(doc_vecs)

####

print "Clustering..."
N_CLUSTERS = 4
som = MiniSom(4, 4, 64, sigma=0.3, learning_rate=0.5)
som.train_random(doc_vecs, 100)
qnt = som.quantization(doc_vecs)

uniques = []
for i in qnt:
	has_it = False
	for elem in uniques:
		if np.array_equal(elem, i):
			has_it = True
	if not has_it:
		uniques += [i]

####

def get_similar_words(doc):
	score_dict = {}
	for word in model.vocab.keys():
开发者ID:PragmaticLab,项目名称:w2v_to_d2v,代码行数:33,代码来源:som.py


注:本文中的minisom.MiniSom.quantization方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。