From 1f5a5f1d0defb3d91cc91996f370846b5fceb36d Mon Sep 17 00:00:00 2001 From: Maira Gutierrez Date: Tue, 17 Oct 2023 18:09:58 +0200 Subject: [PATCH] task completed with bonus --- your-code/main.py | 79 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..40c4ff3 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,68 +1,80 @@ #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.randint(101, 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.random.randint(1,2, size = (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? - - +a.size == b.size +"No they don't have the same size" #8. Are you able to add a and b? Why or why not? +"No, because they don't have the same shape. One of them needs to be transposed." #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 = a + c #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. +print(a) +print(d) - +"Yes. The in array 'd' every element is the result of every element in array 'a' plus 1, because all elements in array 'b' were 1." #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? +a == e - +"Yes, because all elements are being multiplied by one so it gives the same result" #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" - - +import numpy as np +d_max = np.max(d) +d_min = np.min(d) +d_mean = np.mean(d) #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,7 +86,21 @@ 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. """ - +for i in range(d.shape[0]): + for x in range(d.shape[1]): + for z in range(d.shape[2]): + val = d[i][x][z] + if val > d_min and val < d_mean: + f[i][x][z] = 25 + elif val > d_mean and val < d_max: + f[i][x][z] = 75 + elif val == d_mean: + f[i][x][z] = 50 + elif val == d_min: + f[i][x][z] = 0 + elif val == d_max: + f[i][x][z] = 100 +print(f) @@ -98,7 +124,8 @@ [ 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 @@ -111,4 +138,24 @@ [ '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 +""" + +g = np.empty((2,3,5), dtype=str) +print(g) + +labels = {0:"A", 25: "B", 50: "C", 75: "D", 100: "E"} +for i in range(g.shape[0]): + for x in range(g.shape[1]): + for z in range(g.shape[2]): + val = d[i][x][z] + if val > d_min and val < d_mean: + g[i][x][z] = labels[25] + elif val > d_mean and val < d_max: + g[i][x][z] = labels[75] + elif val == d_mean: + g[i][x][z] = labels[50] + elif val == d_min: + g[i][x][z] = labels[0] + elif val == d_max: + g[i][x][z] = labels[100] +print(g) \ No newline at end of file