From 3c39aed5260008b1e626ef752344230e670f008e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noem=C3=AD=20Cervero?= Date: Tue, 17 Oct 2023 17:37:52 +0200 Subject: [PATCH] finished lab numpy --- your-code/main.py | 80 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 21 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..f9d0575 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,68 +1,93 @@ #1. Import the NUMPY package under the name np. - +import numpy as np #2. Print the NUMPY version and the configuration. - +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? - +a = np.random.random((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(f"The size of a is {a.size}") +print(f"The size of b is {b.size}") - +# -> We can prove it by itering all the elements in the array +def count_elements(array): + counter = 0 + for i in array: + for j in i: + for x in j: + counter += 1 + return counter +print(f"a has {count_elements(a)} elements") +print(f"b has {count_elements(a)} elements") #8. Are you able to add a and b? Why or why not? - +try: + addition = np.add(a, b) + print(addition) +except ValueError: + print("The arrays cannot be added because they have different shape.") #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". - +c = np.transpose(b, (1, 2, 0)) +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? - +d = np.add(a, c) +print(d) +# -> Now it works because both 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(a) +print(d) +# As the transposed b had ones, the result of d is that all the elements f a have changed to the previous value + 1. #12. Multiply a and c. Assign the result to e. - +e = np.multiply(a, c) +print(e) #13. Does e equal to a? Why or why not? - - +# Yes, it equals because what multiply does is that multiplies each element of the array a for 1 (array c). #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) +d_min = np.min(d) +d_mean = np.mean(d) - +# Print the results +print("Max value:", d_max) +print("Min value:", d_min) +print("Mean value:", 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) """ @@ -74,8 +99,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. """ - - +f[(d > d_min) & (d < d_mean)] = 25 +f[d == d_mean] = 50 +f[(d > d_mean) & (d < d_max)] = 75 +f[d == d_min] = 0 +f[d == d_max] = 100 +print(f) """ @@ -98,6 +127,8 @@ [ 75., 75., 75., 75., 75.], [ 25., 75., 0., 75., 75.]]]) """ +print(d) +print(f) """ @@ -111,4 +142,11 @@ [ '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 +""" +x = np.empty((2, 3, 5)).astype(str) +x[(d > d_min) & (d < d_mean)] = 'B' +x[d == d_mean] = 'C' +x[(d > d_mean) & (d < d_max)] = 'D' +x[d == d_min] = 'A' +x[d == d_max] = 'E' +print(x) \ No newline at end of file