Skip to content

Commit

Permalink
ueapy#5 dtype usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sharma-satyam697 committed Jun 23, 2023
1 parent f4da573 commit 81f4454
Showing 1 changed file with 142 additions and 20 deletions.
162 changes: 142 additions & 20 deletions notebooks/solutions/13-Numpy-Intro-solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -36,9 +36,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, ..., 86398, 86399, 86400])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# a)\n",
"np.arange(0,86401)\n",
Expand All @@ -48,9 +59,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 3600, 7200, 10800, 14400, 18000, 21600, 25200, 28800,\n",
" 32400, 36000, 39600, 43200, 46800, 50400, 54000, 57600, 61200,\n",
" 64800, 68400, 72000, 75600, 79200, 82800, 86400])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# b)\n",
"np.arange(0,86401,60) # minutes\n",
Expand Down Expand Up @@ -90,9 +114,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2. 3.2 5.5 -6.4 -2.2 2.4]\n",
" [ 1. 22. 4. 0.1 5.3 -9. ]\n",
" [-1. 18. 6.2 1.3 7. -5. ]\n",
" [ 3. 1. 2.1 21. 1.1 -2. ]]\n"
]
}
],
"source": [
"# Solution\n",
"a=np.array([[2, 3.2, 5.5, -6.4, -2.2, 2.4],\n",
Expand All @@ -112,29 +147,64 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([-6.4, 0.1, 1.3, 21. ])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[:, 3]\n",
"# This is the 4th column of a (recall that we start indexing with 0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1. , 22. , 4. , 0.1],\n",
" [-1. , 18. , 6.2, 1.3],\n",
" [ 3. , 1. , 2.1, 21. ]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[1:4, 0:4]\n",
"# rows 1 to 4 (second to last) and columns first to fourth"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([4. , 6.2, 2.1])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a[1:, 2]\n",
"# All rows starting from the second, from the third column"
Expand All @@ -149,19 +219,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([ 2.4, -9. , -5. , -2. ])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# i)\n",
"a[:,-1] # This is the last column"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([-1. , 18. , 6.2, 1.3, 7. , -5. ])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ii)\n",
"a[-2,:] # This is the row before last"
Expand Down Expand Up @@ -189,13 +281,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {
"nbpresent": {
"id": "e4b78e95-ad9a-4a6d-a54b-68d7d4a72eab"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 5, 10, 15, 20],\n",
" [25, 30, 35, 40, 45],\n",
" [50, 55, 60, 65, 70],\n",
" [75, 80, 85, 90, 95]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.arange(0, 100, 5).reshape(4, 5) \n",
"a"
Expand Down Expand Up @@ -248,6 +354,14 @@
"result"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Reason to use dype is to get the array with our specific data type. Above dtype = a.dytpe refers to the same datatype which an array 'a' holds"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -290,6 +404,14 @@
"print(result)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Why do we use dtype for creating placeholder array? (is this needed?)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -462,7 +584,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.11.1"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 81f4454

Please sign in to comment.