def get_distance_matrix(self): """! @brief Calculates distance matrix (U-matrix). @details The U-Matrix visualizes based on the distance in input space between a weight vector and its neighbors on map. @return (list) Distance matrix (U-matrix). @see show_distance_matrix() @see get_density_matrix() """ if self.__ccore_som_pointer is not None: self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) if self._conn_type != type_conn.func_neighbor: self._neighbors = wrapper.som_get_neighbors(self.__ccore_som_pointer) distance_matrix = [[0.0] * self._cols for i in range(self._rows)] for i in range(self._rows): for j in range(self._cols): neuron_index = i * self._cols + j if self._conn_type == type_conn.func_neighbor: self._create_connections(type_conn.grid_eight) for neighbor_index in self._neighbors[neuron_index]: distance_matrix[i][j] += euclidean_distance_square(self._weights[neuron_index], self._weights[neighbor_index]) distance_matrix[i][j] /= len(self._neighbors[neuron_index]) return distance_matrix
def get_distance_matrix(self): """! @brief Calculates distance matrix (U-matrix). @details The U-Matrix visualizes based on the distance in input space between a weight vector and its neighbors on map. @return (list) Distance matrix (U-matrix). @see show_distance_matrix() @see get_density_matrix() """ if (self.__ccore_som_pointer is not None): self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) if (self._conn_type != type_conn.func_neighbor): self._neighbors = wrapper.som_get_neighbors( self.__ccore_som_pointer) distance_matrix = [[0.0] * self._cols for i in range(self._rows)] for i in range(self._rows): for j in range(self._cols): neuron_index = i * self._cols + j if (self._conn_type == type_conn.func_neighbor): self._create_connections(type_conn.grid_eight) for neighbor_index in self._neighbors[neuron_index]: distance_matrix[i][j] += euclidean_distance_sqrt( self._weights[neuron_index], self._weights[neighbor_index]) distance_matrix[i][j] /= len(self._neighbors[neuron_index]) return distance_matrix
def weights(self): """! @return (list) Weights of each neuron. """ if (self.__ccore_som_pointer is not None): self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) return self._weights
def weights(self): """! @return (list) Weights of each neuron. """ if (self.__ccore_som_pointer is not None): self._weights = wrapper.som_get_weights(self.__ccore_som_pointer); return self._weights;
def weights(self): """! @brief Return weight of each neuron. @return (list) Weights of each neuron. """ if self.__ccore_som_pointer is not None: self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) return self._weights
def get_density_matrix(self, surface_divider=20.0): """! @brief Calculates density matrix (P-Matrix). @param[in] surface_divider (double): Divider in each dimension that affect radius for density measurement. @return (list) Density matrix (P-Matrix). @see get_distance_matrix() """ if (self.__ccore_som_pointer is not None): self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) density_matrix = [[0] * self._cols for i in range(self._rows)] dimension = len(self._weights[0]) dim_max = [float('-Inf')] * dimension dim_min = [float('Inf')] * dimension for weight in self._weights: for index_dim in range(dimension): if (weight[index_dim] > dim_max[index_dim]): dim_max[index_dim] = weight[index_dim] if (weight[index_dim] < dim_min[index_dim]): dim_min[index_dim] = weight[index_dim] radius = [0.0] * len(self._weights[0]) for index_dim in range(dimension): radius[index_dim] = (dim_max[index_dim] - dim_min[index_dim]) / surface_divider for point in self._data: for index_neuron in range(len(self)): point_covered = True for index_dim in range(dimension): if (abs(point[index_dim] - self._weights[index_neuron][index_dim]) > radius[index_dim]): point_covered = False break row = math.floor(index_neuron / self._cols) col = index_neuron - row * self._cols if (point_covered is True): density_matrix[row][col] += 1 return density_matrix
def get_density_matrix(self, surface_divider = 20.0): """! @brief Calculates density matrix (P-Matrix). @param[in] surface_divider (double): Divider in each dimension that affect radius for density measurement. @return (list) Density matrix (P-Matrix). @see get_distance_matrix() """ if self.__ccore_som_pointer is not None: self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) density_matrix = [[0] * self._cols for i in range(self._rows)] dimension = len(self._weights[0]) dim_max = [ float('-Inf') ] * dimension dim_min = [ float('Inf') ] * dimension for weight in self._weights: for index_dim in range(dimension): if weight[index_dim] > dim_max[index_dim]: dim_max[index_dim] = weight[index_dim] if weight[index_dim] < dim_min[index_dim]: dim_min[index_dim] = weight[index_dim] radius = [0.0] * len(self._weights[0]) for index_dim in range(dimension): radius[index_dim] = ( dim_max[index_dim] - dim_min[index_dim] ) / surface_divider ## TODO: do not use data for point in self._data: for index_neuron in range(len(self)): point_covered = True for index_dim in range(dimension): if abs(point[index_dim] - self._weights[index_neuron][index_dim]) > radius[index_dim]: point_covered = False break row = int(math.floor(index_neuron / self._cols)) col = index_neuron - row * self._cols if point_covered is True: density_matrix[row][col] += 1 return density_matrix
def get_density_matrix(self, surface_divider = 20.0): """! @brief Calculates density matrix (P-Matrix). @param[in] surface_divider (double): Divider in each dimension that affect radius for density measurement. @return (list) Density matrix (P-Matrix). @see get_distance_matrix() """ if (self.__ccore_som_pointer is not None): self._weights = wrapper.som_get_weights(self.__ccore_som_pointer); density_matrix = [ [0] * self._cols for i in range(self._rows) ]; dimension = len(self._weights[0]); dim_max = [ float('-Inf') ] * dimension; dim_min = [ float('Inf') ] * dimension; for weight in self._weights: for index_dim in range(dimension): if (weight[index_dim] > dim_max[index_dim]): dim_max[index_dim] = weight[index_dim]; if (weight[index_dim] < dim_min[index_dim]): dim_min[index_dim] = weight[index_dim]; radius = [0.0] * len(self._weights[0]); for index_dim in range(dimension): radius[index_dim] = ( dim_max[index_dim] - dim_min[index_dim] ) / surface_divider; for point in self._data: for index_neuron in range(len(self)): point_covered = True; for index_dim in range(dimension): if (abs(point[index_dim] - self._weights[index_neuron][index_dim]) > radius[index_dim]): point_covered = False; break; row = math.floor(index_neuron / self._cols); col = index_neuron - row * self._cols; if (point_covered is True): density_matrix[row][col] += 1; return density_matrix;
def show_network(self, awards=False, belongs=False, coupling=True, dataset=True, marker_type='o'): """! @brief Shows neurons in the dimension of data. @param[in] awards (bool): If True - displays how many objects won each neuron. @param[in] belongs (bool): If True - marks each won object by according index of neuron-winner (only when dataset is displayed too). @param[in] coupling (bool): If True - displays connections between neurons (except case when function neighbor is used). @param[in] dataset (bool): If True - displays inputs data set. @param[in] marker_type (string): Defines marker that is used for dispaying neurons in the network. """ if (self.__ccore_som_pointer is not None): self._size = wrapper.som_get_size(self.__ccore_som_pointer) self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) self._neighbors = wrapper.som_get_neighbors( self.__ccore_som_pointer) self._award = wrapper.som_get_awards(self.__ccore_som_pointer) dimension = len(self._weights[0]) fig = plt.figure() axes = None # Check for dimensions if ((dimension == 1) or (dimension == 2)): axes = fig.add_subplot(111) elif (dimension == 3): axes = fig.gca(projection='3d') else: raise NameError( 'Dwawer supports only 1D, 2D and 3D data representation') # Show data if ((self._data is not None) and (dataset is True)): for x in self._data: if (dimension == 1): axes.plot(x[0], 0.0, 'b|', ms=30) elif (dimension == 2): axes.plot(x[0], x[1], 'b.') elif (dimension == 3): axes.scatter(x[0], x[1], x[2], c='b', marker='.') # Show neurons for index in range(self._size): color = 'g' if (self._award[index] == 0): color = 'y' if (dimension == 1): axes.plot(self._weights[index][0], 0.0, color + marker_type) if (awards == True): location = '{0}'.format(self._award[index]) axes.text(self._weights[index][0], 0.0, location, color='black', fontsize=10) if (belongs == True): location = '{0}'.format(index) axes.text(self._weights[index][0], 0.0, location, color='black', fontsize=12) for k in range(len(self._capture_objects[index])): point = self._data[self._capture_objects[index][k]] axes.text(point[0], 0.0, location, color='blue', fontsize=10) if (dimension == 2): axes.plot(self._weights[index][0], self._weights[index][1], color + marker_type) if (awards == True): location = '{0}'.format(self._award[index]) axes.text(self._weights[index][0], self._weights[index][1], location, color='black', fontsize=10) if (belongs == True): location = '{0}'.format(index) axes.text(self._weights[index][0], self._weights[index][1], location, color='black', fontsize=12) for k in range(len(self._capture_objects[index])): point = self._data[self._capture_objects[index][k]] axes.text(point[0], point[1], location, color='blue', fontsize=10) if ((self._conn_type != type_conn.func_neighbor) and (coupling != False)): for neighbor in self._neighbors[index]: if (neighbor > index): axes.plot([ self._weights[index][0], self._weights[neighbor][0] ], [ self._weights[index][1], self._weights[neighbor][1] ], 'g', linewidth=0.5) elif (dimension == 3): axes.scatter(self._weights[index][0], self._weights[index][1], self._weights[index][2], c=color, marker=marker_type) if ((self._conn_type != type_conn.func_neighbor) and (coupling != False)): for neighbor in self._neighbors[index]: if (neighbor > index): axes.plot([ self._weights[index][0], self._weights[neighbor][0] ], [ self._weights[index][1], self._weights[neighbor][1] ], [ self._weights[index][2], self._weights[neighbor][2] ], 'g-', linewidth=0.5) plt.title("Network Structure") plt.grid() plt.show()
def __download_dump_from_ccore(self): self._location = self.__initialize_locations(self._rows, self._cols) self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) self._award = wrapper.som_get_awards(self.__ccore_som_pointer) self._capture_objects = wrapper.som_get_capture_objects(self.__ccore_som_pointer)
def show_network(self, awards = False, belongs = False, coupling = True, dataset = True, marker_type = 'o'): """! @brief Shows neurons in the dimension of data. @param[in] awards (bool): If True - displays how many objects won each neuron. @param[in] belongs (bool): If True - marks each won object by according index of neuron-winner (only when dataset is displayed too). @param[in] coupling (bool): If True - displays connections between neurons (except case when function neighbor is used). @param[in] dataset (bool): If True - displays inputs data set. @param[in] marker_type (string): Defines marker that is used for dispaying neurons in the network. """ if self.__ccore_som_pointer is not None: self._size = wrapper.som_get_size(self.__ccore_som_pointer) self._weights = wrapper.som_get_weights(self.__ccore_som_pointer) self._neighbors = wrapper.som_get_neighbors(self.__ccore_som_pointer) self._award = wrapper.som_get_awards(self.__ccore_som_pointer) dimension = len(self._weights[0]) fig = plt.figure() # Check for dimensions if (dimension == 1) or (dimension == 2): axes = fig.add_subplot(111) elif dimension == 3: axes = fig.gca(projection='3d') else: raise NotImplementedError('Impossible to show network in data-space that is differ from 1D, 2D or 3D.') if (self._data is not None) and (dataset is True): for x in self._data: if dimension == 1: axes.plot(x[0], 0.0, 'b|', ms = 30) elif dimension == 2: axes.plot(x[0], x[1], 'b.') elif dimension == 3: axes.scatter(x[0], x[1], x[2], c = 'b', marker = '.') # Show neurons for index in range(self._size): color = 'g' if self._award[index] == 0: color = 'y' if dimension == 1: axes.plot(self._weights[index][0], 0.0, color + marker_type) if awards: location = '{0}'.format(self._award[index]) axes.text(self._weights[index][0], 0.0, location, color='black', fontsize = 10) if belongs and self._data is not None: location = '{0}'.format(index) axes.text(self._weights[index][0], 0.0, location, color='black', fontsize = 12) for k in range(len(self._capture_objects[index])): point = self._data[self._capture_objects[index][k]] axes.text(point[0], 0.0, location, color='blue', fontsize = 10) if dimension == 2: axes.plot(self._weights[index][0], self._weights[index][1], color + marker_type) if awards: location = '{0}'.format(self._award[index]) axes.text(self._weights[index][0], self._weights[index][1], location, color='black', fontsize=10) if belongs and self._data is not None: location = '{0}'.format(index) axes.text(self._weights[index][0], self._weights[index][1], location, color='black', fontsize=12) for k in range(len(self._capture_objects[index])): point = self._data[self._capture_objects[index][k]] axes.text(point[0], point[1], location, color='blue', fontsize=10) if (self._conn_type != type_conn.func_neighbor) and (coupling != False): for neighbor in self._neighbors[index]: if neighbor > index: axes.plot([self._weights[index][0], self._weights[neighbor][0]], [self._weights[index][1], self._weights[neighbor][1]], 'g', linewidth=0.5) elif dimension == 3: axes.scatter(self._weights[index][0], self._weights[index][1], self._weights[index][2], c=color, marker=marker_type) if (self._conn_type != type_conn.func_neighbor) and (coupling != False): for neighbor in self._neighbors[index]: if neighbor > index: axes.plot([self._weights[index][0], self._weights[neighbor][0]], [self._weights[index][1], self._weights[neighbor][1]], [self._weights[index][2], self._weights[neighbor][2]], 'g-', linewidth=0.5) plt.title("Network Structure") plt.grid() plt.show()
def show_network(self, awards = False, belongs = False, coupling = True, dataset = True, marker_type = 'o'): """! @brief Shows neurons in the dimension of data. @param[in] awards (bool): If True - displays how many objects won each neuron. @param[in] belongs (bool): If True - marks each won object by according index of neuron-winner (only when dataset is displayed too). @param[in] coupling (bool): If True - displays connections between neurons (except case when function neighbor is used). @param[in] dataset (bool): If True - displays inputs data set. @param[in] marker_type (string): Defines marker that is used for dispaying neurons in the network. """ if (self.__ccore_som_pointer is not None): self._size = wrapper.som_get_size(self.__ccore_som_pointer); self._weights = wrapper.som_get_weights(self.__ccore_som_pointer); self._neighbors = wrapper.som_get_neighbors(self.__ccore_som_pointer); self._award = wrapper.som_get_awards(self.__ccore_som_pointer); dimension = len(self._weights[0]); fig = plt.figure(); axes = None; # Check for dimensions if ( (dimension == 1) or (dimension == 2) ): axes = fig.add_subplot(111); elif (dimension == 3): axes = fig.gca(projection='3d'); else: raise NameError('Dwawer supports only 1D, 2D and 3D data representation'); # Show data if (dataset == True): for x in self._data: if (dimension == 1): axes.plot(x[0], 0.0, 'b|', ms = 30); elif (dimension == 2): axes.plot(x[0], x[1], 'b.'); elif (dimension == 3): axes.scatter(x[0], x[1], x[2], c = 'b', marker = '.'); # Show neurons for index in range(self._size): color = 'g'; if (self._award[index] == 0): color = 'y'; if (dimension == 1): axes.plot(self._weights[index][0], 0.0, color + marker_type); if (awards == True): location = '{0}'.format(self._award[index]); axes.text(self._weights[index][0], 0.0, location, color='black', fontsize = 10); if (belongs == True): location = '{0}'.format(index); axes.text(self._weights[index][0], 0.0, location, color='black', fontsize = 12); for k in range(len(self._capture_objects[index])): point = self._data[self._capture_objects[index][k]]; axes.text(point[0], 0.0, location, color='blue', fontsize = 10); if (dimension == 2): axes.plot(self._weights[index][0], self._weights[index][1], color + marker_type); if (awards == True): location = '{0}'.format(self._award[index]); axes.text(self._weights[index][0], self._weights[index][1], location, color='black', fontsize = 10); if (belongs == True): location = '{0}'.format(index); axes.text(self._weights[index][0], self._weights[index][1], location, color='black', fontsize = 12); for k in range(len(self._capture_objects[index])): point = self._data[self._capture_objects[index][k]]; axes.text(point[0], point[1], location, color='blue', fontsize = 10); if ( (self._conn_type != type_conn.func_neighbor) and (coupling != False) ): for neighbor in self._neighbors[index]: if (neighbor > index): axes.plot([self._weights[index][0], self._weights[neighbor][0]], [self._weights[index][1], self._weights[neighbor][1]], 'g', linewidth = 0.5); elif (dimension == 3): axes.scatter(self._weights[index][0], self._weights[index][1], self._weights[index][2], c = color, marker = marker_type); if ( (self._conn_type != type_conn.func_neighbor) and (coupling != False) ): for neighbor in self._neighbors[index]: if (neighbor > index): axes.plot([self._weights[index][0], self._weights[neighbor][0]], [self._weights[index][1], self._weights[neighbor][1]], [self._weights[index][2], self._weights[neighbor][2]], 'g-', linewidth = 0.5); plt.title("Network Structure"); plt.grid(); plt.show();