Beispiel #1
0
    def plot(self, x, y, weights=None, title='Linear Regression'):
        """
        Plot the input values x with their corresponding output values y (either true or predicted).
        Also, plot the linear regression line if weights are given; assuming h_w(x) = weights[0]*x + weights[1].
        
        This will draw on the existing pacman window (clearing it first) or create a new one if no window exists.
        
        x: array or list of N scalar values.
        y: array or list of N scalar values.
        weights: array or list of 2 values (or if just one value, the bias weight is assumed to be zero). If None,
            no line is drawn. Default: None
        """
        if np.array(x).size == 0:
            return

        if isinstance(x[0], np.ndarray):
            # Scrape the first element of each data point
            x = [data[0] for data in x]

        xmin = int(math.floor(min(x)))
        ymin = int(math.floor(min(y)))
        xmax = int(math.ceil(max(x)))
        ymax = int(math.ceil(max(y)))
        width = xmax - xmin + 3
        height = ymax - ymin + 3
        self.initPlot(xmin, ymin, width, height)

        gameState = self.blankGameState.deepCopy()

        gameState.agentStates = []

        # Put pacman in bottom left
        if self.addPacmanToLineStart is True:
            gameState.agentStates.append(
                AgentState(Configuration((1, 1), Directions.STOP), True))

        # Add ghost at each point
        for (px, py) in zip(x, y):
            point = (px + self.xShift, py + self.yShift)
            gameState.agentStates.append(
                AgentState(Configuration(point, Directions.STOP), False))

#         self.initialize(gameState)
        graphicsUtils.clear_screen()
        self.infoPane = InfoPane(gameState.layout, self.gridSize)
        self.drawStaticObjects(gameState)
        self.drawAgentObjects(gameState)

        graphicsUtils.changeText(self.infoPane.scoreText, title)
        graphicsUtils.refresh()
        graphicsUtils.sleep(1)

        if weights is not None:
            self.setWeights(weights)
Beispiel #2
0
    def plot(self, x, y, weights=None, title='Linear Classification'):
        """
        Plot the 2D input points, data[i], colored based on their corresponding labels (either true or predicted).
        Also, plot the linear separator line if weights are given.

        This will draw on the existing pacman window (clearing it first) or create a new one if no window exists.

        x: list of 2D points, where each 2D point in the list is a 2 element numpy.ndarray
        y: list of N labels, one for each point in data. Labels can be of any type that can be converted
            a string.
        weights: array of 3 values the first two are the weight on the data and the third value is the bias
        weight term. If there are only 2 values in weights, the bias term is assumed to be zero.  If None,
        no line is drawn. Default: None
        """
        if np.array(x).size == 0:
            return

        # Process data, sorting by label
        possibleLabels = list(set(y))
        sortedX1 = {}
        sortedX2 = {}
        for label in possibleLabels:
            sortedX1[label] = []
            sortedX2[label] = []

        for i in range(len(x)):
            sortedX1[y[i]].append(x[i][0])
            sortedX2[y[i]].append(x[i][1])

        x1min = float("inf")
        x1max = float("-inf")
        for x1Values in sortedX1.values():
            x1min = min(min(x1Values), x1min)
            x1max = max(max(x1Values), x1max)
        x2min = float("inf")
        x2max = float("-inf")
        for x2Values in sortedX2.values():
            x2min = min(min(x2Values), x2min)
            x2max = max(max(x2Values), x2max)

        x1min = int(math.floor(x1min))
        x1max = int(math.ceil(x1max))
        x2min = int(math.floor(x2min))
        x2max = int(math.ceil(x2max))

        width = x1max - x1min + 3
        height = x2max - x2min + 3
        self.initPlot(x1min, x2min, width, height)

        gameState = self.blankGameState.deepCopy()

        gameState.agentStates = []

        # Add ghost/pacman at each point
        for (labelIndex, label) in enumerate(possibleLabels):
            pointsX1 = sortedX1[label]
            pointsX2 = sortedX2[label]
            for (px, py) in zip(pointsX1, pointsX2):
                point = (px + self.xShift, py + self.yShift)
                agent = AgentState(Configuration(point, Directions.STOP),
                                   False)
                agent.isPacman = (labelIndex == 0)
                if labelIndex == 2:
                    agent.scaredTimer = 1
                gameState.agentStates.append(agent)


#         self.initialize(gameState)
        graphicsUtils.clear_screen()
        self.infoPane = InfoPane(gameState.layout, self.gridSize)
        self.drawStaticObjects(gameState)
        self.drawAgentObjects(gameState)

        graphicsUtils.changeText(self.infoPane.scoreText, title)
        graphicsUtils.refresh()

        if weights is not None:
            self.setWeights(weights)
Beispiel #3
0
    def plot(self, x, y, weights=None, title='Logistic Regression'):
        """
        Plot the 1D input points, data[i], colored based on their corresponding labels (either true or predicted).
        Also, plot the logistic function fit if weights are given.

        This will draw on the existing pacman window (clearing it first) or create a new one if no window exists.

        x: list of 1D points, where each 1D point in the list is a 1 element numpy.ndarray
        y: list of N labels, one for each point in data. Labels can be of any type that can be converted
            a string.
        weights: array of 2 values the first one is the weight on the data and the second value is the bias weight term.
        If there are only 1 values in weights,
            the bias term is assumed to be zero.  If None, no line is drawn. Default: None
        """
        if np.array(x).size == 0:
            return

        # Process data, sorting by label
        possibleLabels = list(set(y))
        sortedX = {}
        for label in possibleLabels:
            sortedX[label] = []

        for i in range(len(x)):
            sortedX[y[i]].append(x[i])

        xmin = int(math.floor(min(x)))
        xmax = int(math.ceil(max(x)))
        ymin = int(math.floor(0)) - 1
        ymax = int(math.ceil(1))
        width = xmax - xmin + 3
        height = ymax - ymin + 3
        self.initPlot(xmin, ymin, width, height)

        gameState = self.blankGameState.deepCopy()

        gameState.agentStates = []

        # Put pacman in bottom left
        if self.addPacmanToLineStart is True:
            gameState.agentStates.append(
                AgentState(Configuration((1, 1), Directions.STOP), True))

        # Add ghost at each point
        for (py, label) in enumerate(possibleLabels):
            pointsX = sortedX[label]
            for px in pointsX:
                point = (px + self.xShift, py + self.yShift)
                agent = AgentState(Configuration(point, Directions.STOP),
                                   False)
                agent.isPacman = 1 - py
                gameState.agentStates.append(agent)

#         self.initialize(gameState)
        graphicsUtils.clear_screen()
        self.infoPane = InfoPane(gameState.layout, self.gridSize)
        self.drawStaticObjects(gameState)
        self.drawAgentObjects(gameState)

        graphicsUtils.changeText(self.infoPane.scoreText, title)
        graphicsUtils.refresh()

        if weights is not None:
            self.setWeights(weights)