From 90d638cfae09b584486fb1373ef509bb0bf9e2a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 9 Jan 2019 14:19:33 +0100 Subject: [PATCH] added some stuff --- ex_29.py | 19 +++++++++++++++++++ ex_30.py | 21 +++++++++++++++++++++ ex_31.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ex_32.py | 23 +++++++++++++++++++++++ ex_33.py | 40 +++++++++++++++++++++++++++++++++++++++ ex_34.py | 42 +++++++++++++++++++++++++++++++++++++++++ ex_35.py | 21 +++++++++++++++++++++ ex_36.py | 4 ++++ example.db | Bin 0 -> 8192 bytes 9 files changed, 224 insertions(+) create mode 100644 ex_29.py create mode 100644 ex_30.py create mode 100644 ex_31.py create mode 100644 ex_32.py create mode 100644 ex_33.py create mode 100644 ex_34.py create mode 100644 ex_35.py create mode 100644 ex_36.py create mode 100644 example.db diff --git a/ex_29.py b/ex_29.py new file mode 100644 index 0000000..b837ba2 --- /dev/null +++ b/ex_29.py @@ -0,0 +1,19 @@ +from math import exp +from random import uniform +import matplotlib.pyplot as plt + +def f(n, f0=100): + result = f0 + for i in range(n): + result *= exp(0.2 + uniform(-0.04, 0.04)) + return result + +X = list(range(350)) +y1 = [f(x) for x in X] +y2 = [100*exp(0.2*x) for x in X] + +plt.plot(X, y1, X, y2) +plt.show() + + + diff --git a/ex_30.py b/ex_30.py new file mode 100644 index 0000000..586ecf7 --- /dev/null +++ b/ex_30.py @@ -0,0 +1,21 @@ +from random import uniform +import matplotlib.pyplot as plt +import numpy as np +import scipy.optimize + +def f(n, f0=100): + result = f0 + for i in range(n): + result *= np.exp(0.2 + uniform(-0.04, 0.04)) + return result + +X = np.arange(0, 100, 1) +Y = np.array([f(int(x)) for x in X]) + +fitf = lambda x, k, a: k*np.exp(x*a) +(k, a), pcov = scipy.optimize.curve_fit(fitf, X, Y, p0=[100, 0.2]) + +fitted = lambda x: k*np.exp(x*a) + +plt.plot(X, Y, X, fitted(X)) +plt.show() diff --git a/ex_31.py b/ex_31.py new file mode 100644 index 0000000..9f909f6 --- /dev/null +++ b/ex_31.py @@ -0,0 +1,54 @@ +import scipy.optimize +import numpy as np +from defusedxml import ElementTree +from collections import deque +import matplotlib.pyplot as plt +from scipy.optimize import curve_fit + +fit_f1 = lambda x, K, alpha: K * np.exp(alpha * x) +fit_f2 = lambda x, a, b: a*x + b + + + +data = {"Germany": deque() + , "France": deque() + , "Italy": deque() +# , "United States": deque() +# , "Angola": deque() +# , "China": deque() +} + +with open("data/API_NY.GDP.MKTP.KN_DS2_en_xml_v2_10230884.xml") as fin: + tree = ElementTree.parse(fin) + for record in tree.getroot().find("data").findall("record"): + this_data = {field.get("name"): field.text for field in record.findall("field")} + if(this_data["Country or Area"] in data): + if(this_data["Value"] != None): + data[this_data["Country or Area"]].append((this_data["Year"], this_data["Value"])) + + + +class Data(object): + def __init__(self, raw_data): + self.x = np.array([int(k) for k, v in raw_data]) + self.y = np.array([float(v) for k, v in raw_data]) + +plots = deque() +for country, values in data.items(): + values = Data(values) + + popt1, pcov = curve_fit(fit_f1, values.x, values.y + , p0=[values.y[0], 1]) + popt2, pcov = curve_fit(fit_f2, values.x, values.y + , p0=[values.y[0], (values.y[-1] - values.y[0])/(values.x[-1] - values.x[0])]) + + f1 = lambda x: fit_f1(x, popt1[0], popt1[1]) + f2 = lambda x: fit_f2(x, popt2[0], popt2[1]) + + p1, = plt.plot(values.x, values.y, label="{}: real".format(country)) + p2, = plt.plot(values.x, f1(values.x), label="%s: exponential fit, K=%.3e, $\\alpha$=%.3e" % (country, *popt1)) + p3, = plt.plot(values.x, f2(values.x), label="%s: linear fit, a=%.3e, b=%.3e" % (country, *popt2)) + plots.extend([p1, p2, p3]) + +plt.legend(handles=list(plots)) +plt.show() diff --git a/ex_32.py b/ex_32.py new file mode 100644 index 0000000..100acda --- /dev/null +++ b/ex_32.py @@ -0,0 +1,23 @@ +import time + +def bad_timeit(f): + def timeitwrapper(*args, **kwargs): + start = time.time() + result = f(*args, **kwargs) + stop = time.time() + elapsed = stop - start + print("the function", f.__name__, "has been executed in", elapsed, "s.") + return result + return timeitwrapper + +def _recursive_fibonacci(n): + if n in (0, 1): + return 1 + return _recursive_fibonacci(n - 1) + _recursive_fibonacci(n - 2) + +@bad_timeit +def recursive_fibonacci(n): + return _recursive_fibonacci(n) + +if( __name__ == "__main__"): + recursive_fibonacci(43) diff --git a/ex_33.py b/ex_33.py new file mode 100644 index 0000000..13bd08e --- /dev/null +++ b/ex_33.py @@ -0,0 +1,40 @@ +import numpy as np +import matplotlib.pyplot as plt + +class BrownIterator(object): + def __init__(self, N, m): + self._N = N + self._max_m = m + self._i = 0 + self._xs = None + self._ys = None + def __iter__(self): + self._xs = np.zeros(self._N) + self._ys = np.zeros(self._N) + self._i = 0 + return self + def __next__(self): + self._i += 1 + if(self._i > self._max_m): + raise StopIteration() + if(self._i == 1): + return self._xs, self._ys + + theta = np.random.uniform(0, np.pi * 2, self._N) + + self._xs = self._xs + np.cos(theta) + self._ys = self._ys + np.sin(theta) + return self._xs, self._ys + +if( __name__ == "__main__"): + data = np.array([i for i in BrownIterator(1000, 321)]) + print(data) + + p1, = plt.plot(data[20,0], data[20,1], "ro", label="t = 20") + p2, = plt.plot(data[80,0], data[80,1], "bo", label="t = 80") + p3, = plt.plot(data[320,0], data[320,1], "go", label="t = 320") + + plt.legend(handles=[p1, p2, p3]) + plt.show() + + diff --git a/ex_34.py b/ex_34.py new file mode 100644 index 0000000..fc0a4ff --- /dev/null +++ b/ex_34.py @@ -0,0 +1,42 @@ +import numpy as np +import random + +class RankPageCalculator(object): + def __init__(self, connections): + self._connections = connections + self._dim = len(connections) + self._hits = np.arange(0, self._dim, 1) + self._current = random.randint(0, self._dim - 1) + + def run(self, n, alpha): + for i in range(n): + if(np.random.uniform(0, 1) < alpha): + self._current = random.randint(0, self._dim - 1) + self._hits[self._current] += 1 + continue + uniform = np.random.uniform(0, 1, self._dim) + uniform[1 != self._connections[self._current]] = 0 + self._current = uniform.argmax() + self._hits[self._current] += 1 + + return self._hits + + +adj = np.array([[0, 1, 1, 0, 0, 0, 0, 0, 0] + , [0, 0, 1, 0, 1, 0, 0, 0, 0] + , [1, 1, 0, 1, 0, 0, 0, 0, 0] + , [0, 0, 1, 0, 1, 0, 0, 0, 0] + , [0, 1, 0, 1, 0, 0, 0, 0, 0] + , [0, 0, 0, 0, 1, 0, 1, 0, 1] + , [0, 0, 0, 0, 0, 1, 0, 0, 0] + , [0, 0, 0, 0, 0, 0, 1, 0, 0] + , [0, 0, 0, 0, 0, 1, 1, 1, 0]], dtype=np.int16) + + +print(RankPageCalculator(adj).run(100000, 0.05)) +print(RankPageCalculator(adj).run(100000, 0.3)) + +adj[4, 5] = 1 +adj[5, 4] = 1 +print(RankPageCalculator(adj).run(100000, 0.05)) +print(RankPageCalculator(adj).run(100000, 0.3)) diff --git a/ex_35.py b/ex_35.py new file mode 100644 index 0000000..5e7990c --- /dev/null +++ b/ex_35.py @@ -0,0 +1,21 @@ +import time + +class counter_of_calls(object): + def __init__(self, f): + self._f = f + self._cnt = 0 + + def __call__(self, *args, **kwargs): + self._cnt += 1 + result = self._f(*args, **kwargs) + print(self._f.__name__, "has been called", self._cnt, "times; last time:", time.time()) + return result + + +@counter_of_calls +def f(x): + return x + 6 + +if( __name__ == "__main__"): + print(f(3)) + print(f(5)) diff --git a/ex_36.py b/ex_36.py new file mode 100644 index 0000000..28c1f65 --- /dev/null +++ b/ex_36.py @@ -0,0 +1,4 @@ + +def check_range(f): + def wrapper(**kwargs): + for k,v in diff --git a/example.db b/example.db new file mode 100644 index 0000000000000000000000000000000000000000..06d0f5b4ebc74ad2ed87525d4e6074cda9a7b967 GIT binary patch literal 8192 zcmeI#ze~eF6bJCTU~!VVWfE>v!Gwmym~2fYgIEe`1UI>QFK{$XB)K5C`ET^!=s#!@ zvvhLo@_pRBcfa7^v%Qz6r8Xq;s@_T?e0I(_XLBJKW7askG6e7P$++%IzyB{R=1vx8 z-77xbv2GwB009U<00Izz00bZa0SG_<0uVS5Q2f-kZT@p<