Skip to content

Commit

Permalink
cleaning up lesson 5
Browse files Browse the repository at this point in the history
  • Loading branch information
pmackenz committed Jul 29, 2024
1 parent f97acfd commit 366b0db
Showing 1 changed file with 17 additions and 203 deletions.
220 changes: 17 additions & 203 deletions code/jupyter/LessonNotes/Lesson 05-File IO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,175 +96,43 @@
"\n",
"INITIALIZATION:\n",
"\n",
"N = 0\n",
"sum = 0\n",
"sum2 = 0\n",
" N = 0\n",
" sum = 0\n",
" sum2 = 0\n",
"\n",
"LOOP:\n",
"\n",
" N = N + 1\n",
" N = N + 1\n",
" \n",
" $\\sum z_i$: sum[i] = sum[i-1] + zi \n",
" $\\sum z_i$: sum[i] = sum[i-1] + zi \n",
" \n",
" $\\sum (z_i)^2$: sum2[i] = sum2[i-1] + zi*zi\n",
" $\\sum (z_i)^2$: sum2[i] = sum2[i-1] + zi*zi\n",
"\n",
"AFTER LOOP:\n",
"\n",
"mean = sum / N\n",
" mean = sum / N\n",
"\n",
"sig2 = sum2 / N = mean**2\n",
" sig2 = sum2 / N = mean**2\n",
"\n",
"sig = sqrt(sig2)"
" sig = sqrt(sig2)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.795687209703e+01,1.557629314377e+03,1.191593706587e+00,1.059354827371e+00\n",
"<class 'str'>\n",
"1.795687209703e+01,1.557629314377e+03,1.191593706587e+00,1.059354827371e+00\n",
"1.795687209703e+01,1.557629314377e+03,1.191593706587e+00,1.059354827371e+00\n",
"\n"
]
}
],
"outputs": [],
"source": [
"f = open(\"exercise6_data.csv\",'r')\n",
"\n",
"for line in f:\n",
" print(line, end='')\n",
" print(type(line))\n",
" print(line*2)\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.795687209703e+01,1.557629314377e+03,1.191593706587e+00,1.059354827371e+00\n",
"['1.795687209703e+01', '1.557629314377e+03', '1.191593706587e+00', '1.059354827371e+00\\n']\n",
"[17.95687209703, 1557.629314377, 1.191593706587, 1.059354827371]\n",
"[1.79568721e+01 1.55762931e+03 1.19159371e+00 1.05935483e+00]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"\n",
"f = open(\"exercise6_data.csv\",'r')\n",
"\n",
"for line in f:\n",
" print(line, end='')\n",
" \n",
" vars = line.split(',')\n",
" \n",
" print(vars)\n",
" #print(vars[0], float(vars[0])**2)\n",
" \n",
" vals = []\n",
" for var in vars:\n",
" vals.append(float(var))\n",
" \n",
" print(vals)\n",
" \n",
" zi = np.array(vals)\n",
" \n",
" print(zi)\n",
" \n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.79568721e+01 1.55762931e+03 1.19159371e+00 1.05935483e+00]\n",
"[21.30828208 6.28045016 4.31374321 0.67786395]\n",
"[ 8.07432437 13.76647756 5.39585814 1.71447895]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
" # YOUR CODE HERE\n",
"\n",
"f = open(\"exercise6_data.csv\",'r')\n",
"\n",
"cnt = 0\n",
"\n",
"for line in f:\n",
" zi = np.array( [ float(x) for x in line.split(',') ] )\n",
" print(zi)\n",
" \n",
" cnt += 1\n",
" \n",
" if cnt > 2:\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x: mean=10.127054375946278, stdev=5.0204001371842875\n",
"y1: mean=1023.0376305650618, stdev=4289.945369140428\n",
"y2: mean=3.928646271819789, stdev=1.705542095948374\n",
"y3: mean=1.9926873807139653, stdev=2.090070459208848\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# INITIALIZATION:\n",
"\n",
"sumZ = np.zeros(4) # [ x, y1, y2, y3 ]\n",
"sumZ2 = np.zeros(4)\n",
"N = 0\n",
"\n",
"# LOOP:\n",
"\n",
"f = open(\"exercise6_data.csv\",'r')\n",
"\n",
"for line in f:\n",
" N += 1\n",
" zi = np.array( [ float(x) for x in line.split(',') ] ) # list of values for ONE ROW\n",
" sumZ += zi \n",
" sumZ2 += zi*zi \n",
"\n",
"# AFTER LOOP:\n",
"\n",
"mean = sumZ / N\n",
"sig2 = sumZ2 / N - mean**2\n",
"sig = np.sqrt(sig2)\n",
"\n",
"# OUTPUT\n",
"\n",
"for name, m, s in zip(('x', 'y1', 'y2', 'y3'), mean, sig):\n",
" print(\"{}: mean={}, stdev={}\".format(name, m, s))"
" break"
]
},
{
Expand Down Expand Up @@ -300,57 +168,10 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x: mean=10.127054375946278, stdev=5.0204001371842875\n",
"y1: mean=1023.0376305650618, stdev=4289.945369140428\n",
"y2: mean=3.928646271819789, stdev=1.705542095948374\n",
"y3: mean=1.9926873807139653, stdev=2.090070459208848\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# INITIALIZATION:\n",
"\n",
"sumZ = np.zeros(4) # [ x, y1, y2, y3 ]\n",
"sumZ2 = np.zeros(4)\n",
"N = 0\n",
"\n",
"# LOOP:\n",
"\n",
"try:\n",
" f = open(\"exercise6_data.csv\",'r')\n",
" read_ok = True\n",
"except:\n",
" print(\"File not found\")\n",
" read_ok = False\n",
" \n",
"\n",
"if read_ok:\n",
" for line in f:\n",
" N += 1\n",
" zi = np.array( [ float(x) for x in line.split(',') ] ) # list of values for ONE ROW\n",
" sumZ += zi \n",
" sumZ2 += zi**2 \n",
"\n",
" # AFTER LOOP:\n",
"\n",
" mean = sumZ / N\n",
" sig2 = sumZ2 / N - mean**2\n",
" sig = np.sqrt(sig2)\n",
"\n",
" # OUTPUT\n",
"\n",
" for name, m, s in zip(('x', 'y1', 'y2', 'y3'), mean, sig):\n",
" print(\"{}: mean={}, stdev={}\".format(name, m, s))\n"
]
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
Expand All @@ -367,13 +188,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -511,7 +325,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 366b0db

Please sign in to comment.