 {"id":152,"date":"2019-08-05T22:41:33","date_gmt":"2019-08-06T05:41:33","guid":{"rendered":"https:\/\/joshuarenglish.com\/blog\/?p=152"},"modified":"2019-08-05T22:41:38","modified_gmt":"2019-08-06T05:41:38","slug":"uncle-josh-cracks-his-knuckles-on-ml-pt-1","status":"publish","type":"post","link":"https:\/\/joshuarenglish.com\/blog\/2019\/08\/05\/uncle-josh-cracks-his-knuckles-on-ml-pt-1\/","title":{"rendered":"Uncle Josh Cracks His Knuckles on ML, Pt. 1"},"content":{"rendered":"\n<p>I&#8217;ve had an on-off relationship with Machine Learning and Neural Nets. They fascinate me but they also look incredibly complicated and I&#8217;m busy and scatterbrained and can&#8217;t remember most of the calculus and linear algebra I learned so I went ahead and got started on a ML project that is a direct ripoff of Jabril&#8217;s  <a href=\"https:\/\/www.youtube.com\/playlist?list=PL0nQ4vmdWaA0mzW4zPffYnaRzzO7ZqDZ0\">Making My First Machine Learning Game<\/a> .<\/p>\n\n\n\n<p>At least, it&#8217;s not a blatant rip-off because he didn&#8217;t show code examples in his videos and he codes in Unity which I don&#8217;t know at all, so I&#8217;m sticking to what I know: Python, and what I used to know: wxPython. <\/p>\n\n\n\n<p style=\"text-align:right\"><em>Side note: Check out the rest of his channel. His stuff is good.<\/em><\/p>\n\n\n\n<p>I spent too much time trying to get my home installation of Vim to behave like my work installation, which is fine-tuned and gets more attention than my home installation. Then I spent some time re-learning wxPython and trying to solve one of the problems that bugged me for years: Graphic coordinates in most computer systems is not &#8220;logical&#8221; to me. The most common native screen coordinate system is the origin in the upper-left corner, positive x to the right, positive y down.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"338\" src=\"https:\/\/joshuarenglish.com\/blog\/wp-content\/uploads\/2019\/08\/DefaultScreenCoords.png\" alt=\"Default screen coordinates with the origin in the upper-left hand corner\" class=\"wp-image-157\" srcset=\"https:\/\/joshuarenglish.com\/blog\/wp-content\/uploads\/2019\/08\/DefaultScreenCoords.png 600w, https:\/\/joshuarenglish.com\/blog\/wp-content\/uploads\/2019\/08\/DefaultScreenCoords-300x169.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Default Screen Coordinates that Suck<\/figcaption><\/figure><\/div>\n\n\n\n<p>But this isn&#8217;t what I want most of the time. I want the origin either in the lower-left corner or dead center. In this case I wanted the origin to be dead center of the window.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"338\" src=\"https:\/\/joshuarenglish.com\/blog\/wp-content\/uploads\/2019\/08\/LogicalScreenCoords-1.png\" alt=\"Screen Coordinates with the origin in the center\" class=\"wp-image-160\" srcset=\"https:\/\/joshuarenglish.com\/blog\/wp-content\/uploads\/2019\/08\/LogicalScreenCoords-1.png 600w, https:\/\/joshuarenglish.com\/blog\/wp-content\/uploads\/2019\/08\/LogicalScreenCoords-1-300x169.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Graphing-friendly coordinate system<\/figcaption><\/figure>\n\n\n\n<p>I know I could take every single point I have to deal with&#8211;the end points of every line segment, the triangle I&#8217;m going to use to draw my Forrest Gump rip-off, the lines illustrating what Forrest &#8220;knows&#8221;&#8211;and use some custom formula to transform those points to a system that when drawn by the program look right.<\/p>\n\n\n\n<p>I spent a lot of time dealing with a <a href=\"https:\/\/twitter.com\/raymondh\">Raymond Hettinger <\/a>mantra: There has to be a better way. And there is. It&#8217;s been in wxPython for a while, apparently, and I either never found it or never quite understood how to use it. The magic is in the <a href=\"https:\/\/wxpython.org\/Phoenix\/docs\/html\/wx.DC.html#wx.DC.SetTransformMatrix\"><code>DC.SetTransformMatrix<\/code><\/a> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MainFrame(wx.Frame):\n     def init(self, parent):\n         wx.Frame.init(self, parent, size=(600, 600))\n         self.Center()\n         self.Bind(wx.EVT_PAINT, self.OnPaint)\n         self.SetTitle(\"Runner\")\n\n    def OnPaint(self, event):\n        self.DrawEverything()\n\n    def DrawEverything(self):\n        dc = wx.MemoryDC()\n        dc.Clear()\n        size_x, size_y = self.GetClientSize()\n        dc.SelectObject(wx.Bitmap(size_x, size_y, 32))\n        matrix = wx.AffineMatrix2D()\n        matrix.Translate(size_x\/2, size_y\/2)\n        matrix.Mirror(wx.VERTICAL)\n        dc.SetTransformMatrix(matrix)\n\n        # Draw Stuff to dc here\n\n        win = wx.WindowDC(self)\n        win.Blit(0, 0, size_x, size_y, dc, 0, 0)<\/code><\/pre>\n\n\n\n<p>And lo and behold, when I tell the program to draw a circle at (0,0) with a radius of 50, I get a circle in the middle of the screen. If I draw a rectangle from (0, 0) to to (20, 10), I get a rectangle in the upper-right quadrant of the screen just like I wanted.  <\/p>\n\n\n\n<p>There is some odd behavior.  I used another matrix to transform the triangle I am using for the runner to rotate it and move it. The particular lines of code feel like they are backwards, but they work.<\/p>\n\n\n<pre><code>rm = wx.AffineMatrix2D()\nrm.Translate(self.runner.x, self.runner.y)\nrm.Rotate(self.runner.d)\npoints = [(-3, -5), (15, 0), (-3, 5)]<br \/>points = [rm.TransformPoint(a, b) for (a, b) in points]<br \/>dc.SetPen(wx.BLUE_PEN)\ndc.DrawPolygon(points)\ndc.DrawLine(points[0], points[1])<\/code><\/pre>\n\n\n<p>I know I should probably draw this sprite in it&#8217;s own DC and blit it appropriately, but this was a quick and dirty to get something on the screen solution.<\/p>\n\n\n\n<p>Next, how I tried to relearn linear algebra in an afternoon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had an on-off relationship with Machine Learning and Neural Nets. They fascinate me but they also look incredibly complicated and I&#8217;m busy and scatterbrained and can&#8217;t remember most of the calculus and linear algebra I learned so I went ahead and got started on a ML project that is a direct ripoff of Jabril&#8217;s &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","_share_on_mastodon":"0"},"categories":[34],"tags":[91,92,94,93],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-boxes-that-go-bing","tag-machine-learning","tag-python","tag-wx-affinematrix2d","tag-wxpython"],"share_on_mastodon":{"url":"","error":""},"_links":{"self":[{"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":5,"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joshuarenglish.com\/blog\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}