diff --git a/your-code/main.pynb_completed.ipynb b/your-code/main.pynb_completed.ipynb new file mode 100644 index 0000000..9af8dd0 --- /dev/null +++ b/your-code/main.pynb_completed.ipynb @@ -0,0 +1,456 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#1. Import the NUMPY package under the name np.\n", + "\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.26.0\n" + ] + } + ], + "source": [ + "#2. Print the NUMPY version and the configuration.\n", + "\n", + "print(np.version.version)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "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", + "\n", + "a = np.random.random((2,3,5))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.7526861 , 0.88161134, 0.040113 , 0.9682027 , 0.22825111],\n", + " [0.60061964, 0.09773518, 0.34704848, 0.13854004, 0.9781127 ],\n", + " [0.59388505, 0.05230312, 0.07939614, 0.50513025, 0.30175203]],\n", + "\n", + " [[0.5566256 , 0.9956584 , 0.61252727, 0.85991003, 0.30842392],\n", + " [0.36075712, 0.23135583, 0.52093942, 0.3223463 , 0.12806055],\n", + " [0.70516468, 0.44155237, 0.55611476, 0.65653224, 0.82274355]]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#4. Print a.\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n", + "#Assign the array to variable \"b\"\n", + "b = np.ones((5, 2, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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.]]])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#6. Print b.\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#7. Do a and b have the same size? How do you prove that in Python code?\n", + "a.size == b.size" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "operands could not be broadcast together with shapes (2,3,5) (5,2,3)\n" + ] + } + ], + "source": [ + "#8. Are you able to add a and b? Why or why not? ValueError\n", + "\n", + "try:\n", + " a + b\n", + "except ValueError:\n", + " print(\"operands could not be broadcast together with shapes (2,3,5) (5,2,3)\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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.]]])" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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", + "\n", + "c = b.transpose(1, 2, 0)\n", + "\n", + "c" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "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 = a + c\n", + "\n", + "#its is now posiible because they now have the same shape" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.7526861 0.88161134 0.040113 0.9682027 0.22825111]\n", + " [0.60061964 0.09773518 0.34704848 0.13854004 0.9781127 ]\n", + " [0.59388505 0.05230312 0.07939614 0.50513025 0.30175203]]\n", + "\n", + " [[0.5566256 0.9956584 0.61252727 0.85991003 0.30842392]\n", + " [0.36075712 0.23135583 0.52093942 0.3223463 0.12806055]\n", + " [0.70516468 0.44155237 0.55611476 0.65653224 0.82274355]]]\n", + "[[[1.7526861 1.88161134 1.040113 1.9682027 1.22825111]\n", + " [1.60061964 1.09773518 1.34704848 1.13854004 1.9781127 ]\n", + " [1.59388505 1.05230312 1.07939614 1.50513025 1.30175203]]\n", + "\n", + " [[1.5566256 1.9956584 1.61252727 1.85991003 1.30842392]\n", + " [1.36075712 1.23135583 1.52093942 1.3223463 1.12806055]\n", + " [1.70516468 1.44155237 1.55611476 1.65653224 1.82274355]]]\n" + ] + } + ], + "source": [ + "\n", + "#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.\n", + "\n", + "print(a)\n", + "\n", + "print(d)\n", + "\n", + "#d is just equal to a + d as every value in a has been added by 1" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.7526861 , 0.88161134, 0.040113 , 0.9682027 , 0.22825111],\n", + " [0.60061964, 0.09773518, 0.34704848, 0.13854004, 0.9781127 ],\n", + " [0.59388505, 0.05230312, 0.07939614, 0.50513025, 0.30175203]],\n", + "\n", + " [[0.5566256 , 0.9956584 , 0.61252727, 0.85991003, 0.30842392],\n", + " [0.36075712, 0.23135583, 0.52093942, 0.3223463 , 0.12806055],\n", + " [0.70516468, 0.44155237, 0.55611476, 0.65653224, 0.82274355]]])" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#12. Multiply a and c. Assign the result to e.\n", + "\n", + "e = a * c\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]],\n", + "\n", + " [[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]]])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#13. Does e equal to a? Why or why not?\n", + "e == a\n", + "\n", + "#yes they are equal because 'e' is 'a' multiplied by one" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The max value of d is 1.9956584011061855\n", + "The min value of d is 1.0401129984264466\n", + "The mean value of d is 1.4881366300353638\n" + ] + } + ], + "source": [ + "#14. Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\"\n", + "\n", + "d_max = np.max(d)\n", + "\n", + "d_min = np.min(d)\n", + "\n", + "d_mean = np.mean(d)\n", + "\n", + "print(\"The max value of d is\", d_max)\n", + "print(\"The min value of d is\", d_min)\n", + "print(\"The mean value of d is\", d_mean)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "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))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#16. Populate the values in f. For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.\n", + "If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.\n", + "If a value equals to d_mean, assign 50 to the corresponding value in f.\n", + "Assign 0 to the corresponding value(s) in f for d_min in d.\n", + "Assign 100 to the corresponding value(s) in f for d_max in d.\n", + "In the end, f should have only the following values: 0, 25, 50, 75, and 100.\n", + "Note: you don't have to use Numpy in this question.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(len(d)):\n", + " for j in range(len(d[i])):\n", + " for k in range(len(d[i][j])):\n", + " if d_min < d[i][j][k] and d_mean> d[i][j][k]:\n", + " f[i][j][k] = 25\n", + " elif d_mean < d[i][j][k] and d_max > d[i][j][k]:\n", + " f[i][j][k] = 75\n", + " elif d_mean == d[i][j][k]:\n", + " f[i][j][k] = 50\n", + " elif d_min == d[i][j][k]:\n", + " f[i][j][k] = 0\n", + " elif d_max == d[i][j][k]:\n", + " f[i][j][k] = 100\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#17. Print d and f. Do you have your expected f?\n", + "For instance, if your d is:\n", + "array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931],\n", + " [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981],\n", + " [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]],\n", + "\n", + " [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381],\n", + " [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881],\n", + " [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]])\n", + "\n", + "Your f should be:\n", + "array([[[ 75., 75., 75., 25., 75.],\n", + " [ 75., 75., 25., 25., 25.],\n", + " [ 75., 25., 75., 75., 75.]],\n", + "\n", + " [[ 25., 25., 25., 25., 100.],\n", + " [ 75., 75., 75., 75., 75.],\n", + " [ 25., 75., 0., 75., 75.]]])\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.7526861 1.88161134 1.040113 1.9682027 1.22825111]\n", + " [1.60061964 1.09773518 1.34704848 1.13854004 1.9781127 ]\n", + " [1.59388505 1.05230312 1.07939614 1.50513025 1.30175203]]\n", + "\n", + " [[1.5566256 1.9956584 1.61252727 1.85991003 1.30842392]\n", + " [1.36075712 1.23135583 1.52093942 1.3223463 1.12806055]\n", + " [1.70516468 1.44155237 1.55611476 1.65653224 1.82274355]]]\n", + "[[[ 75. 75. 0. 75. 25.]\n", + " [ 75. 25. 25. 25. 75.]\n", + " [ 75. 25. 25. 75. 25.]]\n", + "\n", + " [[ 75. 100. 75. 75. 25.]\n", + " [ 25. 25. 75. 25. 25.]\n", + " [ 75. 25. 75. 75. 75.]]]\n" + ] + } + ], + "source": [ + "print(d)\n", + "\n", + "print(f)\n", + "\n", + "#yes f is as expecteed as the value have changed correctly" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "ironhack", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}