Numpy

In [1]:
import numpy as np
In [2]:
x = np.array([1, 2, 3, 4, 5])
In [3]:
2 * x
# x + x
Out[3]:
array([ 2,  4,  6,  8, 10])
In [4]:
x**2
Out[4]:
array([ 1,  4,  9, 16, 25])
In [5]:
x**x
Out[5]:
array([   1,    4,   27,  256, 3125])
In [6]:
np.cos(x)
Out[6]:
array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362,  0.28366219])

Achtung: Man braucht das cos aus numpy!

In [7]:
import math
math.cos(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-25067472c670> in <module>()
      1 import math
----> 2 math.cos(x)

TypeError: only length-1 arrays can be converted to Python scalars

Arrays können beliebige Dimension haben:

In [8]:
# zweidimensionales Array
y = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

y + y
Out[8]:
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [14, 16, 18]])

Es gibt viele nützliche Funktionen, die Arrays erstellen:

In [9]:
np.zeros(10)
Out[9]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
In [10]:
np.ones(5)
Out[10]:
array([ 1.,  1.,  1.,  1.,  1.])
In [11]:
# kennen wir schon:
np.linspace(0, 1, 11)
Out[11]:
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ])
In [12]:
# wie range() für arrays:
np.arange(0, 10)
Out[12]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Numpy Indexing

Numpy erlaubt einem sehr bequem bestimmte Elemente aus einem Array auszuwählen

In [13]:
x = np.arange(0, 10)

# kennen wir schon:
x[4]
Out[13]:
4
In [14]:
# alle Elemente mit Index 1 bis 3:
x[1:4]
Out[14]:
array([1, 2, 3])
In [15]:
# negative Indizes zählen vom Ende des Arrays:
x[-1], x[-2]
Out[15]:
(9, 8)
In [16]:
# Kombination aus beiden:
x[3:-2]
Out[16]:
array([3, 4, 5, 6, 7])
In [17]:
# man kann eine Schrittgröße angeben
x[::2]
Out[17]:
array([0, 2, 4, 6, 8])
In [18]:
# Trick zum Umkehren: negative Schrittgröße
x[::-1]
Out[18]:
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
In [19]:
y = np.array([x, x + 10, x + 20, x + 30])
y
Out[19]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]])
In [20]:
# mehrere Indizes mit Kommas getrennt:
y[3, 2:-1]
Out[20]:
array([32, 33, 34, 35, 36, 37, 38])
In [21]:
# Oder man gibt nur einen Index an ⇒ eindimensionales Array
y[2]
Out[21]:
array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29])

Reduzieren von Arrays

Viele Rechenoperationen reduzieren ein Array auf einen einzelnen Wert

In [22]:
x
Out[22]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [23]:
np.sum(x)
Out[23]:
45
In [24]:
np.prod(x)
Out[24]:
0
In [25]:
np.mean(x)
Out[25]:
4.5
In [26]:
# Standardabweichung:
np.std(x)
Out[26]:
2.8722813232690143
In [27]:
# Fehler des Mittelwerts:
np.std(x) / np.sqrt(len(x))
Out[27]:
0.90829510622924747
In [28]:
# Differenzen zwischen benachbarten Elementen
np.ediff1d(x)
Out[28]:
array([1, 1, 1, 1, 1, 1, 1, 1, 1])

Input / Output

Die Funktion loadtxt kennen wir schon.

Sie gibt den Inhalt einer Textdatei als Array zurück.

Das Gegenstück ist savetxt.

In [29]:
n = np.arange(11)
x = np.linspace(0, 1, 11)

np.savetxt('test.txt', [n, x])

Die Datei enthält jetzt

0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
0. 0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.

Laden wir jetzt die Daten:

In [30]:
a, b = np.loadtxt('test.txt')

print(a)
print(b)
[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]
[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1. ]

Oft schreibt man seine Datenpunkte aber spaltenweise:

1  1
2  4
3  9
4 16
5 20

Dann kann man unpack=True verwenden, um die Daten zu transponieren. Um die Daten entsprechend transponiert zu speichern, verwenden wir

In [31]:
np.savetxt('test_T.txt', np.array([n, x]).T)

SciPy

SciPy

SciPy

In [32]:
# Fehler des Mittelwerts:
from scipy.stats import sem
sem(x)
Out[32]:
0.10000000000000002
In [33]:
# Physikalische Konstanten:
import scipy.constants as const
const.epsilon_0
Out[33]:
8.854187817620389e-12
In [34]:
# Temperaturen konvertieren:
const.C2K(100), const.K2C(100)
Out[34]:
(373.14999999999998, -173.14999999999998)
In [35]:
# noch mehr Konstanten (mit Fehler und Einheit)!
const.physical_constants
Out[35]:
{'Angstrom star': (1.00001495e-10, 'm', 9e-17),
 'Avogadro constant': (6.02214129e+23, 'mol^-1', 2.7e+16),
 'Bohr magneton': (9.27400968e-24, 'J T^-1', 2e-31),
 'Bohr magneton in Hz/T': (13996245550.0, 'Hz T^-1', 310.0),
 'Bohr magneton in K/T': (0.67171388, 'K T^-1', 6.1e-07),
 'Bohr magneton in eV/T': (5.7883818066e-05, 'eV T^-1', 3.8e-14),
 'Bohr magneton in inverse meters per tesla': (46.6864498, 'm^-1 T^-1', 1e-06),
 'Bohr radius': (5.2917721092e-11, 'm', 1.7e-20),
 'Boltzmann constant': (1.3806488e-23, 'J K^-1', 1.3e-29),
 'Boltzmann constant in Hz/K': (20836618000.0, 'Hz K^-1', 19000.0),
 'Boltzmann constant in eV/K': (8.6173324e-05, 'eV K^-1', 7.8e-11),
 'Boltzmann constant in inverse meters per kelvin': (69.503476,
  'm^-1 K^-1',
  6.3e-05),
 'Compton wavelength': (2.4263102389e-12, 'm', 1.6e-21),
 'Compton wavelength over 2 pi': (3.86159268e-13, 'm', 2.5e-22),
 'Cu x unit': (1.00207697e-13, 'm', 2.8e-20),
 'Faraday constant': (96485.3365, 'C mol^-1', 0.0021),
 'Faraday constant for conventional electric current': (96485.3321,
  'C_90 mol^-1',
  0.0043),
 'Fermi coupling constant': (1.166364e-05, 'GeV^-2', 5e-11),
 'Hartree energy': (4.35974434e-18, 'J', 1.9e-25),
 'Hartree energy in eV': (27.21138505, 'eV', 6e-07),
 'Josephson constant': (483597870000000.0, 'Hz V^-1', 11000000.0),
 'Loschmidt constant (273.15 K, 100 kPa)': (2.6516462e+25, 'm^-3', 2.4e+19),
 'Loschmidt constant (273.15 K, 101.325 kPa)': (2.6867805e+25,
  'm^-3',
  2.4e+19),
 'Mo x unit': (1.00209952e-13, 'm', 5.3e-20),
 'Newtonian constant of gravitation': (6.67384e-11, 'm^3 kg^-1 s^-2', 8e-15),
 'Newtonian constant of gravitation over h-bar c': (6.70837e-39,
  '(GeV/c^2)^-2',
  8e-43),
 'Planck constant': (6.62606957e-34, 'J s', 2.9e-41),
 'Planck constant in eV s': (4.135667516e-15, 'eV s', 9.1e-23),
 'Planck constant over 2 pi': (1.054571726e-34, 'J s', 4.7e-42),
 'Planck constant over 2 pi in eV s': (6.58211928e-16, 'eV s', 1.5e-23),
 'Planck constant over 2 pi times c in MeV fm': (197.3269718,
  'MeV fm',
  4.4e-06),
 'Planck length': (1.616199e-35, 'm', 9.7e-40),
 'Planck mass': (2.17651e-08, 'kg', 1.3e-12),
 'Planck mass energy equivalent in GeV': (1.220932e+19,
  'GeV',
  730000000000000.0),
 'Planck temperature': (1.416833e+32, 'K', 8.5e+27),
 'Planck time': (5.39106e-44, 's', 3.2e-48),
 'Rydberg constant': (10973731.568539, 'm^-1', 5.5e-05),
 'Rydberg constant times c in Hz': (3289841960364000.0, 'Hz', 17000.0),
 'Rydberg constant times hc in J': (2.179872171e-18, 'J', 9.6e-26),
 'Rydberg constant times hc in eV': (13.60569253, 'eV', 3e-07),
 'Sackur-Tetrode constant (1 K, 100 kPa)': (-1.1517078, '', 2.3e-06),
 'Sackur-Tetrode constant (1 K, 101.325 kPa)': (-1.1648708, '', 2.3e-06),
 'Stefan-Boltzmann constant': (5.670373e-08, 'W m^-2 K^-4', 2.1e-13),
 'Thomson cross section': (6.652458734e-29, 'm^2', 1.3e-37),
 'Wien displacement law constant': (0.0028977685, 'm K', 5.1e-09),
 'Wien frequency displacement law constant': (58789254000.0,
  'Hz K^-1',
  53000.0),
 'Wien wavelength displacement law constant': (0.0028977721, 'm K', 2.6e-09),
 'alpha particle mass': (6.64465675e-27, 'kg', 2.9e-34),
 'alpha particle mass energy equivalent': (5.97191967e-10, 'J', 2.6e-17),
 'alpha particle mass energy equivalent in MeV': (3727.37924, 'MeV', 8.2e-05),
 'alpha particle mass in u': (4.001506179125, 'u', 6.2e-11),
 'alpha particle molar mass': (0.004001506179125, 'kg mol^-1', 6.2e-14),
 'alpha particle-electron mass ratio': (7294.2995361, '', 2.9e-06),
 'alpha particle-proton mass ratio': (3.97259968933, '', 3.6e-10),
 'atomic mass constant': (1.660538921e-27, 'kg', 7.3e-35),
 'atomic mass constant energy equivalent': (1.492417954e-10, 'J', 6.6e-18),
 'atomic mass constant energy equivalent in MeV': (931.494061, 'MeV', 2.1e-05),
 'atomic mass unit-electron volt relationship': (931494061.0, 'eV', 21.0),
 'atomic mass unit-hartree relationship': (34231776.845, 'E_h', 0.024),
 'atomic mass unit-hertz relationship': (2.2523427168e+23,
  'Hz',
  160000000000000.0),
 'atomic mass unit-inverse meter relationship': (751300660420000.0,
  'm^-1',
  530000.0),
 'atomic mass unit-joule relationship': (1.492417954e-10, 'J', 6.6e-18),
 'atomic mass unit-kelvin relationship': (10809540800000.0, 'K', 9800000.0),
 'atomic mass unit-kilogram relationship': (1.660538921e-27, 'kg', 7.3e-35),
 'atomic unit of 1st hyperpolarizability': (3.206361449e-53,
  'C^3 m^3 J^-2',
  7.1e-61),
 'atomic unit of 1st hyperpolarizablity': (3.20636151e-53,
  'C^3 m^3 J^-2',
  2.8e-60),
 'atomic unit of 2nd hyperpolarizability': (6.23538054e-65,
  'C^4 m^4 J^-3',
  2.8e-72),
 'atomic unit of 2nd hyperpolarizablity': (6.2353808e-65,
  'C^4 m^4 J^-3',
  1.1e-71),
 'atomic unit of action': (1.054571726e-34, 'J s', 4.7e-42),
 'atomic unit of charge': (1.602176565e-19, 'C', 3.5e-27),
 'atomic unit of charge density': (1081202338000.0, 'C m^-3', 24000.0),
 'atomic unit of current': (0.00662361795, 'A', 1.5e-10),
 'atomic unit of electric dipole mom.': (8.47835326e-30, 'C m', 1.9e-37),
 'atomic unit of electric dipole moment': (8.47835309e-30, 'C m', 7.3e-37),
 'atomic unit of electric field': (514220652000.0, 'V m^-1', 11000.0),
 'atomic unit of electric field gradient': (9.717362e+21,
  'V m^-2',
  210000000000000.0),
 'atomic unit of electric polarizability': (1.6487772754e-41,
  'C^2 m^2 J^-1',
  1.6e-50),
 'atomic unit of electric polarizablity': (1.648777274e-41,
  'C^2 m^2 J^-1',
  1.6e-49),
 'atomic unit of electric potential': (27.21138505, 'V', 6e-07),
 'atomic unit of electric quadrupole mom.': (4.486551331e-40,
  'C m^2',
  9.9e-48),
 'atomic unit of electric quadrupole moment': (4.48655124e-40,
  'C m^2',
  3.9e-47),
 'atomic unit of energy': (4.35974434e-18, 'J', 1.9e-25),
 'atomic unit of force': (8.23872278e-08, 'N', 3.6e-15),
 'atomic unit of length': (5.2917721092e-11, 'm', 1.7e-20),
 'atomic unit of mag. dipole mom.': (1.854801936e-23, 'J T^-1', 4.1e-31),
 'atomic unit of mag. flux density': (235051.7464, 'T', 0.0052),
 'atomic unit of magn. dipole moment': (1.8548019e-23, 'J T^-1', 1.6e-30),
 'atomic unit of magn. flux density': (235051.7464, 'T', 0.0052),
 'atomic unit of magnetizability': (7.891036607e-29, 'J T^-2', 1.3e-37),
 'atomic unit of mass': (9.10938291e-31, 'kg', 4e-38),
 'atomic unit of mom.um': (1.99285174e-24, 'kg m s^-1', 8.8e-32),
 'atomic unit of momentum': (1.99285174e-24, 'kg m s^-1', 8.8e-32),
 'atomic unit of permittivity': (1.1126500560536183e-10, 'F m^-1', 0.0),
 'atomic unit of time': (2.418884326502e-17, 's', 1.2e-28),
 'atomic unit of velocity': (2187691.26379, 'm s^-1', 0.00071),
 'characteristic impedance of vacuum': (376.73031346177066, 'ohm', 0.0),
 'classical electron radius': (2.8179403267e-15, 'm', 2.7e-24),
 'conductance quantum': (7.7480917346e-05, 'S', 2.5e-14),
 'conventional value of Josephson constant': (483597900000000.0,
  'Hz V^-1',
  0.0),
 'conventional value of von Klitzing constant': (25812.807, 'ohm', 0.0),
 'deuteron g factor': (0.8574382308, '', 7.2e-09),
 'deuteron mag. mom.': (4.33073489e-27, 'J T^-1', 1e-34),
 'deuteron mag. mom. to Bohr magneton ratio': (0.0004669754556, '', 3.9e-12),
 'deuteron mag. mom. to nuclear magneton ratio': (0.8574382308, '', 7.2e-09),
 'deuteron magn. moment': (4.33073482e-27, 'J T^-1', 3.8e-34),
 'deuteron magn. moment to Bohr magneton ratio': (0.0004669754567, '', 5e-12),
 'deuteron magn. moment to nuclear magneton ratio': (0.8574382329,
  '',
  9.2e-09),
 'deuteron mass': (3.34358348e-27, 'kg', 1.5e-34),
 'deuteron mass energy equivalent': (3.00506297e-10, 'J', 1.3e-17),
 'deuteron mass energy equivalent in MeV': (1875.612859, 'MeV', 4.1e-05),
 'deuteron mass in u': (2.013553212712, 'u', 7.7e-11),
 'deuteron molar mass': (0.002013553212712, 'kg mol^-1', 7.7e-14),
 'deuteron rms charge radius': (2.1424e-15, 'm', 2.1e-18),
 'deuteron-electron mag. mom. ratio': (-0.0004664345537, '', 3.9e-12),
 'deuteron-electron magn. moment ratio': (-0.0004664345548, '', 5e-12),
 'deuteron-electron mass ratio': (3670.4829652, '', 1.5e-06),
 'deuteron-neutron mag. mom. ratio': (-0.44820652, '', 1.1e-07),
 'deuteron-neutron magn. moment ratio': (-0.44820652, '', 1.1e-07),
 'deuteron-proton mag. mom. ratio': (0.307012207, '', 2.4e-09),
 'deuteron-proton magn. moment ratio': (0.3070122084, '', 4.5e-09),
 'deuteron-proton mass ratio': (1.99900750097, '', 1.8e-10),
 'electric constant': (8.854187817620389e-12, 'F m^-1', 0.0),
 'electron charge to mass quotient': (-175882008800.0, 'C kg^-1', 3900.0),
 'electron g factor': (-2.00231930436153, '', 5.3e-13),
 'electron gyromag. ratio': (176085970800.0, 's^-1 T^-1', 3900.0),
 'electron gyromag. ratio over 2 pi': (28024.95266, 'MHz T^-1', 0.00062),
 'electron gyromagn. ratio': (176085970800.0, 's^-1 T^-1', 3900.0),
 'electron gyromagn. ratio over 2 pi': (28024.95266, 'MHz T^-1', 0.00062),
 'electron mag. mom.': (-9.2847643e-24, 'J T^-1', 2.1e-31),
 'electron mag. mom. anomaly': (0.00115965218076, '', 2.7e-13),
 'electron mag. mom. to Bohr magneton ratio': (-1.00115965218076, '', 2.7e-13),
 'electron mag. mom. to nuclear magneton ratio': (-1838.2819709, '', 7.5e-07),
 'electron magn. moment': (-9.28476412e-24, 'J T^-1', 8e-31),
 'electron magn. moment anomaly': (0.0011596521859, '', 3.8e-12),
 'electron magn. moment to Bohr magneton ratio': (-1.0011596521859,
  '',
  3.8e-12),
 'electron magn. moment to nuclear magneton ratio': (-1838.28197107,
  '',
  8.5e-07),
 'electron mass': (9.10938291e-31, 'kg', 4e-38),
 'electron mass energy equivalent': (8.18710506e-14, 'J', 3.6e-21),
 'electron mass energy equivalent in MeV': (0.510998928, 'MeV', 1.1e-08),
 'electron mass in u': (0.00054857990946, 'u', 2.2e-13),
 'electron molar mass': (5.4857990946e-07, 'kg mol^-1', 2.2e-16),
 'electron to alpha particle mass ratio': (0.000137093355578, '', 5.5e-14),
 'electron to shielded helion mag. mom. ratio': (864.058257, '', 1e-05),
 'electron to shielded helion magn. moment ratio': (864.058255, '', 1e-05),
 'electron to shielded proton mag. mom. ratio': (-658.2275971, '', 7.2e-06),
 'electron to shielded proton magn. moment ratio': (-658.2275956, '', 7.1e-06),
 'electron volt': (1.602176565e-19, 'J', 3.5e-27),
 'electron volt-atomic mass unit relationship': (1.07354415e-09, 'u', 2.4e-17),
 'electron volt-hartree relationship': (0.03674932379, 'E_h', 8.1e-10),
 'electron volt-hertz relationship': (241798934800000.0, 'Hz', 5300000.0),
 'electron volt-inverse meter relationship': (806554.429, 'm^-1', 0.018),
 'electron volt-joule relationship': (1.602176565e-19, 'J', 3.5e-27),
 'electron volt-kelvin relationship': (11604.519, 'K', 0.011),
 'electron volt-kilogram relationship': (1.782661845e-36, 'kg', 3.9e-44),
 'electron-deuteron mag. mom. ratio': (-2143.923498, '', 1.8e-05),
 'electron-deuteron magn. moment ratio': (-2143.923493, '', 2.3e-05),
 'electron-deuteron mass ratio': (0.00027244371095, '', 1.1e-13),
 'electron-helion mass ratio': (0.00018195430761, '', 1.7e-13),
 'electron-muon mag. mom. ratio': (206.7669896, '', 5.2e-06),
 'electron-muon magn. moment ratio': (206.7669894, '', 5.4e-06),
 'electron-muon mass ratio': (0.00483633166, '', 1.2e-10),
 'electron-neutron mag. mom. ratio': (960.9205, '', 0.00023),
 'electron-neutron magn. moment ratio': (960.9205, '', 0.00023),
 'electron-neutron mass ratio': (0.00054386734461, '', 3.2e-13),
 'electron-proton mag. mom. ratio': (-658.2106848, '', 5.4e-06),
 'electron-proton magn. moment ratio': (-658.2106862, '', 6.6e-06),
 'electron-proton mass ratio': (0.00054461702178, '', 2.2e-13),
 'electron-tau mass ratio': (0.000287592, '', 2.6e-08),
 'electron-triton mass ratio': (0.00018192000653, '', 1.7e-13),
 'elementary charge': (1.602176565e-19, 'C', 3.5e-27),
 'elementary charge over h': (241798934800000.0, 'A J^-1', 5300000.0),
 'fine-structure constant': (0.0072973525698, '', 2.4e-12),
 'first radiation constant': (3.74177153e-16, 'W m^2', 1.7e-23),
 'first radiation constant for spectral radiance': (1.191042869e-16,
  'W m^2 sr^-1',
  5.3e-24),
 'hartree-atomic mass unit relationship': (2.9212623246e-08, 'u', 2.1e-17),
 'hartree-electron volt relationship': (27.21138505, 'eV', 6e-07),
 'hartree-hertz relationship': (6579683920729000.0, 'Hz', 33000.0),
 'hartree-inverse meter relationship': (21947463.13708, 'm^-1', 0.00011),
 'hartree-joule relationship': (4.35974434e-18, 'J', 1.9e-25),
 'hartree-kelvin relationship': (315775.04, 'K', 0.29),
 'hartree-kilogram relationship': (4.85086979e-35, 'kg', 2.1e-42),
 'helion g factor': (-4.255250613, '', 5e-08),
 'helion mag. mom.': (-1.074617486e-26, 'J T^-1', 2.7e-34),
 'helion mag. mom. to Bohr magneton ratio': (-0.001158740958, '', 1.4e-11),
 'helion mag. mom. to nuclear magneton ratio': (-2.127625306, '', 2.5e-08),
 'helion mass': (5.00641234e-27, 'kg', 2.2e-34),
 'helion mass energy equivalent': (4.49953902e-10, 'J', 2e-17),
 'helion mass energy equivalent in MeV': (2808.391482, 'MeV', 6.2e-05),
 'helion mass in u': (3.0149322468, 'u', 2.5e-09),
 'helion molar mass': (0.0030149322468, 'kg mol^-1', 2.5e-12),
 'helion-electron mass ratio': (5495.8852754, '', 5e-06),
 'helion-proton mass ratio': (2.9931526707, '', 2.5e-09),
 'hertz-atomic mass unit relationship': (4.4398216689e-24, 'u', 3.1e-33),
 'hertz-electron volt relationship': (4.135667516e-15, 'eV', 9.1e-23),
 'hertz-hartree relationship': (1.5198298460045e-16, 'E_h', 7.6e-28),
 'hertz-inverse meter relationship': (3.3356409519815204e-09, 'm^-1', 0.0),
 'hertz-joule relationship': (6.62606957e-34, 'J', 2.9e-41),
 'hertz-kelvin relationship': (4.7992434e-11, 'K', 4.4e-17),
 'hertz-kilogram relationship': (7.37249668e-51, 'kg', 3.3e-58),
 'inverse fine-structure constant': (137.035999074, '', 4.4e-08),
 'inverse meter-atomic mass unit relationship': (1.3310250512e-15,
  'u',
  9.4e-25),
 'inverse meter-electron volt relationship': (1.23984193e-06, 'eV', 2.7e-14),
 'inverse meter-hartree relationship': (4.556335252755e-08, 'E_h', 2.3e-19),
 'inverse meter-hertz relationship': (299792458.0, 'Hz', 0.0),
 'inverse meter-joule relationship': (1.986445684e-25, 'J', 8.8e-33),
 'inverse meter-kelvin relationship': (0.01438777, 'K', 1.3e-08),
 'inverse meter-kilogram relationship': (2.210218902e-42, 'kg', 9.8e-50),
 'inverse of conductance quantum': (12906.4037217, 'ohm', 4.2e-06),
 'joule-atomic mass unit relationship': (6700535850.0, 'u', 300.0),
 'joule-electron volt relationship': (6.24150934e+18, 'eV', 140000000000.0),
 'joule-hartree relationship': (2.29371248e+17, 'E_h', 10000000000.0),
 'joule-hertz relationship': (1.509190311e+33, 'Hz', 6.7e+25),
 'joule-inverse meter relationship': (5.03411701e+24, 'm^-1', 2.2e+17),
 'joule-kelvin relationship': (7.2429716e+22, 'K', 6.6e+16),
 'joule-kilogram relationship': (1.1126500560536185e-17, 'kg', 0.0),
 'kelvin-atomic mass unit relationship': (9.2510868e-14, 'u', 8.4e-20),
 'kelvin-electron volt relationship': (8.6173324e-05, 'eV', 7.8e-11),
 'kelvin-hartree relationship': (3.1668114e-06, 'E_h', 2.9e-12),
 'kelvin-hertz relationship': (20836618000.0, 'Hz', 19000.0),
 'kelvin-inverse meter relationship': (69.503476, 'm^-1', 6.3e-05),
 'kelvin-joule relationship': (1.3806488e-23, 'J', 1.3e-29),
 'kelvin-kilogram relationship': (1.536179e-40, 'kg', 1.4e-46),
 'kilogram-atomic mass unit relationship': (6.02214129e+26, 'u', 2.7e+19),
 'kilogram-electron volt relationship': (5.60958885e+35, 'eV', 1.2e+28),
 'kilogram-hartree relationship': (2.061485968e+34, 'E_h', 9.1e+26),
 'kilogram-hertz relationship': (1.356392608e+50, 'Hz', 6e+42),
 'kilogram-inverse meter relationship': (4.52443873e+41, 'm^-1', 2e+34),
 'kilogram-joule relationship': (8.987551787368176e+16, 'J', 0.0),
 'kilogram-kelvin relationship': (6.5096582e+39, 'K', 5.9e+33),
 'lattice parameter of silicon': (5.431020504e-10, 'm', 8.9e-18),
 'lattice spacing of silicon': (1.920155762e-10, 'm', 5e-18),
 'mag. constant': (1.2566370614359173e-06, 'N A^-2', 0.0),
 'mag. flux quantum': (2.067833758e-15, 'Wb', 4.6e-23),
 'magn. constant': (1.2566370614359173e-06, 'N A^-2', 0.0),
 'magn. flux quantum': (2.067833758e-15, 'Wb', 4.6e-23),
 'molar Planck constant': (3.9903127176e-10, 'J s mol^-1', 2.8e-19),
 'molar Planck constant times c': (0.119626565779, 'J m mol^-1', 8.4e-11),
 'molar gas constant': (8.3144621, 'J mol^-1 K^-1', 7.5e-06),
 'molar mass constant': (0.001, 'kg mol^-1', 0.0),
 'molar mass of carbon-12': (0.012, 'kg mol^-1', 0.0),
 'molar volume of ideal gas (273.15 K, 100 kPa)': (0.022710953,
  'm^3 mol^-1',
  2.1e-08),
 'molar volume of ideal gas (273.15 K, 101.325 kPa)': (0.022413968,
  'm^3 mol^-1',
  2e-08),
 'molar volume of silicon': (1.205883301e-05, 'm^3 mol^-1', 8e-13),
 'muon Compton wavelength': (1.173444103e-14, 'm', 3e-22),
 'muon Compton wavelength over 2 pi': (1.867594294e-15, 'm', 4.7e-23),
 'muon g factor': (-2.0023318418, '', 1.3e-09),
 'muon mag. mom.': (-4.49044807e-26, 'J T^-1', 1.5e-33),
 'muon mag. mom. anomaly': (0.00116592091, '', 6.3e-10),
 'muon mag. mom. to Bohr magneton ratio': (-0.00484197044, '', 1.2e-10),
 'muon mag. mom. to nuclear magneton ratio': (-8.89059697, '', 2.2e-07),
 'muon magn. moment': (-4.49044799e-26, 'J T^-1', 4e-33),
 'muon magn. moment to Bohr magneton ratio': (-0.00484197045, '', 1.3e-10),
 'muon magn. moment to nuclear magneton ratio': (-8.89059698, '', 2.3e-07),
 'muon mass': (1.883531475e-28, 'kg', 9.6e-36),
 'muon mass energy equivalent': (1.692833667e-11, 'J', 8.6e-19),
 'muon mass energy equivalent in MeV': (105.6583715, 'MeV', 3.5e-06),
 'muon mass in u': (0.1134289267, 'u', 2.9e-09),
 'muon molar mass': (0.0001134289267, 'kg mol^-1', 2.9e-12),
 'muon-electron mass ratio': (206.7682843, '', 5.2e-06),
 'muon-neutron mass ratio': (0.1124545177, '', 2.8e-09),
 'muon-proton mag. mom. ratio': (-3.183345107, '', 8.4e-08),
 'muon-proton magn. moment ratio': (-3.183345118, '', 8.9e-08),
 'muon-proton mass ratio': (0.1126095272, '', 2.8e-09),
 'muon-tau mass ratio': (0.0594649, '', 5.4e-06),
 'natural unit of action': (1.054571726e-34, 'J s', 4.7e-42),
 'natural unit of action in eV s': (6.58211928e-16, 'eV s', 1.5e-23),
 'natural unit of energy': (8.18710506e-14, 'J', 3.6e-21),
 'natural unit of energy in MeV': (0.510998928, 'MeV', 1.1e-08),
 'natural unit of length': (3.86159268e-13, 'm', 2.5e-22),
 'natural unit of mass': (9.10938291e-31, 'kg', 4e-38),
 'natural unit of mom.um': (2.73092429e-22, 'kg m s^-1', 1.2e-29),
 'natural unit of mom.um in MeV/c': (0.510998928, 'MeV/c', 1.1e-08),
 'natural unit of momentum': (2.73092429e-22, 'kg m s^-1', 1.2e-29),
 'natural unit of momentum in MeV/c': (0.510998928, 'MeV/c', 1.1e-08),
 'natural unit of time': (1.28808866833e-21, 's', 8.3e-31),
 'natural unit of velocity': (299792458.0, 'm s^-1', 0.0),
 'neutron Compton wavelength': (1.3195909068e-15, 'm', 1.1e-24),
 'neutron Compton wavelength over 2 pi': (2.1001941568e-16, 'm', 1.7e-25),
 'neutron g factor': (-3.82608545, '', 9e-07),
 'neutron gyromag. ratio': (183247179.0, 's^-1 T^-1', 43.0),
 'neutron gyromag. ratio over 2 pi': (29.1646943, 'MHz T^-1', 6.9e-06),
 'neutron gyromagn. ratio': (183247179.0, 's^-1 T^-1', 43.0),
 'neutron gyromagn. ratio over 2 pi': (29.1646943, 'MHz T^-1', 6.9e-06),
 'neutron mag. mom.': (-9.6623647e-27, 'J T^-1', 2.3e-33),
 'neutron mag. mom. to Bohr magneton ratio': (-0.00104187563, '', 2.5e-10),
 'neutron mag. mom. to nuclear magneton ratio': (-1.91304272, '', 4.5e-07),
 'neutron magn. moment': (-9.6623645e-27, 'J T^-1', 2.4e-33),
 'neutron magn. moment to Bohr magneton ratio': (-0.00104187563, '', 2.5e-10),
 'neutron magn. moment to nuclear magneton ratio': (-1.91304273, '', 4.5e-07),
 'neutron mass': (1.674927351e-27, 'kg', 7.4e-35),
 'neutron mass energy equivalent': (1.505349631e-10, 'J', 6.6e-18),
 'neutron mass energy equivalent in MeV': (939.565379, 'MeV', 2.1e-05),
 'neutron mass in u': (1.008664916, 'u', 4.3e-10),
 'neutron molar mass': (0.001008664916, 'kg mol^-1', 4.3e-13),
 'neutron to shielded proton mag. mom. ratio': (-0.68499694, '', 1.6e-07),
 'neutron to shielded proton magn. moment ratio': (-0.68499694, '', 1.6e-07),
 'neutron-electron mag. mom. ratio': (0.00104066882, '', 2.5e-10),
 'neutron-electron magn. moment ratio': (0.00104066882, '', 2.5e-10),
 'neutron-electron mass ratio': (1838.6836605, '', 1.1e-06),
 'neutron-muon mass ratio': (8.892484, '', 2.2e-07),
 'neutron-proton mag. mom. ratio': (-0.68497934, '', 1.6e-07),
 'neutron-proton magn. moment ratio': (-0.68497934, '', 1.6e-07),
 'neutron-proton mass difference': (2.30557392e-30, '', 7.6e-37),
 'neutron-proton mass difference energy equivalent': (2.0721465e-13,
  '',
  6.8e-20),
 'neutron-proton mass difference energy equivalent in MeV': (1.29333217,
  '',
  4.2e-07),
 'neutron-proton mass difference in u': (0.00138844919, '', 4.5e-10),
 'neutron-proton mass ratio': (1.00137841917, '', 4.5e-10),
 'neutron-tau mass ratio': (0.52879, '', 4.8e-05),
 'nuclear magneton': (5.05078353e-27, 'J T^-1', 1.1e-34),
 'nuclear magneton in K/T': (0.00036582682, 'K T^-1', 3.3e-10),
 'nuclear magneton in MHz/T': (7.62259357, 'MHz T^-1', 1.7e-07),
 'nuclear magneton in eV/T': (3.1524512605e-08, 'eV T^-1', 2.2e-17),
 'nuclear magneton in inverse meters per tesla': (0.02542623527,
  'm^-1 T^-1',
  5.6e-10),
 'proton Compton wavelength': (1.32140985623e-15, 'm', 9.4e-25),
 'proton Compton wavelength over 2 pi': (2.1030891047e-16, 'm', 1.5e-25),
 'proton charge to mass quotient': (95788335.8, 'C kg^-1', 2.1),
 'proton g factor': (5.585694713, '', 4.6e-08),
 'proton gyromag. ratio': (267522200.5, 's^-1 T^-1', 6.3),
 'proton gyromag. ratio over 2 pi': (42.5774806, 'MHz T^-1', 1e-06),
 'proton gyromagn. ratio': (267522200.5, 's^-1 T^-1', 6.3),
 'proton gyromagn. ratio over 2 pi': (42.5774806, 'MHz T^-1', 1e-06),
 'proton mag. mom.': (1.410606743e-26, 'J T^-1', 3.3e-34),
 'proton mag. mom. to Bohr magneton ratio': (0.00152103221, '', 1.2e-11),
 'proton mag. mom. to nuclear magneton ratio': (2.792847356, '', 2.3e-08),
 'proton mag. shielding correction': (2.5694e-05, '', 1.4e-08),
 'proton magn. moment': (1.41060671e-26, 'J T^-1', 1.2e-33),
 'proton magn. moment to Bohr magneton ratio': (0.001521032206, '', 1.5e-11),
 'proton magn. moment to nuclear magneton ratio': (2.792847351, '', 2.8e-08),
 'proton magn. shielding correction': (2.5694e-05, '', 1.4e-08),
 'proton mass': (1.672621777e-27, 'kg', 7.4e-35),
 'proton mass energy equivalent': (1.503277484e-10, 'J', 6.6e-18),
 'proton mass energy equivalent in MeV': (938.272046, 'MeV', 2.1e-05),
 'proton mass in u': (1.007276466812, 'u', 9e-11),
 'proton molar mass': (0.001007276466812, 'kg mol^-1', 9e-14),
 'proton rms charge radius': (8.775e-16, 'm', 5.1e-18),
 'proton-electron mass ratio': (1836.15267245, '', 7.5e-07),
 'proton-muon mass ratio': (8.88024331, '', 2.2e-07),
 'proton-neutron mag. mom. ratio': (-1.45989806, '', 3.4e-07),
 'proton-neutron magn. moment ratio': (-1.45989805, '', 3.4e-07),
 'proton-neutron mass ratio': (0.99862347826, '', 4.5e-10),
 'proton-tau mass ratio': (0.528063, '', 4.8e-05),
 'quantum of circulation': (0.0003636947552, 'm^2 s^-1', 2.4e-13),
 'quantum of circulation times 2': (0.0007273895104, 'm^2 s^-1', 4.7e-13),
 'second radiation constant': (0.01438777, 'm K', 1.3e-08),
 'shielded helion gyromag. ratio': (203789465.9, 's^-1 T^-1', 5.1),
 'shielded helion gyromag. ratio over 2 pi': (32.43410084,
  'MHz T^-1',
  8.1e-07),
 'shielded helion gyromagn. ratio': (203789465.9, 's^-1 T^-1', 5.1),
 'shielded helion gyromagn. ratio over 2 pi': (32.43410084,
  'MHz T^-1',
  8.1e-07),
 'shielded helion mag. mom.': (-1.074553044e-26, 'J T^-1', 2.7e-34),
 'shielded helion mag. mom. to Bohr magneton ratio': (-0.001158671471,
  '',
  1.4e-11),
 'shielded helion mag. mom. to nuclear magneton ratio': (-2.127497718,
  '',
  2.5e-08),
 'shielded helion magn. moment': (-1.074553024e-26, 'J T^-1', 9.3e-34),
 'shielded helion magn. moment to Bohr magneton ratio': (-0.001158671474,
  '',
  1.4e-11),
 'shielded helion magn. moment to nuclear magneton ratio': (-2.127497723,
  '',
  2.5e-08),
 'shielded helion to proton mag. mom. ratio': (-0.761766558, '', 1.1e-08),
 'shielded helion to proton magn. moment ratio': (-0.761766562, '', 1.2e-08),
 'shielded helion to shielded proton mag. mom. ratio': (-0.7617861313,
  '',
  3.3e-09),
 'shielded helion to shielded proton magn. moment ratio': (-0.7617861313,
  '',
  3.3e-09),
 'shielded proton gyromag. ratio': (267515326.8, 's^-1 T^-1', 6.6),
 'shielded proton gyromag. ratio over 2 pi': (42.5763866, 'MHz T^-1', 1e-06),
 'shielded proton mag. mom.': (1.410570499e-26, 'J T^-1', 3.5e-34),
 'shielded proton mag. mom. to Bohr magneton ratio': (0.001520993128,
  '',
  1.7e-11),
 'shielded proton mag. mom. to nuclear magneton ratio': (2.792775598,
  '',
  3e-08),
 'shielded proton magn. moment': (1.41057047e-26, 'J T^-1', 1.2e-33),
 'shielded proton magn. moment to Bohr magneton ratio': (0.001520993132,
  '',
  1.6e-11),
 'shielded proton magn. moment to nuclear magneton ratio': (2.792775604,
  '',
  3e-08),
 'speed of light in vacuum': (299792458.0, 'm s^-1', 0.0),
 'standard acceleration of gravity': (9.80665, 'm s^-2', 0.0),
 'standard atmosphere': (101325.0, 'Pa', 0.0),
 'standard-state pressure': (100000.0, 'Pa', 0.0),
 'tau Compton wavelength': (6.97787e-16, 'm', 6.3e-20),
 'tau Compton wavelength over 2 pi': (1.11056e-16, 'm', 1e-20),
 'tau mass': (3.16747e-27, 'kg', 2.9e-31),
 'tau mass energy equivalent': (2.84678e-10, 'J', 2.6e-14),
 'tau mass energy equivalent in MeV': (1776.82, 'MeV', 0.16),
 'tau mass in u': (1.90749, 'u', 0.00017),
 'tau molar mass': (0.00190749, 'kg mol^-1', 1.7e-07),
 'tau-electron mass ratio': (3477.15, '', 0.31),
 'tau-muon mass ratio': (16.8167, '', 0.0015),
 'tau-neutron mass ratio': (1.89111, '', 0.00017),
 'tau-proton mass ratio': (1.89372, '', 0.00017),
 'triton g factor': (5.957924896, '', 7.6e-08),
 'triton mag. mom.': (1.504609447e-26, 'J T^-1', 3.8e-34),
 'triton mag. mom. to Bohr magneton ratio': (0.001622393657, '', 2.1e-11),
 'triton mag. mom. to nuclear magneton ratio': (2.978962448, '', 3.8e-08),
 'triton mass': (5.0073563e-27, 'kg', 2.2e-34),
 'triton mass energy equivalent': (4.50038741e-10, 'J', 2e-17),
 'triton mass energy equivalent in MeV': (2808.921005, 'MeV', 6.2e-05),
 'triton mass in u': (3.0155007134, 'u', 2.5e-09),
 'triton molar mass': (0.0030155007134, 'kg mol^-1', 2.5e-12),
 'triton-electron mag. mom. ratio': (-0.001620514423, '', 2.1e-11),
 'triton-electron mass ratio': (5496.9215267, '', 5e-06),
 'triton-neutron mag. mom. ratio': (-1.55718553, '', 3.7e-07),
 'triton-proton mag. mom. ratio': (1.066639908, '', 1e-08),
 'triton-proton mass ratio': (2.9937170308, '', 2.5e-09),
 'unified atomic mass unit': (1.660538921e-27, 'kg', 7.3e-35),
 'von Klitzing constant': (25812.8074434, 'ohm', 8.4e-06),
 'weak mixing angle': (0.2223, '', 0.0021),
 '{220} lattice spacing of silicon': (1.920155714e-10, 'm', 3.2e-18)}
In [36]:
# Beliebige Funktionen fitten

from scipy.optimize import curve_fit

x, y = np.loadtxt('example_data.txt', unpack=True)

def f(x, a, b):
    return a * x + b

params, covariance = curve_fit(f, x, y)
# covariance ist die Kovarianzmatrix

errors = np.sqrt(np.diag(covariance))

print('a =', params[0], '+/-', errors[0])
print('b =', params[1], '+/-', errors[1])
a = 4.97742634445 +/- 0.0214389925816
b = 0.0700845140769 +/- 0.123809052985