<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://entorb.net//wiki/index.php?action=history&amp;feed=atom&amp;title=Python_-_Math</id>
	<title>Python - Math - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://entorb.net//wiki/index.php?action=history&amp;feed=atom&amp;title=Python_-_Math"/>
	<link rel="alternate" type="text/html" href="https://entorb.net//wiki/index.php?title=Python_-_Math&amp;action=history"/>
	<updated>2026-05-06T11:25:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://entorb.net//wiki/index.php?title=Python_-_Math&amp;diff=4842&amp;oldid=prev</id>
		<title>Torben at 20:21, 30 October 2024</title>
		<link rel="alternate" type="text/html" href="https://entorb.net//wiki/index.php?title=Python_-_Math&amp;diff=4842&amp;oldid=prev"/>
		<updated>2024-10-30T20:21:21Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Coding]][[Category:Python]]&lt;br /&gt;
===Average of List===&lt;br /&gt;
 def myMean(L):&lt;br /&gt;
   return float(sum(L)) / len(L)&lt;br /&gt;
&lt;br /&gt;
===Lin Reg===&lt;br /&gt;
 from math import sqrt&lt;br /&gt;
 def myLinReg(X, Y):&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
         Linear regression of y = ax + b&lt;br /&gt;
         real a, real b = linreg(X, Y)&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     if len(X) != len(Y):  raise ValueError, &amp;#039;unequal length&amp;#039;&lt;br /&gt;
     N = len(X)&lt;br /&gt;
     if   N == 0:&lt;br /&gt;
       raise ValueError, &amp;#039;list too short&amp;#039;&lt;br /&gt;
       a = b = 0.0&lt;br /&gt;
     elif N == 1:&lt;br /&gt;
       a = 0&lt;br /&gt;
       b = Y[0]&lt;br /&gt;
     elif N == 2:&lt;br /&gt;
       a = float(X[0]-X[1])/(Y[0]-Y[1])&lt;br /&gt;
       b = Y[0]-a*X[0]&lt;br /&gt;
     else :&lt;br /&gt;
       Sx = Sy = Sxx = Syy = Sxy = 0.0 # sums&lt;br /&gt;
       for x, y in map(None, X, Y):&lt;br /&gt;
           Sx = Sx + x&lt;br /&gt;
           Sy = Sy + y&lt;br /&gt;
           Sxx = Sxx + x*x&lt;br /&gt;
           Syy = Syy + y*y&lt;br /&gt;
           Sxy = Sxy + x*y&lt;br /&gt;
       det = Sxx * N - Sx * Sx&lt;br /&gt;
       if det == 0.0 : det = 1e-5 # workaround&lt;br /&gt;
       a, b = (Sxy * N - Sy * Sx)/det, (Sxx * Sy - Sx * Sxy)/det&lt;br /&gt;
       #meanerror = residual = 0.0&lt;br /&gt;
       #for x, y in map(None, X, Y):&lt;br /&gt;
       #    meanerror = meanerror + (y - Sy/N)**2&lt;br /&gt;
       #    residual = residual + (y - a * x - b)**2&lt;br /&gt;
       #if meanerror == 0.0 : meanerror = 1e-5 # workaround&lt;br /&gt;
       #RR = 1 - residual/meanerror&lt;br /&gt;
       #ss = residual / (N-2)&lt;br /&gt;
       #Var_a, Var_b = ss * N / det, ss * Sxx / det&lt;br /&gt;
     return a, b&lt;br /&gt;
&lt;br /&gt;
===Curve Fitting===&lt;br /&gt;
from [https://www.geeksforgeeks.org/scipy-curve-fitting/]&lt;br /&gt;
 import numpy as np&lt;br /&gt;
 &lt;br /&gt;
 # curve-fit() function imported from scipy&lt;br /&gt;
 from scipy.optimize import curve_fit&lt;br /&gt;
 from matplotlib import pyplot as plt&lt;br /&gt;
 &lt;br /&gt;
 # Test function with coefficients as parameters&lt;br /&gt;
 def fit_function(x, a, b):&lt;br /&gt;
     return a * np.exp(b * x)&lt;br /&gt;
 &lt;br /&gt;
 p0 = [data_y[-1], 0.14]  # initial guess of parameters&lt;br /&gt;
 param, param_cov = curve_fit(&lt;br /&gt;
     fit_function, data_x, data_y, p0, bounds=((-np.inf, -np.inf), (np.inf, np.inf))&lt;br /&gt;
 )&lt;br /&gt;
 &lt;br /&gt;
 print(f&amp;quot;Coefficients:\n{param}&amp;quot;)&lt;br /&gt;
 print(f&amp;quot;Covariance of coefficients:\n{param_cov}&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 data_y_fit = []&lt;br /&gt;
 for x in data_x:&lt;br /&gt;
     y = fit_function(x, param[0], param[1])&lt;br /&gt;
     data_y_fit.append(y)&lt;/div&gt;</summary>
		<author><name>Torben</name></author>
	</entry>
</feed>