scipy
print("Hello World")
Hello World
class Number:
def __init__(self, value):
self.value = value
# Klärender Kommentar
def sum(self):
if self.value <= 0:
return 0
else:
s = 0
for i in range(self.value):
s += i
return s
fifty = Number(50)
fifty.sum()
zeile = [0,0]
matrix = [zeile, zeile]
matrix
matrix[0][0] = 1
matrix
qtconsole
¶notebook
¶Interaktive Zellen-Basierte Oberfläche im Browser
Unterstützt LaTeX-Formeln
HTML und Markdown Formatierung von Text
Dateiformat geeignet für Arbeitsblätter
nbconvert
¶scipy
¶sympy
¶from sympy import *
x = symbols('x', real=True)
solve(x**2 + 4*x - 5, x)
sympy
¶42 # Beliebig grosse Integer in Python
(1/42)**(1/2) # Eingeschränkte 64-bit Fliesskommazahlen
Rational(1,42)**Rational(0.5)
S('(1/42)^(1/2)') # sympyify...
(x**3 + 4*x**2 + sin(x)**2*x + cos(x)**2*x) / x - 1
expr = _.simplify()
expr
expr.expand()
expr
expr.subs(x, 4)
Eq(expr, 5)
solve(Eq(expr, 5), x)
f = x**3 + 2*x**2 + exp(2*x) - sin(x)
f
f.diff(x)
f.integrate(x)
f.integrate((x,-1, 3))
N(_)
numpy
Arrays¶import numpy as np
data = np.array([[1, 2, 3, 4], [2, 3, -2, 3], [-4, 3, 2, -1]])
data
array([[ 1, 2, 3, 4], [ 2, 3, -2, 3], [-4, 3, 2, -1]])
np.arange(1, 10)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.linspace(0.5, 6.5, 9) # Bereiche mit nicht-ganzzahligen Schritten
array([ 0.5 , 1.25, 2. , 2.75, 3.5 , 4.25, 5. , 5.75, 6.5 ])
# Rechnen mit Arrays
data / 4 + 1
array([[ 1.25, 1.5 , 1.75, 2. ], [ 1.5 , 1.75, 0.5 , 1.75], [ 0. , 1.75, 1.5 , 0.75]])
data + np.array([5, 4, 3, 5])
array([[6, 6, 6, 9], [7, 7, 1, 8], [1, 7, 5, 4]])
np.abs(data)
array([[1, 2, 3, 4], [2, 3, 2, 3], [4, 3, 2, 1]])
matplotlib
¶2D und 3D Darstellungen von Daten
http://matplotlib.org/examples/
simple_3danim.py
slider_demo.py
from sympy import *
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from IPython.display import display
# Oft benutze Sympy-Variablen
x, y, z = symbols("x,y,z", real=True)
k, m, n = symbols("k,m,n", integer=True)
# Funktionen und Variablen für Vektorgeometrie
s,t = symbols("s,t", real=True)
Vec = lambda *args: Matrix([[x] for x in args])
# LaTeX-Formeln anzeigen
init_printing()
# Plots direkt im Dokument anzeigen
# (Nur für Jupyter-Notebook, nicht für iPython)
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
# P_1 = Matrix([[1], [19.25], [0]])
P_1 = Vec(1, 19.25, 0)
P_2 = Vec(2, 22, -3.5)
r_1 = Vec(0, 0, 1)
r_2 = Vec(-1, -0.5, 1.5)
Eq(Vec(x,y,z), P_2 + s*r_2)
n = r_1.cross(r_2)
n
Eq(n.dot(Vec(x,y,z)), n.dot(P_1))
(n.dot(P_2) - n.dot(P_1))/n.norm()
abs(_)
g_1 = P_1 + s*r_1
g_2 = P_2 + t*r_2
d = (g_2 - g_1).norm()
d
from scipy.optimize import minimize
def dist(vals):
return d.subs(s, vals[0]).subs(t, vals[1])
minimize(dist, (0,0))
fun: 2.012461179750537 hess_inv: array([[ 5.49142869, 2.31123865], [ 2.31123865, 1.53496148]]) jac: array([ -8.94069672e-08, 1.07288361e-06]) message: 'Optimization terminated successfully.' nfev: 48 nit: 10 njev: 12 status: 0 success: True x: array([-0.64999788, 1.90000152])
Bei welcher Geschwindigkeit passen am meisten Autos durch eine Strasse?
def bremsweg(v, a):
return v**2 / (2*a)
# Zeit pro Auto:
def fahrzeug_zeit(v, a = 8, t_r = 1.2, l = 5):
platz_pro_auto = l + t_r*v + bremsweg(v, a)
return platz_pro_auto / v
v = np.linspace(1, 40)
plt.plot(v, fahrzeug_zeit(v))
[<matplotlib.lines.Line2D at 0x7f1ef23e4320>]
# Numerische Lösung
from scipy.optimize import minimize
result = minimize(fahrzeug_zeit, 5)
if result["success"]:
opt_speed = result["x"][0]
# In Kilometer pro Stunde
opt_speed * 3.6
# Algebraische Lösung mit Sympy
v, a, t_r, l = symbols("v, a, t_r, l", real=True)
fahrzeug_zeit(v, a, t_r, l)
ziel_func = _.simplify()
ziel_func
ziel_func.diff(v)
solve(_, v)
_[1]
_.subs(a,8).subs(l,5)
_.n() * 3.6
import scipy.stats as st
n = 30
p = 0.4
x = np.arange(0, n + 1)
xn = np.linspace(0,n,100)
y = st.binom(n, p).pmf(x)
yn = st.norm.pdf(xn, loc=n*p, scale=(n*p*(1-p))**0.5)
plt.bar(x, y, align="center", width=0.8)
plt.plot(xn, yn, color="red", linewidth=2)
[<matplotlib.lines.Line2D at 0x7f1ef1c77208>]
In einer Klasse wurden bei einer Semesterprüfung die Vorbereitungszeiten und die erreichten Punktzahlen der einzelnen Schülerinnen und Schüler festgehalten (Punktemaximum 100).
Zeit | $$3.0$$ | $$4.2$$ | $$2.0$$ | $$9.3$$ | $$5.2$$ | $$5.5$$ | $$0.5$$ | $$6.2$$ | $$7.6$$ | $$1.0$$ | $$1.0$$ | $$3.5$$ | $$8.4$$ | $$1.4$$ | $$3.5$$ |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Punkte | $$82$$ | $$90$$ | $$53$$ | $$89$$ | $$74$$ | $$78$$ | $$40$$ | $$87$$ | $$87$$ | $$47$$ | $$60$$ | $$88$$ | $$100$$ | $$48$$ | $$87$$ |
Zeit | $$4.9$$ | $$7.0$$ | $$2.6$$ | $$0.5$$ | $$8.2$$ | $$5.1$$ | $$1.5$$ | $$3.7$$ | $$6.5$$ | $$3.0$$ | $$2.5$$ | $$2.4$$ | $$5.4$$ | $$2.3$$ | $$5.4$$ |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Punkte | $$93$$ | $$96$$ | $$75$$ | $$56$$ | $$94$$ | $$86$$ | $$73$$ | $$85$$ | $$85$$ | $$86$$ | $$71$$ | $$82$$ | $$70$$ | $$61$$ | $$92$$ |
Stelle die Daten graphisch dar.
Bestimme die Gleichung der besten Regressionskurve und füge sie in den Plot ein.
Wir betrachten die folgenden Modelle:
Logarithmische $x$- und $y$-Achse: $$f(x) = a\cdot x^b$$
Logarithmische $x$-Achse: $$f(x) = a\cdot \ln(x) + b$$
Logarithmische $y$-Achse: $$f(x) = a\cdot b^x$$
time = np.array([3.0,4.2,2.0,9.3,5.2,5.5,0.5,6.2,7.6,1.0,1.0,3.5,8.4,1.4,3.5,4.9,7.0,2.6,0.5,8.2,5.1,1.5,3.7,6.5,3.0,2.5,2.4,5.4,2.3,5.4])
points = np.array([82,90,53,89,74,78,40,87,87,47,60,88,100,48,87,93,96,75,56,94,86,73,85,85,86,71,82,70,61,92])
plt.plot(time, points, 'o')
[<matplotlib.lines.Line2D at 0x7f1ef1820518>]
plt.plot(time, points, 'o')
plt.yscale('log')
plt.show()
plt.plot(time, points, 'o')
plt.yscale('log')
plt.xscale('log')
plt.show()
plt.plot(time, points, 'o')
plt.xscale('log')
plt.show()
from scipy.stats import linregress
# Exponentialfunktion
slope_e, intercept_e, r_e = linregress(time, np.log(points))[:3]
# Potenzfunktion
slope_p, intercept_p, r_p = linregress(np.log(time), np.log(points))[:3]
# Logarithmusfunktion
slope_l, intercept_l, r_l = linregress(np.log(time), points)[:3]
r_e, r_p, r_l
def potenzfkt(x):
return np.exp(intercept_p)*x**slope_p
def logfkt(x):
return slope_l*np.log(x) + intercept_l
time_vals = np.linspace(min(time), max(time))
plt.plot(time, points, 'o')
plt.plot(time_vals, potenzfkt(time_vals), linewidth=2)
plt.plot(time_vals, logfkt(time_vals), linewidth=2)
[<matplotlib.lines.Line2D at 0x7f1ef1637898>]