Browse Source

updates

master
Joerg Marks 2 years ago
parent
commit
530e313b90
  1. 284
      notebooks/02_fit_exp_fit_iMinuit.ipynb
  2. 126
      notebooks/02_fit_histFit.ipynb
  3. 147
      notebooks/02_fit_scikitFit.ipynb
  4. 148
      notebooks/03_ml_basics_activation.ipynb
  5. 0
      notebooks/03_ml_basics_ex_2_display_Cifar10_Greyscale.ipynb
  6. 493
      notebooks/03_ml_basics_simple_neural_network.ipynb
  7. 0
      notebooks/03_ml_basics_simple_neural_network_exercise_solution.ipynb

284
notebooks/02_fit_exp_fit_iMinuit.ipynb

@ -0,0 +1,284 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example fit for the usage of iminuit"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"plt.rcParams[\"font.size\"] = 20\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Data "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.array([0.2,0.4,0.6,0.8,1.,1.2,1.4,1.6,1.8,2.,2.2,2.4,2.6,2.8,3.,3.2,3.4,3.6,3.8,4.],dtype='d')\n",
"dy = np.array([0.04,0.021,0.035,0.03,0.029,0.019,0.024,0.018,0.019,0.022,0.02,0.025,0.018,0.024,0.019,0.021,0.03,0.019,0.03,0.024 ], dtype='d')\n",
"y = np.array([1.792,1.695,1.541,1.514,1.427,1.399,1.388,1.270,1.262,1.228,1.189,1.182,1.121,1.129,1.124,1.089,1.092,1.084,1.058,1.057 ], dtype='d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define fit functions -an exponential"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def xp(a, b , c):\n",
" return a * np.exp(b*x) + c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"least-squares function: sum of data residuals squared"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def LS(a,b,c):\n",
" return np.sum((y - xp(a,b,c)) ** 2 / dy ** 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"import Minuit object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from iminuit import Minuit"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Minuit instance using LS function to minimize"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"LS.errordef = Minuit.LEAST_SQUARES\n",
"m = Minuit(LS, a=0.9, b=-0.7 , c=0.95)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run migrad , parameter c is now fixed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.migrad()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"release fix on \"c\" and minimize again"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.fixed[\"c\"] = False\n",
"m.migrad()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get covariance information"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.hesse()\n",
"m.params\n",
"m.covariance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copy covariance information to numpy arrays"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get a 2D contour of the function around the minimum for 2 parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.minos()\n",
"print (m.merrors['a']) # Print control information of parameter a\n",
"m.draw_profile('b', subtract_min=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Minos algorithm uses the profile likelihood method to compute (generally asymmetric) confidence intervals. This can be plotted"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.draw_mncontour('a', 'b')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access fit results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(m.values,m.errors)\n",
"a_fit = m.values[\"a\"]\n",
"b_fit = m.values[\"b\"]\n",
"c_fit = m.values[\"c\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Prepare data to display fitted function "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_plot = np.linspace( 0.1, 4.5 , 100 )\n",
"y_fit = a_fit * np.exp(b_fit*x_plot) + c_fit "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"plot data and fit results with matplotlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure()\n",
"plt.errorbar(x, y, dy , fmt=\"o\")\n",
"plt.plot(x_plot, y_fit)\n",
"plt.title(\"iminuit exponential Fit\")\n",
"plt.xlim(-0.1, 4.1)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

126
notebooks/02_fit_histFit.ipynb

@ -0,0 +1,126 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Test Histogramm Fitting in pyROOT"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math\n",
"#import ROOT\n",
"from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1D, TH2D, TF1 \n",
"from ROOT import gROOT, gBenchmark, gRandom, gSystem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Read data previously used from text file and put it in a numpy array"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.genfromtxt('D0Mass.txt', dtype='d')\n",
"print(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create histogramm and draw"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d0 = TH1D( 'd0', 'D0 Mass ', 200, 1700. , 2000. )\n",
"for x in data :\n",
" d0.Fill(x)\n",
"c = TCanvas( 'myC', 'Dynamic Filling Example',700, 500 )\n",
"d0.Draw()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To display the notebook draw the canvas."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %jsroot on\n",
"c.Draw()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"perform Fit"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"func = TF1(\"func\", 'gaus', 1840. , 1880.)\n",
"myfit = d0.Fit(func,\"S\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

147
notebooks/02_fit_scikitFit.ipynb

@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fit 3rd order Polynomial to graph data using scikit-learn, more infos\n",
"https://www.datatechnotes.com/2018/06/polynomial-regression-fitting-in-python.html"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"plt.rcParams[\"font.size\"] = 20\n",
"\n",
"import numpy as np\n",
"\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.preprocessing import PolynomialFeatures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='d')\n",
"dx = np.array([0.1,0.1,0.5,0.1,0.5,0.1,0.5,0.1,0.5,0.1], dtype='d')\n",
"y = np.array([1.1 ,2.3 ,2.7 ,3.2 ,3.1 ,2.4 ,1.7 ,1.5 ,1.5 ,1.7 ], dtype='d')\n",
"dy = np.array([0.15,0.22,0.29,0.39,0.31,0.21,0.13,0.15,0.19,0.13], dtype='d')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" building polynomial model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"polyModel = PolynomialFeatures(degree = 3)\n",
"xpol = polyModel.fit_transform(x.reshape(-1, 1))\n",
"preg = polyModel.fit(xpol,y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Building linear model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"linearModel = LinearRegression(fit_intercept = True)\n",
"linearModel.fit(xpol, y[:, np.newaxis])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plotting\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_plot = np.linspace(0.1,10.1,200)\n",
"polyfit = linearModel.predict(preg.fit_transform(x_plot.reshape(-1, 1)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" plot data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure()\n",
"plt.errorbar(x, y, dy , dx, fmt=\"o\")\n",
"plt.plot(x_plot, polyfit )\n",
"plt.title(\"scikit-learn Fit Test\")\n",
"plt.xlim(-0.1, 10.1)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

148
notebooks/03_ml_basics_activation.ipynb

@ -0,0 +1,148 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d0ce4228",
"metadata": {},
"source": [
"# plot activation functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3c8093a",
"metadata": {},
"outputs": [],
"source": [
"# Importing the required libraries\n",
"import math\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a134fb45",
"metadata": {},
"outputs": [],
"source": [
"# The definition of activation functions mathematically\n",
"# Sigmoid Function\n",
"def sigmoid(x):\n",
" a = []\n",
" for i in x:\n",
" a.append(1/(1+math.exp(-i)))\n",
" return a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "954c32ed",
"metadata": {},
"outputs": [],
"source": [
"# Hyperbolic Tanjant Function\n",
"def tanh(x, derivative=False):\n",
" if (derivative == True):\n",
" return (1 - (x ** 2))\n",
" return np.tanh(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c0e5136",
"metadata": {},
"outputs": [],
"source": [
"# ReLU Function\n",
"def re(x):\n",
" b = []\n",
" for i in x:\n",
" if i<0:\n",
" b.append(0)\n",
" else:\n",
" b.append(i)\n",
" return b"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f29cac64",
"metadata": {},
"outputs": [],
"source": [
"# Leaky ReLU Function\n",
"def lr(x):\n",
" b = []\n",
" for i in x:\n",
" if i<0:\n",
" b.append(i/10)\n",
" else:\n",
" b.append(i)\n",
" return b\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1854dc7b",
"metadata": {},
"outputs": [],
"source": [
"# Determining the intervals to be created for the graph\n",
"x = np.arange(-3., 3., 0.1)\n",
"sig = sigmoid(x)\n",
"tanh = tanh(x)\n",
"relu = re(x)\n",
"leaky_relu = lr(x)\n",
"swish = sig*x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c535ccb",
"metadata": {},
"outputs": [],
"source": [
"# Displaying the functions\n",
"line_1, = plt.plot(x,sig, label='Sigmoid')\n",
"line_2, = plt.plot(x,tanh, label='Tanh')\n",
"line_3, = plt.plot(x,relu, label='ReLU')\n",
"line_4, = plt.plot(x,leaky_relu, label='Leaky ReLU')\n",
"line_5, = plt.plot(x,swish, label='Swish')\n",
"plt.legend(handles=[line_1, line_2, line_3, line_4, line_5])\n",
"plt.axhline(y=0, color='k')\n",
"plt.axvline(x=0, color='k')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0
notebooks/03_ml_basics_ex_1_display_Cifar10_Greyscale.ipynb → notebooks/03_ml_basics_ex_2_display_Cifar10_Greyscale.ipynb

493
notebooks/03_ml_basics_simple_neural_network.ipynb
File diff suppressed because one or more lines are too long
View File

0
notebooks/simple_neural_network_exercise_solution.ipynb → notebooks/03_ml_basics_simple_neural_network_exercise_solution.ipynb

Loading…
Cancel
Save