Latest recipes tagged "regression"http://code.activestate.com/recipes/tags/regression/new/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>
Simple linear regression (Python)
2012-05-12T13:36:55-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578129-simple-linear-regression/
<p style="color: grey">
Python
recipe 578129
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/decrease/">decrease</a>, <a href="/recipes/tags/increase/">increase</a>, <a href="/recipes/tags/linear/">linear</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/percental/">percental</a>, <a href="/recipes/tags/regression/">regression</a>, <a href="/recipes/tags/simple/">simple</a>).
Revision 3.
</p>
<p><strong>What?</strong></p>
<ul>
<li>It's about forecasting.</li>
<li>It's about calculating a linear function.</li>
<li><em>Details I found</em>: <a href="http://www.faes.de/Basis/Basis-Statistik/Basis-Statistik-Korrelation-Re/basis-statistik-korrelation-re.html" rel="nofollow">http://www.faes.de/Basis/Basis-Statistik/Basis-Statistik-Korrelation-Re/basis-statistik-korrelation-re.html</a> (in german)</li>
</ul>
<p>Also I don't know how the formula have been created the practical part was very easy to me. I have verified one example (see code) using Open Office Calc (I've learned: you can display the formula for the trend line as well as the coefficient of correlation - great).</p>
<p><strong>Why?</strong></p>
<ul>
<li>In <a href="http://code.activestate.com/recipes/578111/">recipe 578111</a> I'm printing out current error rate for different training sessions in mental arithmetic.</li>
<li>Anyway I would like to be able to given information - approximately - about how many you have improved since you train yourself.</li>
</ul>
<p><strong>What has changed?</strong></p>
<ul>
<li><strong>Revision2</strong>: Didn't compile with Jython 2.5.3b1 because of not supported exception syntax. Now it does work without exception.</li>
<li><strong>Revision3</strong>: Test data row for failure not removed.</li>
</ul>
Locate and import Python's standard regression tests (Python)
2010-09-17T11:31:31-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577394-locate-and-import-pythons-standard-regression-test/
<p style="color: grey">
Python
recipe 577394
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/regression/">regression</a>, <a href="/recipes/tags/testing/">testing</a>).
</p>
<p>The Python standard library comes with an extensive set of regression tests. I had need to import and run some of these tests from my own code, but the test directory is not in Python's path. This simple helper function solves the problem.</p>