def plotProgresskMeans(X, centroids, previous, idx, K, i): """Display the progress of k-Means as it is running.""" util.plotDataPoints(X, idx) plt.plot(centroids[:, 0], centroids[:, 1], 'kx') for j in range(len(centroids)): # plt.plot([centroids[j, 0], previous[j, 0]], # [centroids[j, 1], previous[j, 1]], 'k') util.drawLine(centroids[j, :], previous[j, :], 'k') plt.title('Iteration number %d' % (i + 1))
def OnPaint(self, event): dc = wx.BufferedPaintDC(self, self.screenBuf) size = self.GetClientSize() dc.SetBrush(wx.WHITE_BRUSH) dc.SetPen(wx.WHITE_PEN) dc.DrawRectangle(0, 0, size.width, size.height) dc.SetPen(wx.BLACK_PEN) dc.SetTextForeground(wx.BLACK) for y in range(self.rows + 1): util.drawLine(dc, self.offset, self.offset + y * self.cellSize, self.cols * self.cellSize + 1, 0) for x in range(self.cols + 1): util.drawLine(dc, self.offset + x * self.cellSize, self.offset, 0, self.rows * self.cellSize) dc.SetFont(self.normalFont) for y in range(self.rows): for x in range(self.cols): i = y * self.cols + x if i < len(self.chars): util.drawText(dc, self.chars[i], x * self.cellSize + self.offset + self.cellSize // 2 + 1, y * self.cellSize + self.offset + self.cellSize // 2 + 1, util.ALIGN_CENTER, util.VALIGN_CENTER) y = self.offset + self.rows * self.cellSize pad = 5 if self.selected: self.drawCharBox(dc, "Selected:", self.selected, self.offset, y + pad, 75) c = util.upper(self.selected) if c == self.selected: c = util.lower(self.selected) if c == self.selected: c = None if c: self.drawCharBox(dc, "Opposite case:", c, self.offset + 150, y + pad, 110) dc.SetFont(self.smallFont) dc.DrawText("Character code: %d" % ord(self.selected), 360, y + pad) else: dc.SetFont(self.smallFont) dc.DrawText("Click on a character to select it.", self.offset, y + pad)
def OnPaint(self, event): dc = wx.BufferedPaintDC(self, self.screenBuf) cfgGui = self.getCfgGui() w, h = self.GetClientSizeTuple() dc.SetBrush(cfgGui.tabBarBgBrush) dc.SetPen(cfgGui.tabBarBgPen) dc.DrawRectangle(0, 0, w, h) dc.SetPen(cfgGui.tabBorderPen) dc.DrawLine(0, h - 1, w, h - 1) xpos = self.paddingX tabW = self.tabWidth tabH = h - 2 tabY = h - tabH if not self.font: textH = h - self.textY - 1 self.font = util.createPixelFont(textH, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL) self.boldFont = util.createPixelFont(textH, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD) maxTab = self.getLastVisibleTab() for i in range(self.firstTab, maxTab + 1): dc.SetFont(self.font) p = self.pages[i] dc.DestroyClippingRegion() dc.SetClippingRegion(xpos, tabY, tabW, tabH) dc.SetPen(cfgGui.tabBorderPen) if i == self.selected: points = ((6, 1), (tabW - 8, 1), (tabW - 6, 2), (tabW - 2, tabH), (0, tabH), (4, 2)) dc.SetBrush(cfgGui.workspaceBrush) else: points = ((5, 2), (tabW - 8, 2), (tabW - 6, 3), (tabW - 2, tabH - 1), (0, tabH - 1), (3, 3)) dc.SetBrush(cfgGui.tabNonActiveBgBrush) dc.DrawPolygon(points, xpos, tabY) # clip the text to fit within the tabs dc.DestroyClippingRegion() dc.SetClippingRegion(xpos, tabY, tabW - self.paddingX * 3, tabH) dc.SetPen(cfgGui.tabTextPen) dc.SetTextForeground(cfgGui.tabTextColor) dc.DrawText(p[1], xpos + self.paddingX, self.textY) dc.DestroyClippingRegion() dc.SetFont(self.boldFont) dc.DrawText(u"\u00d7", xpos + tabW - self.paddingX * 2, self.textY) xpos += tabW # start of right arrow rx = w - 1 - self.paddingX - self.arrowWidth + 1 if self.firstTab != 0: dc.DestroyClippingRegion() dc.SetPen(cfgGui.tabTextPen) util.drawLine(dc, rx - self.arrowSpacing - 1, self.arrowY, 0, self.arrowHeight) util.drawLine(dc, rx - self.arrowSpacing - 2, self.arrowY, -self.arrowWidth + 1, self.arrowHeight // 2 + 1) util.drawLine(dc, rx - self.arrowSpacing - self.arrowWidth, self.arrowY + self.arrowHeight // 2, self.arrowWidth - 1, self.arrowHeight // 2 + 1) if maxTab < (len(self.pages) - 1): dc.DestroyClippingRegion() dc.SetPen(cfgGui.tabTextPen) util.drawLine(dc, rx, self.arrowY, 0, self.arrowHeight) util.drawLine(dc, rx + 1, self.arrowY, self.arrowWidth - 1, self.arrowHeight // 2 + 1) util.drawLine(dc, rx + 1, self.arrowY + self.arrowHeight - 1, self.arrowWidth - 1, -(self.arrowHeight // 2 + 1))
def OnPaint(self, event): dc = wx.BufferedPaintDC(self, self.screenBuf) cfgGui = self.getCfgGui() w, h = self.GetClientSizeTuple() dc.SetBrush(cfgGui.tabBarBgBrush) dc.SetPen(cfgGui.tabBarBgPen) dc.DrawRectangle(0, 0, w, h) dc.SetPen(cfgGui.tabBorderPen) dc.DrawLine(0,h-1,w,h-1) xpos = self.paddingX tabW = self.tabWidth tabH = h - 2 tabY = h - tabH if not self.font: textH = h - self.textY - 1 self.font = util.createPixelFont( textH, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL) self.boldFont = util.createPixelFont( textH, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD) maxTab = self.getLastVisibleTab() for i in range(self.firstTab, maxTab + 1): dc.SetFont(self.font) p = self.pages[i] dc.DestroyClippingRegion() dc.SetClippingRegion(xpos, tabY, tabW, tabH) dc.SetPen(cfgGui.tabBorderPen) if i == self.selected: points=((6,1),(tabW-8,1),(tabW-6,2),(tabW-2,tabH),(0,tabH),(4,2)) dc.SetBrush(cfgGui.workspaceBrush) else: points=((5,2),(tabW-8,2),(tabW-6,3),(tabW-2,tabH-1),(0,tabH-1),(3,3)) dc.SetBrush(cfgGui.tabNonActiveBgBrush) dc.DrawPolygon(points,xpos,tabY) # clip the text to fit within the tabs dc.DestroyClippingRegion() dc.SetClippingRegion(xpos, tabY, tabW - self.paddingX * 3, tabH) dc.SetPen(cfgGui.tabTextPen) dc.SetTextForeground(cfgGui.tabTextColor) dc.DrawText(p[1], xpos + self.paddingX, self.textY) dc.DestroyClippingRegion() dc.SetFont(self.boldFont) dc.DrawText("×", xpos + tabW - self.paddingX * 2, self.textY) xpos += tabW # start of right arrow rx = w - 1 - self.paddingX - self.arrowWidth + 1 if self.firstTab != 0: dc.DestroyClippingRegion() dc.SetPen(cfgGui.tabTextPen) util.drawLine(dc, rx - self.arrowSpacing - 1, self.arrowY, 0, self.arrowHeight) util.drawLine(dc, rx - self.arrowSpacing - 2, self.arrowY, -self.arrowWidth + 1, self.arrowHeight // 2 + 1) util.drawLine(dc, rx - self.arrowSpacing - self.arrowWidth, self.arrowY + self.arrowHeight // 2, self.arrowWidth - 1, self.arrowHeight // 2 + 1) if maxTab < (len(self.pages) - 1): dc.DestroyClippingRegion() dc.SetPen(cfgGui.tabTextPen) util.drawLine(dc, rx, self.arrowY, 0, self.arrowHeight) util.drawLine(dc, rx + 1, self.arrowY, self.arrowWidth - 1, self.arrowHeight // 2 + 1) util.drawLine(dc, rx + 1, self.arrowY + self.arrowHeight - 1, self.arrowWidth - 1, -(self.arrowHeight // 2 + 1))
def OnPaint(self, event): dc = wx.BufferedPaintDC(self, self.screenBuf) size = self.GetClientSize() dc.SetBrush(wx.WHITE_BRUSH) dc.SetPen(wx.WHITE_PEN) dc.DrawRectangle(0, 0, size.width, size.height) dc.SetPen(wx.BLACK_PEN) dc.SetTextForeground(wx.BLACK) for y in range(self.rows + 1): util.drawLine(dc, self.offset, self.offset + y * self.cellSize, self.cols * self.cellSize + 1, 0) for x in range(self.cols + 1): util.drawLine(dc, self.offset + x * self.cellSize, self.offset, 0, self.rows * self.cellSize) dc.SetFont(self.normalFont) for y in range(self.rows): for x in range(self.cols): i = y * self.cols + x if i < len(self.chars): util.drawText( dc, self.chars[i], x * self.cellSize + self.offset + self.cellSize // 2 + 1, y * self.cellSize + self.offset + self.cellSize // 2 + 1, util.ALIGN_CENTER, util.VALIGN_CENTER) y = self.offset + self.rows * self.cellSize pad = 5 if self.selected: code = ord(self.selected) self.drawCharBox(dc, "Selected", self.selected, self.offset, y + pad, 75) c = util.upper(self.selected) if c == self.selected: c = util.lower(self.selected) if c == self.selected: c = None if c: self.drawCharBox(dc, "Opposite Case", c, self.offset + 150, y + pad, 110) dc.SetFont(self.smallFont) dc.DrawText("Character Code: %d" % code, 360, y + pad) if code == 32: dc.DrawText("Normal Space", 360, y + pad + 30) elif code == 160: dc.DrawText("Non-breaking Space", 360, y + pad + 30) else: dc.SetFont(self.smallFont) dc.DrawText("Select a character", self.offset, y + pad)
plt.xlim([0.5, 6.5]) plt.ylim([2, 8]) wait = input('Program paused. Press enter to continue.\n') # %% =============== Part 2: Principal Component Analysis =============== print('Running PCA on example dataset.') # Before running PCA, it is important to first normalize X X_norm, mu, sigma = util.featureNormalize(X) # Run PCA [U, S] = pca(X_norm) util.drawLine(mu, mu + 1.5 * S[0] * U[:, 0], '--k') util.drawLine(mu, mu + 1.5 * S[1] * U[:, 1], '--k') plt.show() wait = input('Program paused. Press enter to continue.\n') # %% =================== Part 3: Dimension Reduction =================== print('Dimension reduction on example dataset.') # Plot the normalized dataset (returned from pca) plt.figure(figsize=([4, 4])) plt.scatter(X_norm[:, 0], X_norm[:, 1], alpha=0.5) plt.xlim([-4, 3]) plt.ylim([-4, 3])