Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
HSLU deep Learning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Remo Kälin
HSLU deep Learning
Commits
48a2f685
Commit
48a2f685
authored
2 years ago
by
Mirko Birbaumer
Browse files
Options
Downloads
Patches
Plain Diff
Adapted Numpy Pandas Intro Notebook
parent
bd4db564
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb
+520
-0
520 additions, 0 deletions
.../Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb
with
520 additions
and
0 deletions
notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb
0 → 100644
+
520
−
0
View file @
48a2f685
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 Funktions, Conditionals, and Iteration in Python\n",
"\n",
"Let us create a Python function, and call it from a loop."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def HelloWorldXY(x, y):\n",
" if (x < 10):\n",
" print(\"Hello World, x was < 10\")\n",
" elif (x < 20):\n",
" print(\"Hello World, x was >= 10 but < 20\")\n",
" else:\n",
" print(\"Hello World, x was >= 20\")\n",
" return x + y\n",
"\n",
"print(HelloWorldXY(1,2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us call the function `HelloWorldXY()` from a loop:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Now running with i: 8\n"
]
},
{
"ename": "NameError",
"evalue": "name 'HelloWorldXY' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-7ea2350df544>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m25\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# i=8, 13, 18, 23 (start, stop, step)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\n--- Now running with i: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHelloWorldXY\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Result from HelloWorld: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'HelloWorldXY' is not defined"
]
}
],
"source": [
"for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)\n",
" print(\"\\n--- Now running with i: {}\".format(i))\n",
" r = HelloWorldXY(i,i)\n",
" print(\"Result from HelloWorld: {}\".format(r))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want a loop starting at 0 to 2 (exclusive) you could do any of the following:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iterate over the items. `range(2)` is like a list [0,1].\n",
"0\n",
"1\n",
"Iterate over an actual list.\n",
"0\n",
"1\n",
"While works\n",
"0\n",
"1\n",
"Python supports standard key words like continue and break\n",
"Entered while\n",
"while broken\n"
]
}
],
"source": [
"print(\"Iterate over the items. `range(2)` is like a list [0,1].\")\n",
"for i in range(2):\n",
" print(i)\n",
"\n",
"print(\"Iterate over an actual list.\")\n",
"for i in [0,1]:\n",
" print(i)\n",
"\n",
"print(\"While works\")\n",
"i = 0\n",
"while i < 2:\n",
" print(i)\n",
" i += 1\n",
" \n",
"print(\"Python supports standard key words like continue and break\")\n",
"while True:\n",
" print(\"Entered while\")\n",
" break\n",
"print(\"while broken\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.3 Data in Numpy"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape scaler () \n",
"Shape vector (3,) \n",
"Shape matrix (3, 3) \n",
"Shape tensor (3, 3, 2, 1)\n",
"Type scalar or array <class 'numpy.ndarray'> \n",
"Type after addition with integer <class 'numpy.int64'>\n",
"v[1:] = [ 2 10] \n",
"m[1:][2:] = \n",
" [[5 6]\n",
" [8 9]]\n",
"[ 1 2 10] [[ 1 2 10]] [[ 1 2 10]]\n",
"(3,) (1, 3) (1, 3)\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# Scalar\n",
"s = np.array(5)\n",
"# Vector\n",
"v = np.array([1, 2, 10])\n",
"# Matrix\n",
"m = np.array([[1,2,3], \n",
" [4,5,6], \n",
" [7,8,9]])\n",
"# Tensor:\n",
"t = np.array([[[[1],[2]], [[3],[4]], [[5],[6]]],\n",
" [[[7],[8]], [[9],[10]], [[11],[12]]],\n",
" [[[13],[14]], [[15],[16]], [[17],[17]]]])\n",
"\n",
"# Shape\n",
"print(\"Shape scaler\", s.shape, \"\\nShape vector\", v.shape, \"\\nShape matrix\", m.shape, \"\\nShape tensor\", t.shape)\n",
"\n",
"# Type\n",
"print(\"Type scalar or array\", type(s), \"\\nType after addition with integer\", type(s + 3))\n",
"\n",
"# Slicing\n",
"print(\"v[1:] = \", v[1:], \"\\nm[1:][2:] = \\n\", m[1:,1:])\n",
"\n",
"# Reshape arrays\n",
"x = v.reshape(1, 3)\n",
"y = v[None, :]\n",
"print(v, x, y)\n",
"print(v.shape, x.shape, y.shape)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.4 Element-wise Operations"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6, 7, 8, 9, 10]\n",
"[ 6 7 8 9 10]\n",
"[30 35 40 45 50] \n",
" [30 35 40 45 50] \n",
"\n",
"a =\n",
" [[1 3]\n",
" [5 7]] \n",
"b =\n",
" [[2 4]\n",
" [6 8]]\n",
"a + b =\n",
" [[ 3 7]\n",
" [11 15]]\n",
"a * b =\n",
" [[ 2 12]\n",
" [30 56]]\n"
]
},
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (2,2) (5,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-7ddd6b5f4e75>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a * b =\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;31m# Shape mismatch:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a * values =\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,2) (5,) "
]
}
],
"source": [
"# The Python way:\n",
"values = [1, 2, 3, 4, 5]\n",
"for i in range(len(values)):\n",
" values[i] += 5\n",
" \n",
"print(values)\n",
"\n",
"# The Numpy way:\n",
"values = np.array([1, 2, 3, 4, 5])\n",
"values += 5\n",
"\n",
"print(values)\n",
"\n",
"# Multiplication\n",
"x = np.multiply(values, 5)\n",
"y = values * 5\n",
"print(x, \"\\n\", y, \"\\n\")\n",
"\n",
"# Element wise matrix operations\n",
"a = np.array([[1,3],[5,7]])\n",
"b = np.array([[2,4],[6,8]])\n",
"print(\"a =\\n\", a, \"\\nb =\\n\", b)\n",
"print(\"a + b =\\n\", a + b)\n",
"print(\"a * b =\\n\", a * b)\n",
"# Shape mismatch:\n",
"print(\"a * values =\\n\", a * values)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Numpy Matrix Multiplication\n",
"Recap element-wise multiplication:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"m =\n",
" [[1 2 3]\n",
" [4 5 6]] \n",
"n =\n",
" [[0.25 0.5 0.75]\n",
" [1. 1.25 1.5 ]]\n",
"x =\n",
" [[0.25 1. 2.25]\n",
" [4. 6.25 9. ]] \n",
"y =\n",
" [[0.25 1. 2.25]\n",
" [4. 6.25 9. ]]\n"
]
}
],
"source": [
"# Elementwise recap:\n",
"m = np.array([[1,2,3],[4,5,6]])\n",
"# Scalar multiplication\n",
"n = m * 0.25\n",
"# Python Elementwise matrix multiplication\n",
"x = m * n\n",
"# Numpy Elementwise matrix multiplication\n",
"y = np.multiply(m, n)\n",
"\n",
"print(\"m =\\n\", m, \"\\nn =\\n\", n)\n",
"print(\"x =\\n\", x, \"\\ny =\\n\", y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Matrix Product:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a =\n",
" [[1 2 3 4]\n",
" [5 6 7 8]] \n",
"a.shape =\n",
" (2, 4) \n",
"b =\n",
" [[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 9]\n",
" [10 11 12]] \n",
"b.shape =\n",
" (4, 3)\n",
"c = \n",
" [[ 70 80 90]\n",
" [158 184 210]] \n",
"c.shape =\n",
" (2, 3)\n",
"d = \n",
" [[ 70 80 90]\n",
" [158 184 210]] \n",
"d.shape =\n",
" (2, 3)\n"
]
}
],
"source": [
"\"\"\" Using np.matmul \"\"\"\n",
"a = np.array([[1,2,3,4],[5,6,7,8]])\n",
"b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n",
"\n",
"print(\"a =\\n\", a, \"\\na.shape =\\n\", a.shape, \"\\nb =\\n\", b, \"\\nb.shape =\\n\", b.shape)\n",
"\n",
"# Matrix product\n",
"c = np.matmul(a, b)\n",
"print(\"c = \\n\", c, \"\\nc.shape =\\n\", c.shape)\n",
"\n",
"# Dimension mismatch:\n",
"# print(np.matmul(b, a))\n",
"\"\"\" Using np.dot \"\"\"\n",
"d = np.dot(a, b)\n",
"print(\"d = \\n\", d, \"\\nd.shape =\\n\", d.shape)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpose"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"m = \n",
" [[ 1 2 3 4]\n",
" [ 5 6 7 8]\n",
" [ 9 10 11 12]] \n",
"m.T = \n",
" [[ 1 5 9]\n",
" [ 2 6 10]\n",
" [ 3 7 11]\n",
" [ 4 8 12]]\n",
"m = \n",
" [[ 1 2 3 4]\n",
" [ 5 6 7 200]\n",
" [ 9 10 11 12]] \n",
"m_t = \n",
" [[ 1 5 9]\n",
" [ 2 6 10]\n",
" [ 3 7 11]\n",
" [ 4 200 12]]\n",
"entries [3][1], [1][3], respectively are edited in both matrices\n"
]
}
],
"source": [
"m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n",
"print(\"m = \\n\", m,\"\\nm.T = \\n\", m.T)\n",
"\n",
"# note how the transposed matrix is not a copy of the original:\n",
"m_t = m.T\n",
"m_t[3][1] = 200\n",
"print(\"m = \\n\", m, \"\\nm_t = \\n\", m_t)\n",
"print(\"entries [3][1], [1][3], respectively are edited in both matrices\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A real use case"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.27 0.45 0.64 0.31]] (1, 4)\n",
"[[ 0.02 0.001 -0.03 0.036]\n",
" [ 0.04 -0.003 0.025 0.009]\n",
" [ 0.012 -0.045 0.28 -0.067]] (3, 4)\n",
"Matrix multiplication gives:\n",
" [[-0.01299 0.00664 0.13494]] \n",
"or, equivalently:\n",
" [[-0.01299]\n",
" [ 0.00664]\n",
" [ 0.13494]]\n"
]
}
],
"source": [
"inputs = np.array([[-0.27, 0.45, 0.64, 0.31]])\n",
"print(inputs, inputs.shape)\n",
"\n",
"weights = np.array([[0.02, 0.001, -0.03, 0.036], \n",
" [0.04, -0.003, 0.025, 0.009], \n",
" [0.012, -0.045, 0.28, -0.067]])\n",
"print(weights, weights.shape)\n",
"\n",
"print(\"Matrix multiplication gives:\\n\", np.matmul(inputs, weights.T), \"\\nor, equivalently:\\n\", np.matmul(weights, inputs.T))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some more useful Numpy methods"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Showing some basic math on arrays\n",
"Max: 4\n",
"Average: 2.0\n",
"Max index: 2\n",
"\n",
"Use numpy to create a [3,3] dimension array with random number\n",
"[[0.92371879 0.58999086 0.76979433]\n",
" [0.48733651 0.44698554 0.91494542]\n",
" [0.59130531 0.69632003 0.32785335]]\n"
]
}
],
"source": [
"print(\"\\nShowing some basic math on arrays\")\n",
"\n",
"b = np.array([0,1,4,3,2])\n",
"print(\"Max: {}\".format(np.max(b)))\n",
"print(\"Average: {}\".format(np.average(b)))\n",
"print(\"Max index: {}\".format(np.argmax(b)))\n",
"\n",
"print(\"\\nUse numpy to create a [3,3] dimension array with random number\")\n",
"c = np.random.rand(3, 3)\n",
"print(c)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:markdown id: tags:
## 3.2 Funktions, Conditionals, and Iteration in Python
Let us create a Python function, and call it from a loop.
%% Cell type:code id: tags:
```
python
def
HelloWorldXY
(
x
,
y
):
if
(
x
<
10
):
print
(
"
Hello World, x was < 10
"
)
elif
(
x
<
20
):
print
(
"
Hello World, x was >= 10 but < 20
"
)
else
:
print
(
"
Hello World, x was >= 20
"
)
return
x
+
y
print
(
HelloWorldXY
(
1
,
2
))
```
%% Cell type:markdown id: tags:
Now let us call the function
`HelloWorldXY()`
from a loop:
%% Cell type:code id: tags:
```
python
for
i
in
range
(
8
,
25
,
5
):
# i=8, 13, 18, 23 (start, stop, step)
print
(
"
\n
--- Now running with i: {}
"
.
format
(
i
))
r
=
HelloWorldXY
(
i
,
i
)
print
(
"
Result from HelloWorld: {}
"
.
format
(
r
))
```
%% Output
--- Now running with i: 8
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-7ea2350df544> in <module>
1 for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)
2 print("\n--- Now running with i: {}".format(i))
----> 3 r = HelloWorldXY(i,i)
4 print("Result from HelloWorld: {}".format(r))
NameError: name 'HelloWorldXY' is not defined
%% Cell type:markdown id: tags:
If you want a loop starting at 0 to 2 (exclusive) you could do any of the following:
%% Cell type:code id: tags:
```
python
print
(
"
Iterate over the items. `range(2)` is like a list [0,1].
"
)
for
i
in
range
(
2
):
print
(
i
)
print
(
"
Iterate over an actual list.
"
)
for
i
in
[
0
,
1
]:
print
(
i
)
print
(
"
While works
"
)
i
=
0
while
i
<
2
:
print
(
i
)
i
+=
1
print
(
"
Python supports standard key words like continue and break
"
)
while
True
:
print
(
"
Entered while
"
)
break
print
(
"
while broken
"
)
```
%% Output
Iterate over the items. `range(2)` is like a list [0,1].
0
1
Iterate over an actual list.
0
1
While works
0
1
Python supports standard key words like continue and break
Entered while
while broken
%% Cell type:markdown id: tags:
## 3.3 Data in Numpy
%% Cell type:code id: tags:
```
python
import
numpy
as
np
# Scalar
s
=
np
.
array
(
5
)
# Vector
v
=
np
.
array
([
1
,
2
,
10
])
# Matrix
m
=
np
.
array
([[
1
,
2
,
3
],
[
4
,
5
,
6
],
[
7
,
8
,
9
]])
# Tensor:
t
=
np
.
array
([[[[
1
],[
2
]],
[[
3
],[
4
]],
[[
5
],[
6
]]],
[[[
7
],[
8
]],
[[
9
],[
10
]],
[[
11
],[
12
]]],
[[[
13
],[
14
]],
[[
15
],[
16
]],
[[
17
],[
17
]]]])
# Shape
print
(
"
Shape scaler
"
,
s
.
shape
,
"
\n
Shape vector
"
,
v
.
shape
,
"
\n
Shape matrix
"
,
m
.
shape
,
"
\n
Shape tensor
"
,
t
.
shape
)
# Type
print
(
"
Type scalar or array
"
,
type
(
s
),
"
\n
Type after addition with integer
"
,
type
(
s
+
3
))
# Slicing
print
(
"
v[1:] =
"
,
v
[
1
:],
"
\n
m[1:][2:] =
\n
"
,
m
[
1
:,
1
:])
# Reshape arrays
x
=
v
.
reshape
(
1
,
3
)
y
=
v
[
None
,
:]
print
(
v
,
x
,
y
)
print
(
v
.
shape
,
x
.
shape
,
y
.
shape
)
```
%% Output
Shape scaler ()
Shape vector (3,)
Shape matrix (3, 3)
Shape tensor (3, 3, 2, 1)
Type scalar or array <class 'numpy.ndarray'>
Type after addition with integer <class 'numpy.int64'>
v[1:] = [ 2 10]
m[1:][2:] =
[[5 6]
[8 9]]
[ 1 2 10] [[ 1 2 10]] [[ 1 2 10]]
(3,) (1, 3) (1, 3)
%% Cell type:markdown id: tags:
## 3.4 Element-wise Operations
%% Cell type:code id: tags:
```
python
# The Python way:
values
=
[
1
,
2
,
3
,
4
,
5
]
for
i
in
range
(
len
(
values
)):
values
[
i
]
+=
5
print
(
values
)
# The Numpy way:
values
=
np
.
array
([
1
,
2
,
3
,
4
,
5
])
values
+=
5
print
(
values
)
# Multiplication
x
=
np
.
multiply
(
values
,
5
)
y
=
values
*
5
print
(
x
,
"
\n
"
,
y
,
"
\n
"
)
# Element wise matrix operations
a
=
np
.
array
([[
1
,
3
],[
5
,
7
]])
b
=
np
.
array
([[
2
,
4
],[
6
,
8
]])
print
(
"
a =
\n
"
,
a
,
"
\n
b =
\n
"
,
b
)
print
(
"
a + b =
\n
"
,
a
+
b
)
print
(
"
a * b =
\n
"
,
a
*
b
)
# Shape mismatch:
print
(
"
a * values =
\n
"
,
a
*
values
)
```
%% Output
[6, 7, 8, 9, 10]
[ 6 7 8 9 10]
[30 35 40 45 50]
[30 35 40 45 50]
a =
[[1 3]
[5 7]]
b =
[[2 4]
[6 8]]
a + b =
[[ 3 7]
[11 15]]
a * b =
[[ 2 12]
[30 56]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-7ddd6b5f4e75> in <module>
24 print("a * b =\n", a * b)
25 # Shape mismatch:
---> 26 print("a * values =\n", a * values)
ValueError: operands could not be broadcast together with shapes (2,2) (5,)
%% Cell type:markdown id: tags:
## Numpy Matrix Multiplication
Recap element-wise multiplication:
%% Cell type:code id: tags:
```
python
# Elementwise recap:
m
=
np
.
array
([[
1
,
2
,
3
],[
4
,
5
,
6
]])
# Scalar multiplication
n
=
m
*
0.25
# Python Elementwise matrix multiplication
x
=
m
*
n
# Numpy Elementwise matrix multiplication
y
=
np
.
multiply
(
m
,
n
)
print
(
"
m =
\n
"
,
m
,
"
\n
n =
\n
"
,
n
)
print
(
"
x =
\n
"
,
x
,
"
\n
y =
\n
"
,
y
)
```
%% Output
m =
[[1 2 3]
[4 5 6]]
n =
[[0.25 0.5 0.75]
[1. 1.25 1.5 ]]
x =
[[0.25 1. 2.25]
[4. 6.25 9. ]]
y =
[[0.25 1. 2.25]
[4. 6.25 9. ]]
%% Cell type:markdown id: tags:
Matrix Product:
%% Cell type:code id: tags:
```
python
"""
Using np.matmul
"""
a
=
np
.
array
([[
1
,
2
,
3
,
4
],[
5
,
6
,
7
,
8
]])
b
=
np
.
array
([[
1
,
2
,
3
],[
4
,
5
,
6
],[
7
,
8
,
9
],[
10
,
11
,
12
]])
print
(
"
a =
\n
"
,
a
,
"
\n
a.shape =
\n
"
,
a
.
shape
,
"
\n
b =
\n
"
,
b
,
"
\n
b.shape =
\n
"
,
b
.
shape
)
# Matrix product
c
=
np
.
matmul
(
a
,
b
)
print
(
"
c =
\n
"
,
c
,
"
\n
c.shape =
\n
"
,
c
.
shape
)
# Dimension mismatch:
# print(np.matmul(b, a))
"""
Using np.dot
"""
d
=
np
.
dot
(
a
,
b
)
print
(
"
d =
\n
"
,
d
,
"
\n
d.shape =
\n
"
,
d
.
shape
)
```
%% Output
a =
[[1 2 3 4]
[5 6 7 8]]
a.shape =
(2, 4)
b =
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
b.shape =
(4, 3)
c =
[[ 70 80 90]
[158 184 210]]
c.shape =
(2, 3)
d =
[[ 70 80 90]
[158 184 210]]
d.shape =
(2, 3)
%% Cell type:markdown id: tags:
## Transpose
%% Cell type:code id: tags:
```
python
m
=
np
.
array
([[
1
,
2
,
3
,
4
],
[
5
,
6
,
7
,
8
],
[
9
,
10
,
11
,
12
]])
print
(
"
m =
\n
"
,
m
,
"
\n
m.T =
\n
"
,
m
.
T
)
# note how the transposed matrix is not a copy of the original:
m_t
=
m
.
T
m_t
[
3
][
1
]
=
200
print
(
"
m =
\n
"
,
m
,
"
\n
m_t =
\n
"
,
m_t
)
print
(
"
entries [3][1], [1][3], respectively are edited in both matrices
"
)
```
%% Output
m =
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
m.T =
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
m =
[[ 1 2 3 4]
[ 5 6 7 200]
[ 9 10 11 12]]
m_t =
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 200 12]]
entries [3][1], [1][3], respectively are edited in both matrices
%% Cell type:markdown id: tags:
## A real use case
%% Cell type:code id: tags:
```
python
inputs
=
np
.
array
([[
-
0.27
,
0.45
,
0.64
,
0.31
]])
print
(
inputs
,
inputs
.
shape
)
weights
=
np
.
array
([[
0.02
,
0.001
,
-
0.03
,
0.036
],
[
0.04
,
-
0.003
,
0.025
,
0.009
],
[
0.012
,
-
0.045
,
0.28
,
-
0.067
]])
print
(
weights
,
weights
.
shape
)
print
(
"
Matrix multiplication gives:
\n
"
,
np
.
matmul
(
inputs
,
weights
.
T
),
"
\n
or, equivalently:
\n
"
,
np
.
matmul
(
weights
,
inputs
.
T
))
```
%% Output
[[-0.27 0.45 0.64 0.31]] (1, 4)
[[ 0.02 0.001 -0.03 0.036]
[ 0.04 -0.003 0.025 0.009]
[ 0.012 -0.045 0.28 -0.067]] (3, 4)
Matrix multiplication gives:
[[-0.01299 0.00664 0.13494]]
or, equivalently:
[[-0.01299]
[ 0.00664]
[ 0.13494]]
%% Cell type:markdown id: tags:
## Some more useful Numpy methods
%% Cell type:code id: tags:
```
python
print
(
"
\n
Showing some basic math on arrays
"
)
b
=
np
.
array
([
0
,
1
,
4
,
3
,
2
])
print
(
"
Max: {}
"
.
format
(
np
.
max
(
b
)))
print
(
"
Average: {}
"
.
format
(
np
.
average
(
b
)))
print
(
"
Max index: {}
"
.
format
(
np
.
argmax
(
b
)))
print
(
"
\n
Use numpy to create a [3,3] dimension array with random number
"
)
c
=
np
.
random
.
rand
(
3
,
3
)
print
(
c
)
```
%% Output
Showing some basic math on arrays
Max: 4
Average: 2.0
Max index: 2
Use numpy to create a [3,3] dimension array with random number
[[0.92371879 0.58999086 0.76979433]
[0.48733651 0.44698554 0.91494542]
[0.59130531 0.69632003 0.32785335]]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment