From 999fd3a71dc9d1c375f77c4ac3d5eb0b4786acc3 Mon Sep 17 00:00:00 2001 From: Oriol Codina Vallori Date: Tue, 17 Oct 2023 17:33:45 +0200 Subject: [PATCH] lab done --- your-code/main.py | 104 +++++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..7cd0334 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,68 +1,81 @@ #1. Import the NUMPY package under the name np. - +import numpy as np #2. Print the NUMPY version and the configuration. - - +print("2.-----------------") +print(np.__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? - - +print("3.-----------------") +a = np.random.rand(2, 3, 5) +a = np.random.randn(2, 3, 5) +a = np.random.randint(0, 10, size=(2, 3, 5)) #4. Print a. - - +print("4.-----------------") +print(a) #5. Create a 5x2x3 3-dimensional array with all values equaling 1. #Assign the array to variable "b" - - +print("5.-----------------") +b = np.ones((5, 2, 3)) #6. Print b. - - +print("6.-----------------") +print(b) #7. Do a and b have the same size? How do you prove that in Python code? - - - +print("7.-----------------") +print(a.size, "=", b.size) #8. Are you able to add a and b? Why or why not? - - +print("8.-----------------") +""" print(a + b)""" +# Not possible because it's not the same shape. But they have the same size and dimension so we could transpose a to the same shape of b to add them toghether #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". - - +print("9.-----------------") +c = b.transpose(1, 2, 0) +print(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("10.-----------------") +d = a + c +print(d) +#Because now they have the same shape #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. - - - +print("11.-----------------") +print(a) +print(d.astype(int)) +# 'd' array has all its values from 'a' array plus 1. Because all values in 'b' array are 1 then they are transposed into 'c' so they can be added to 'a' and make 'd' #12. Multiply a and c. Assign the result to e. - - +print("12.-----------------") +e = a * c #13. Does e equal to a? Why or why not? - - +print("13.-----------------") +print(e == a) +# yes because all values from 'a' are being multiplied by 1 from array 'c' #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" - - +print("14.-----------------") +d_max = np.max(d) +d_min = np.min(d) +d_mean = np.mean(d) +print(f"d_max is: {d_max}") +print(f"d_min is: {d_min}") +print(f"d_mean is: {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`. - - +print("15.-----------------") +f = np.empty((2, 3, 5)) +print(f) """ @@ -74,9 +87,12 @@ 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. """ - - - +print("16.-----------------") +f[(d_min < d) & (d < d_mean)] = 25 +f[d == d_mean] = 50 +f[(d_mean < d) & (d < d_max)] = 75 +f[d == d_min] = 0 +f[d == d_max] = 100 """ #17. Print d and f. Do you have your expected f? @@ -85,7 +101,7 @@ [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981], [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]], - [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381], + [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381], [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881], [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]]) @@ -94,11 +110,12 @@ [ 75., 75., 25., 25., 25.], [ 75., 25., 75., 75., 75.]], - [[ 25., 25., 25., 25., 100.], + [[ 25., 25., 25., 25., 100.], [ 75., 75., 75., 75., 75.], [ 25., 75., 0., 75., 75.]]]) """ - +print("17.-----------------") +print(f) """ #18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values @@ -107,8 +124,17 @@ [ 'D', 'D', 'B', 'B', 'B'], [ 'D', 'B', 'D', 'D', 'D']], - [[ 'B', 'B', 'B', 'B', 'E'], + [[ 'B', 'B', 'B', 'B', 'E'], [ '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 +""" +print("18.-----------------") +h = np.empty((2, 3, 5)).astype(str) + +h[(d_min < d) & (d < d_mean)] = "B" +h[d == d_mean] = "C" +h[(d_mean < d) & (d < d_max)] = "D" +h[d == d_min] = "A" +h[d == d_max] = "E" +print(h) \ No newline at end of file