Trend lines¶
In [1]:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import r2_score
import sympy as sp
from scipy.optimize import curve_fit
import pandas as pd
Data¶
In [2]:
x = np.linspace(1, 5, 1000)
y = (np.random.random(1000)-.5)*.5 + x
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
z = np.polyfit(x, y, 5)
y_hat = np.poly1d(z)(x)
ax.plot(x, y_hat, "r--", lw=2)
Out[2]:
In [3]:
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
z = np.polyfit(x, y, 1)
y_hat = np.poly1d(z)(x)
ax.plot(x, y_hat, "r--", lw=2)
print(np.poly1d(z))
xs = sp.symbols("x")
poly = sum(sp.S("{:6.10f}".format(v))*xs**i for i, v in enumerate(z[::-1]))
display(poly)
In [4]:
r2_score(y, y_hat)
Out[4]:
higher order¶
In [5]:
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
z = np.polyfit(x, y, 5)
y_hat = np.poly1d(z)(x)
ax.plot(x, y_hat, "r--", lw=2)
print(np.poly1d(z))
xs = sp.symbols("x")
poly = sum(sp.S("{:6.10f}".format(v))*xs**i for i, v in enumerate(z[::-1]))
display(poly)
In [6]:
r2_score(y,y_hat)
Out[6]:
In [7]:
def func(x, a, b, c):
return a * x**5 + b * x**3 + c*x
In [8]:
popt, pcov = curve_fit(func, x, y, ftol=1e-15, xtol=1e-15, gtol=1e-16, bounds=([-np.inf, -np.inf, 0], np.inf))
popt, pcov
Out[8]:
In [9]:
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
ax.plot(x, func(x, *popt), "r--", lw=2)
Out[9]:
In [10]:
np.min(np.diff(y) / np.diff(x)), np.min(np.diff(func(x, *popt)) / np.diff(x))
Out[10]:
In [11]:
r2_score(y,func(x, *popt))
Out[11]:
logarithm¶
In [12]:
#x = np.linspace(1e-5, 2, 1000)
#y = 3 * np.log(x)
In [13]:
def func_log(x, a, b):
return a * np.log(x) + b
In [14]:
popt, pcov = curve_fit(func_log, x, y, ftol=1e-15, xtol=1e-15, gtol=1e-15, )
#p0=[1,1,1], bounds=([-np.inf, 0, -np.inf], np.inf))
popt, pcov
Out[14]:
In [15]:
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
ax.plot(x, func_log(x, *popt), "r--", lw=2)
Out[15]:
In [16]:
r2_score(y,func_log(x, *popt))
Out[16]:
exp¶
In [17]:
#x = np.linspace(1e-5, 2, 1000)
#y = 3 * np.exp(x)
In [18]:
def func_exp(x, a, b):
return a * np.exp(b*x)
In [19]:
popt, pcov = curve_fit(func_exp, x, y, maxfev=1000, ftol=1e-15, xtol=1e-15, gtol=1e-15)
popt, pcov
Out[19]:
In [20]:
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
ax.plot(x, func_exp(x, *popt), "r--", lw=2)
Out[20]:
In [21]:
r2_score(y,func_exp(x, *popt))
Out[21]:
power¶
In [22]:
#x = np.linspace(1e-5, 2, 1000)
#y = x**5
In [23]:
def func_pow(x, a, b):
return a * x**b
In [24]:
popt, pcov = curve_fit(func_pow, x, y, ftol=1e-15, xtol=1e-15, gtol=1e-16)
popt, pcov
Out[24]:
In [25]:
fig, ax = plt.subplots()
ax.plot(x,y,"+", ms=3, mec="k")
ax.plot(x, func_pow(x, *popt), "r--", lw=2)
Out[25]:
In [26]:
r2_score(y, func_pow(x, *popt))
Out[26]:
Copy to clipboard¶
In [27]:
df = pd.DataFrame(np.vstack((x, y)).T)
df.to_clipboard(index=False, header=False)
In [ ]:
In [ ]:
In [89]:
%matplotlib widget
In [90]:
xy = np.array([[13,0.0055],
[15,0.0048],
[20,0.004],
[30,0.003],
[50,0.0022],
[100,0.00165],
[500,0.0012]])
x = xy[:, 0]
y = xy[:, 1]
In [91]:
def func_pow(x, a, b, c):
return a * x**(x*b+c)
In [92]:
popt, pcov = curve_fit(func_pow, x, y, ftol=1e-15, xtol=1e-15, gtol=1e-16,
bounds=(-100, 100))
popt, pcov
Out[92]:
In [93]:
fig, ax = plt.subplots()
ax.plot(x,y,"+--", ms=3, mec="k")
x = np.linspace(0, 500, 100)
ax.plot(x, func_pow(x, *popt), "r--", lw=2)
Out[93]:
In [ ]:
In [ ]: