added some stuff
This commit is contained in:
parent
591a474795
commit
90d638cfae
19
ex_29.py
Normal file
19
ex_29.py
Normal file
|
@ -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()
|
||||||
|
|
||||||
|
|
||||||
|
|
21
ex_30.py
Normal file
21
ex_30.py
Normal file
|
@ -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()
|
54
ex_31.py
Normal file
54
ex_31.py
Normal file
|
@ -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()
|
23
ex_32.py
Normal file
23
ex_32.py
Normal file
|
@ -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)
|
40
ex_33.py
Normal file
40
ex_33.py
Normal file
|
@ -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()
|
||||||
|
|
||||||
|
|
42
ex_34.py
Normal file
42
ex_34.py
Normal file
|
@ -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))
|
21
ex_35.py
Normal file
21
ex_35.py
Normal file
|
@ -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))
|
4
ex_36.py
Normal file
4
ex_36.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
def check_range(f):
|
||||||
|
def wrapper(**kwargs):
|
||||||
|
for k,v in
|
BIN
example.db
Normal file
BIN
example.db
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user