From 43228c2d31642932b58596c58eafab3df7e9d800 Mon Sep 17 00:00:00 2001 From: Miquel Puig Date: Tue, 17 Oct 2023 18:04:51 +0200 Subject: [PATCH 1/2] Miki_Puig --- your-code/main.py | 81 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..6d784cd 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,67 +1,102 @@ #1. Import the NUMPY package under the name np. - - +import numpy as np #2. Print the NUMPY version and the configuration. - - +print(np.version.version) #3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable "a" # Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find? +a = np.random.random(size=(2,3,5)) #4. Print a. - +print(a) #5. Create a 5x2x3 3-dimensional array with all values equaling 1. #Assign the array to variable "b" - +b = np.ones((5, 2, 3)) #6. Print b. - +print(b) #7. Do a and b have the same size? How do you prove that in Python code? +print(a.size) +print(b.size) - +"""a and b have the same size, 30""" #8. Are you able to add a and b? Why or why not? +a + b +""" We can't add a and b because they don't have the same shape/structure""" #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". +a.shape +b.shape +c = b.transpose(1, 2, 0) -#10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +c.shape +#10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +print (c) +d = a + c +print(d) +'''It works because now the matrix a and the new matrix created +from transposing b have the same shape/structure''' #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. +print(a) +print(d) +''' Altought they have the same size ,30, the values innit are +not the same, at the end a it hasn't changed from the begining, +and the new variable d is the same as b but transposed, that where all values 1 plus +the values from a. d is a but adding 1 to every value +''' #12. Multiply a and c. Assign the result to e. +e = a * c +print(e) #13. Does e equal to a? Why or why not? +e == a +'''--> #True''' +''' As b was a matrix al made by 1, multipling it by the matrix a doesn't change the value of a +making the result matrix e, equal to a''' #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_max = np.max(d) +print(d_max) +d_min = np.min(d) +print(d_min) +d_mean = np.mean(d) +print(d_mean) #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`. +f = np.empty((2, 3, 5)) +print(f) +print(d) + @@ -76,6 +111,14 @@ """ +f[(d_min < d) & (d_mean > d)] = 25 +f[(d > d_mean) & (d < d_max)] = 75 +f[d_mean == d] = 50 +f[(d_min == d)] = 0 +f[(d_max == d)] = 100 + +print(f) + """ @@ -98,11 +141,14 @@ [ 75., 75., 75., 75., 75.], [ 25., 75., 0., 75., 75.]]]) """ - +print(d) +print(f) """ -#18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values -("A", "B", "C", "D", and "E") to label the array elements? You are expecting the result to be: +#18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), +how to use string values +("A", "B", "C", "D", and "E") to label the array elements? +You are expecting the result to be: array([[[ 'D', 'D', 'D', 'B', 'D'], [ 'D', 'D', 'B', 'B', 'B'], [ 'D', 'B', 'D', 'D', 'D']], @@ -111,4 +157,13 @@ [ 'D', 'D', 'D', 'D', 'D'], [ 'B', 'D', 'A', 'D', 'D']]]) Again, you don't need Numpy in this question. -""" \ No newline at end of file +""" + + +new_array = np.empty((2,3,5), dtype=str) + +new_array[(d_min < d) & (d_mean > d)] = 'A' +new_array[(d > d_mean) & (d < d_max)] = 'B' +new_array[d_mean == d] = 'C' +new_array[(d_min == d)] = 'D' +new_array[(d_max == d)] = 'E' \ No newline at end of file From 79dc1d174cf33ee75bc2fb34edea9a3d68003099 Mon Sep 17 00:00:00 2001 From: Miquel Puig Date: Tue, 17 Oct 2023 20:25:34 +0200 Subject: [PATCH 2/2] lab_numpy_clean_Miki_Puig --- your-code/main.py | 51 +++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 6d784cd..3bb4891 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -28,29 +28,41 @@ print(a.size) print(b.size) -"""a and b have the same size, 30""" +'''a and b have the same size, 30''' + +''' +In order to see if the sizes of the 2 arrays are the same we are going to compare them, +after running it it shows us True, so we can say that both have the same size +''' + +a.size == b.size + + #8. Are you able to add a and b? Why or why not? a + b -""" We can't add a and b because they don't have the same shape/structure""" +a.shape +b.shape + +'''We can't add a and b because they don't have the same shape/structure''' #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". -a.shape -b.shape c = b.transpose(1, 2, 0) c.shape +print (c) #10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? -print (c) + + d = a + c print(d) -'''It works because now the matrix a and the new matrix created +''' It works because now the matrix a and the new matrix created from transposing b have the same shape/structure''' #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. @@ -60,24 +72,24 @@ ''' Altought they have the same size ,30, the values innit are -not the same, at the end a it hasn't changed from the begining, -and the new variable d is the same as b but transposed, that where all values 1 plus -the values from a. d is a but adding 1 to every value +not the same. Array (a) hasn't changed from the begining, +and the new array (d) is the same as (b) but transposed plus array (a). ''' #12. Multiply a and c. Assign the result to e. e = a * c + print(e) #13. Does e equal to a? Why or why not? e == a -'''--> #True''' +''' True ''' -''' As b was a matrix al made by 1, multipling it by the matrix a doesn't change the value of a -making the result matrix e, equal to a''' +''' As (b) was a array made by values = 1, multipling it by the array (a) it does not change the value from (a), +making the result of the array (e), equal to (a)''' #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" @@ -100,15 +112,17 @@ -""" + #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. + +''' If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f. If a value equals to d_mean, assign 50 to the corresponding value in f. Assign 0 to the corresponding value(s) in f for d_min in d. Assign 100 to the corresponding value(s) in f for d_max in d. In the end, f should have only the following values: 0, 25, 50, 75, and 100. Note: you don't have to use Numpy in this question. -""" +''' f[(d_min < d) & (d_mean > d)] = 25 @@ -121,8 +135,9 @@ -""" #17. Print d and f. Do you have your expected f? + +""" For instance, if your d is: array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931], [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981], @@ -162,8 +177,10 @@ new_array = np.empty((2,3,5), dtype=str) -new_array[(d_min < d) & (d_mean > d)] = 'A' +new_array[(d_min < d) & (d < d_mean)] = 'A' new_array[(d > d_mean) & (d < d_max)] = 'B' new_array[d_mean == d] = 'C' new_array[(d_min == d)] = 'D' -new_array[(d_max == d)] = 'E' \ No newline at end of file +new_array[(d_max == d)] = 'E' + +print(new_array) \ No newline at end of file