Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
353 changes: 353 additions & 0 deletions your-code/Lab_Numpy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.17.2\n"
]
}
],
"source": [
"print(np.version.version)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\"\n",
"# Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find?\n",
"a = np.random.random((2,3,5))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n",
" [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n",
" [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n",
"\n",
" [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n",
" [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n",
" [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n"
]
}
],
"source": [
"#4. Print a.\n",
"\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"#5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n",
"#Assign the array to variable \"b\"\n",
"\n",
"b = np.ones((5,2,3))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1. 1. 1.]\n",
" [1. 1. 1.]]\n",
"\n",
" [[1. 1. 1.]\n",
" [1. 1. 1.]]\n",
"\n",
" [[1. 1. 1.]\n",
" [1. 1. 1.]]\n",
"\n",
" [[1. 1. 1.]\n",
" [1. 1. 1.]]\n",
"\n",
" [[1. 1. 1.]\n",
" [1. 1. 1.]]]\n"
]
}
],
"source": [
"#6. Print b.\n",
"\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30\n",
"30\n"
]
}
],
"source": [
"#7. Do a and b have the same size? How do you prove that in Python code?\n",
"\n",
"print(a.size)\n",
"print(b.size)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"#8. Are you able to add a and b? Why or why not?\n",
"\n",
"#No se pueden agregar porque no tienen la misma forma \n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1.]]\n",
"\n",
" [[1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1.]]]\n"
]
}
],
"source": [
"#9. Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe \"c\".\n",
"\n",
"c = np.reshape(b,(2,3,5))\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1.92598104 1.40197817 1.42482632 1.66371613 1.93045929]\n",
" [1.13760188 1.03166089 1.0704651 1.57791757 1.4598114 ]\n",
" [1.1690768 1.13553113 1.90339829 1.38074646 1.19877052]]\n",
"\n",
" [[1.49544784 1.60575526 1.809597 1.38395514 1.55734081]\n",
" [1.34173021 1.80337932 1.15776063 1.02389911 1.4974305 ]\n",
" [1.26252848 1.69824184 1.87914224 1.63275067 1.58913698]]]\n"
]
}
],
"source": [
"#10. Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". But why does it work now?\n",
"\n",
"d = np.add(a,c)\n",
"print(d)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n",
" [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n",
" [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n",
"\n",
" [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n",
" [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n",
" [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n",
"[[[1.92598104 1.40197817 1.42482632 1.66371613 1.93045929]\n",
" [1.13760188 1.03166089 1.0704651 1.57791757 1.4598114 ]\n",
" [1.1690768 1.13553113 1.90339829 1.38074646 1.19877052]]\n",
"\n",
" [[1.49544784 1.60575526 1.809597 1.38395514 1.55734081]\n",
" [1.34173021 1.80337932 1.15776063 1.02389911 1.4974305 ]\n",
" [1.26252848 1.69824184 1.87914224 1.63275067 1.58913698]]]\n"
]
}
],
"source": [
"#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.\n",
"\n",
"#Los dos arreglos son iguales porque tienen la misma forma es un arreglo de 3 dimensiones con 30 elementos. \n",
"print(a)\n",
"print(d)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n",
" [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n",
" [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n",
"\n",
" [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n",
" [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n",
" [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n"
]
}
],
"source": [
"#12. Multiply a and c. Assign the result to e.\n",
"\n",
"e = np.multiply(a, c) \n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n",
" [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n",
" [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n",
"\n",
" [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n",
" [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n",
" [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n",
"[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n",
" [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n",
" [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n",
"\n",
" [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n",
" [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n",
" [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n"
]
}
],
"source": [
"#13. Does e equal to a? Why or why not?\n",
"print(a)\n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.9304592859190688\n",
"1.0238991148469805\n",
"1.4716678997998724\n"
]
}
],
"source": [
"d_max = d.max()\n",
"d_min = d.min()\n",
"d_mean = d.mean()\n",
"\n",
"print(d_max)\n",
"\n",
"print(d_min)\n",
"\n",
"print(d_mean)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#15. Now we want to label the values in d. First create an empty array \"f\" with the same shape (i.e. 2x3x5) as d using `np.empty`.\n",
"\n",
"f = np.empty([2,3,5])\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading