Popular recipes by Chaobin Tang (唐超斌) http://code.activestate.com/recipes/users/4174076/2014-07-31T15:55:15-07:00ActiveState Code RecipesSimple Linear Regression with Pure Python (Python) 2014-07-31T15:55:15-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578914-simple-linear-regression-with-pure-python/ <p style="color: grey"> Python recipe 578914 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/machine_learning/">machine_learning</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/regression/">regression</a>). </p> <p>Linear regression is a very useful and simple to understand way for predicting values, given a set of training data. The outcome of the regression is a best fitting line function, which, by definition, is the line that minimizes the sum of the squared errors (When plotted on a 2 dimensional coordination system, the errors are the distance between the actual Y' and predicted Y' on the line.) In machine learning, this line equation Y' = b*x + A is solved using Gradient Descent to gradually approach to it. Also, there is a statistical approach that directly solves this line equation without using an iterative algorithm.</p> <p>This recipe is a pure Python implementation of this statistical algorithm. It has no dependencies.</p> <p>If you have pandas and numpy, you can test its result by uncommenting the assert lines.</p> Traverse dotted attribute of an object using built-in function reduce (Python) 2013-01-05T05:49:27-08:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578398-traverse-dotted-attribute-of-an-object-using-built/ <p style="color: grey"> Python recipe 578398 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bif/">bif</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/reduce/">reduce</a>). </p> <p>Making good use of reduce() to traverse dotted attribute of an object.</p> Pipeline made of coroutines (Python) 2012-09-14T09:53:58-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578265-pipeline-made-of-coroutines/ <p style="color: grey"> Python recipe 578265 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>A pipeline made of several coroutines that can be turned off gracefully. </p> A Python chat program using process intercommunication (Python) 2012-09-12T10:02:21-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578260-a-python-chat-program-using-process-intercommunica/ <p style="color: grey"> Python recipe 578260 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/multiprocessing/">multiprocessing</a>). </p> <p>This demo chat program, at some aspect, mimic the way that Erlang makes a distributable application. Because it utilizes process, the CPU cores are used as much as possible.</p> A metaclass that hides details of the attribute name when called. (Python) 2012-08-31T09:53:59-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578249-a-metaclass-that-hides-details-of-the-attribute-na/ <p style="color: grey"> Python recipe 578249 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>In cases, where getting an attribute will expose to the client code the detail of the very attribute name, which could be changeable over time. This NamedAndCachedAttributeType is intended to hide this detail by mapping the real attribute name to another name that is maintained by the class itself. By doing so, the content provider(this class) and the client code(the caller) establish a deal that the changes of names are taken care of by the provider itself. Second, the value is set as a class variable once it is first retreived, as being cached.</p> A Python decorator that re-executes the function on condition (Python) 2012-07-24T09:51:48-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578222-a-python-decorator-that-re-executes-the-function-o/ <p style="color: grey"> Python recipe 578222 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/retry/">retry</a>). Revision 3. </p> <p>Sometimes we want a function to be able to be retried automatically, such as a function that does networking trying to write/read data through a pre-established connection. Instead of writing try/except everywhere, a decorator would save much code and provide a single copy of code to do all the work.</p> Object id mask, against data spiders (Python) 2010-08-16T07:18:43-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/577359-object-id-mask-against-data-spiders/ <p style="color: grey"> Python recipe 577359 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/url/">url</a>). Revision 4. </p> <p>A funny, simple, and efficient encrypt for an object ID. Always used against an unsolicited automated spider that scrawls your site to collect data.</p>