def group_generation(nbGroupes: int, n: int, offset: float = .2) ->list: """ Generates points in distincts groups Same number of points per group Points's coordinates between 0 and 1 """ angle = 0 centroidsX = [] centroidsY = [] while(angle < 2*math.pi): # Generate groups's centers centroidsX.append(math.cos(angle)) centroidsY.append(math.sin(angle)) angle += (2 * math.pi) / nbGroupes points = [] cl = color_generation(nbGroupes) for i in range(nbGroupes): # Generate points for each group points.extend([ norm(uniform(centroidsX[i] - offset, centroidsX[i] + offset), -1 - offset, 1 + offset), norm(uniform(centroidsY[i] - offset, centroidsY[i] + offset), -1 - offset, 1 + offset), cl[i] ] for j in range(n)) return points
def random_generation(n: int, nbClass: int, dim: int = 2) ->list: """ Generates points with random coordinates and a random class Coordinates between 0 and 1 """ p = [] cl = color_generation(nbClass) for i in range(n): p.append([uniform(0, 1) for j in range(dim)]) p[-1].append(choice(cl)) return p
def percent_generation(percentages: list, n: int, offset: float = .2) ->list: """ Generates points in distincts group Variable amount of points per group determined by the percentages parameter Points's coordinates between 0 and 1 """ # Check if the percentages are correct if(sum(percentages) > 1 or sum(percentages) <= 0): raise ValueError("The given percentages aren't correct, sum > 1 or sum <= 0") nbGroupes = len(percentages) cl = color_generation(nbGroupes) # DEBUG : print(percentages) # Conversion : from percents to number of points for i in range(nbGroupes): percentages[i] = int(n * percentages[i]) # Number of points lost because of the divisions lostpoints = n - sum(percentages) print("%s points haven't been placed" % str(lostpoints)) # DEBUG : print(percentages) # Groups positions angle = 0 centroidsX = [] centroidsY = [] while(angle < 2*math.pi): # Generate groups's centers centroidsX.append(math.cos(angle)) centroidsY.append(math.sin(angle)) angle += (2 * math.pi) / nbGroupes # Points generation points = [] for i in range(nbGroupes): # Generate the amount of points specified by the percentages list points.extend([ norm(uniform(centroidsX[i] - offset, centroidsX[i] + offset), -1 - offset, 1 + offset), norm(uniform(centroidsY[i] - offset, centroidsY[i] + offset), -1 - offset, 1 + offset), cl[i] ] for j in range(percentages[i])) return points