%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
x1 = [1,2,3,4,5]
x = [x1]*4
np.array(x)
array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
y = np.random.random([4,5])
y
array([[ 0.14893078, 0.83500504, 0.33254458, 0.34919925, 0.85896067], [ 0.06913916, 0.32680878, 0.98187853, 0.4075068 , 0.12691891], [ 0.46446216, 0.07313599, 0.47927263, 0.30568271, 0.58156629], [ 0.2036421 , 0.38874274, 0.77309067, 0.40568841, 0.22006675]])
plot = [plt.plot]*4
for x0, y0 in zip(x,y):
plot[]
[[<matplotlib.lines.Line2D at 0x7f8c027c9ed0>], [<matplotlib.lines.Line2D at 0x7f8c027c9390>], [<matplotlib.lines.Line2D at 0x7f8c022d6d50>], [<matplotlib.lines.Line2D at 0x7f8c02281b50>]]
S = 30
a = 2
list(np.random.rand(S, a))
[array([ 0.70872968, 0.01092441]), array([ 0.8474592 , 0.00799274]), array([ 0.32509336, 0.37037824]), array([ 0.63809043, 0.56999524]), array([ 0.56902032, 0.93019681]), array([ 0.42995497, 0.8680469 ]), array([ 0.7896895 , 0.79567632]), array([ 0.69650006, 0.0027546 ]), array([ 0.07089027, 0.59454975]), array([ 0.00599476, 0.61580831]), array([ 0.38648582, 0.13004182]), array([ 0.55736395, 0.13058307]), array([ 0.3091056, 0.3718521]), array([ 0.99149774, 0.05002822]), array([ 0.82439725, 0.74503987]), array([ 0.8147719 , 0.31425015]), array([ 0.69684755, 0.21645931]), array([ 0.18054141, 0.46709824]), array([ 0.35552414, 0.04843078]), array([ 0.23009066, 0.36481627]), array([ 0.14554052, 0.57552492]), array([ 0.76747337, 0.14061652]), array([ 0.3047507 , 0.64406451]), array([ 0.70305869, 0.08239102]), array([ 0.5945298 , 0.54540763]), array([ 0.74443342, 0.56341598]), array([ 0.88227626, 0.12969562]), array([ 0.82276061, 0.31183299]), array([ 0.35299081, 0.64966661]), array([ 0.94986125, 0.71439276])]
import multiprocessing as mp
mp.cpu_count()
4
def myplot1(x, y, xfit=np.array([]), yfit=np.array([]), param=None,
scale=['linear', 'linear', 'log', 'log']):
"""my plot function
x: {'label_x', xdata}
y: {'label_y', ydata}
param: {'a': 10, 'b': 20}
"""
if param:
s = [r'$%s = %f$' % (k, v) for k, v in param.items()]
label = s[0]
for _s in s[1:]:
label += ", " + _s
label_x, xdata = x.items()[0]
label_y, ydata = y.items()[0]
fig = plt.figure(figsize=(8, 12))
ax1 = fig.add_subplot(211)
ax1.plot(xdata, ydata)
if len(xfit):
ax1.plot(xfit, yfit, label=label)
ax1.legend(loc='best')
ax1.set_xlabel(label_x)
ax1.set_ylabel(label_y)
ax1.set_xscale(scale[0])
ax1.set_yscale(scale[1])
ax2 = fig.add_subplot(212)
ax2.plot(xdata, ydata)
if len(xfit):
ax2.plot(xfit, yfit, label=label)
ax2.legend(loc='best')
ax2.set_xlabel(label_x)
ax2.set_ylabel(label_y)
ax2.set_xscale(scale[2])
ax2.set_yscale(scale[3])
plt.show()
def myfit(fit_func, parameter, x, y, xmin, xmax):
"""my fitting and plotting function.
fit_func: function (parameter(type:list), x)
parameter: list of tuples: [('param1', param1), ('param2', param2), ...]
x, y: dict
xmin, xmax: float
"""
xkey, xdata = x.items()[0]
ykey, ydata = y.items()[0]
def fit(parameter, x, y):
return y - fit_func(parameter, x)
# use x : xmin < x < xmax
i = 0
while xdata[i] < xmin:
i += 1
imin, imax = i, i
while xdata[i] < xmax:
i += 1
imax = i - 1
paramdata = [b for a, b in parameter]
paramkey = [a for a, b in parameter]
res = leastsq(fit, paramdata, args=(xdata[imin:imax], ydata[imin:imax]))
for i, p in enumerate(res[0]):
print parameter[i][0] + ": " + str(p)
fitted = fit_func(res[0], xdata[imin:imax])
fittedparam = dict([(k, v) for k, v in zip(paramkey, res[0])])
myplot1(x, y, xdata[imin:imax], fitted, param=fittedparam)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f787ea3e9d0>]
x = np.linspace(0.00001, 0.5, 100)
y = x**4/2. - (8./3.)*x**3 + np.pi*x**2
param = [('a', 3.), ('b', 2.)]
xmin, xmax = 0., 0.5
x = {r'$x$': x}
y = {r'$y$': y}
def fit_func(param, x):
a = param[0]
b = param[1]
return a*np.power(x, b)
myfit(fit_func, param, x, y, xmin, xmax)
a: 1.57195618868 b: 1.66602214973
1./1.66602214973
0.6002321158587614
1/0.75
1.3333333333333333
import numpy as np
trial = 10
for t in xrange(trial):
print t
i = 1
K = 10
X = int(np.random.rand()*K*1.5)
while i < K:
if i == X:
print "!:", str(i)
break
i += 1
else:
continue
0 1 !: 2 2 !: 6 3 4 5 6 !: 3 7 !: 9 8 !: 8 9 !: 7
int(np.random.rand()*K)
4