示例#1
0
 def setUp(self) -> None:
     """
     Sets up unittest
     """
     self.a_line = Line(M, N, (MIN_X, MAX_X), (MIN_Y, MAX_Y))
     self.m = self.a_line.m
     self.n = self.a_line.n
 def setUp(self) -> None:
     """
     Sets up unittest
     """
     a_line = Line((1, 1), (0, 0), (-1, 1), (-1, 1))
     self.train_set = a_line.training_set(1000)
     self.file_path = path.dirname(path.abspath(__file__))
示例#3
0
 def __init__(self, m_range: (float, float),
              n_range: ((float, float), (float, float)),
              x_range: (float, float),
              y_range: (float, float)):
     super().__init__(x_range, y_range, "DoubleLine")
     self.m = uniform(m_range[0], m_range[1])
     n1 = uniform(n_range[0][0], n_range[0][1])
     n2 = uniform(n_range[1][0], n_range[1][1])
     self.line1 = Line((self.m, self.m), (n1, n1), x_range, y_range)
     self.line2 = Line((self.m, self.m), (n2, n2), x_range, y_range)
     return
示例#4
0
class TestLine(TestCase):
    def setUp(self) -> None:
        """
        Sets up unittest
        """
        self.a_line = Line(M, N, (MIN_X, MAX_X), (MIN_Y, MAX_Y))
        self.m = self.a_line.m
        self.n = self.a_line.n

    def test_x_to_y(self):
        x = 20
        expected = self.m * x + self.n
        actual = self.a_line.x_to_y(x)
        assert abs(actual[0] - expected) < EPSILON

    def test_y_to_x(self):
        y = 20
        expected = (y - self.n) / self.m
        actual = self.a_line.y_to_x(y)
        assert abs(actual[0] - expected) < EPSILON

    def test_is_above(self):
        x = 0
        y = 40
        assert self.a_line.is_above(x, y)
        x = 1
        y = self.a_line.x_to_y(x)[0]
        y1 = y + 0.5
        y2 = y - 0.5
        assert self.a_line.is_above(x, y1)
        assert not self.a_line.is_above(x, y2)

    def test_graph(self):
        x, y = self.a_line.graph()
        assert len(x) == len(y)
FIG_SIZE = (12 * 3, 12)
X_MIN = Y_MIN = -50
X_MAX = Y_MAX = 50
SLOPE = (-2.0, 2.0)
INTERCEPT = (-10.0, 10.0)
N = 100
TO_SHOW = 1500

np.random.seed(3)
seed(3)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    # Predict a line
    line = Line(SLOPE, INTERCEPT, (X_MIN, X_MAX), (Y_MIN, Y_MAX))

    # Initialize perceptron
    perceptron = LearningPerceptron("learning perceptron", 2, 0.1)

    # Random dataset
    train_set = line.training_set(N)
    test_set = line.training_set(TO_SHOW)

    # First plot
    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=FIG_SIZE)
    prediction = do_prediction(perceptron, test_set)
    logging.info("First Plot")
    plot_result(test_set, np.array(prediction), line, ax1)
    ax1.set_title("Before training\n", fontsize=20)
示例#6
0
class DoubleLine(Pattern):
    """Double Line, define points inside and outside two line on plane"""

    def __init__(self, m_range: (float, float),
                 n_range: ((float, float), (float, float)),
                 x_range: (float, float),
                 y_range: (float, float)):
        super().__init__(x_range, y_range, "DoubleLine")
        self.m = uniform(m_range[0], m_range[1])
        n1 = uniform(n_range[0][0], n_range[0][1])
        n2 = uniform(n_range[1][0], n_range[1][1])
        self.line1 = Line((self.m, self.m), (n1, n1), x_range, y_range)
        self.line2 = Line((self.m, self.m), (n2, n2), x_range, y_range)
        return

    def x_to_y(self, x: float) -> [float]:
        """
        Gives y values from x value

        :param x: abscissa coordinate
        :return: ordinate coordinate
        """
        return [self.line1.x_to_y(x)[0], self.line2.x_to_y(x)[0]]

    def y_to_x(self, y: float) -> [float]:
        """
        Gives x values from y value

        :param y: ordinate coordinate
        :return: abscissa coordinate
        """
        return [self.line1.y_to_x(y)[0], self.line2.y_to_x(y)[0]]

    # outside
    def is_above(self, x: float, y: float) -> bool:
        """
        Return if a given point is above pattern

        :param x: x coordinate
        :param y: y coordinate
        :return: Whether the point is above
        """
        return not self.line1.is_above(x, y) or self.line2.is_above(x, y)

    def graph(self) -> ([float], [float]):
        """
        Gives values to be graph

        :return: Two arrays to be used for graphic pattern
        """
        x1, y1 = self.line1.graph()
        x2, y2 = self.line2.graph()
        return x1 + x2, y1 + y2

    def add_results(self, ax: Axes) -> None:
        """
        Add pattern to a Axes figure on matplotlib

        :param ax: Axes in which results will be inserted
        :return: None (update ax)
        """
        x, y = self.graph()
        x1 = x[0:2]
        x2 = x[2:4]
        y1 = y[0:2]
        y2 = y[2:4]
        ax.plot(x1, y1, "-", label="Line 1")
        ax.plot(x2, y2, "-", label="Line 2")
示例#7
0
X_MAX = Y_MAX = 50
FIG_SIZE = (6 * 4 + 2, 6)
TITLE_SIZE = 20
RADIUS = 30
FACE = 50
CENTER = (0.0, 0.0)
SLOPE = (-2.0, 2.0)
INTERCEPT = (-10.0, 10.0)
INTERCEPT1 = (-40.0, -20.0)
INTERCEPT2 = (20.0, 40.0)
TWO_INTERCEPT = (INTERCEPT1, INTERCEPT2)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    line = Line(SLOPE, INTERCEPT, (X_MIN, X_MAX), (Y_MIN, Y_MAX))
    double_line = DoubleLine(SLOPE, TWO_INTERCEPT, (X_MIN, X_MAX),
                             (Y_MIN, Y_MAX))
    circle = Circle(RADIUS, CENTER, (X_MIN, X_MAX), (Y_MIN, Y_MAX))
    square = Square(FACE, CENTER, (X_MIN, X_MAX), (Y_MIN, Y_MAX))

    fig = plt.figure(figsize=FIG_SIZE)
    ax1 = fig.add_subplot(141)
    ax2 = fig.add_subplot(142)
    ax3 = fig.add_subplot(143)
    ax4 = fig.add_subplot(144)

    figures = {line: ax1, double_line: ax2, circle: ax3, square: ax4}

    for figure in figures.keys():
        logging.info("Generating {}".format(figure.name))