From 7adf65d8f24fb006121c3bc31d41ea92c09d4989 Mon Sep 17 00:00:00 2001 From: Leon Plaza Date: Tue, 17 Oct 2023 17:12:44 +0200 Subject: [PATCH] Leon --- your-code/main.py | 86 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 23 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..31b9c7b 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,67 +1,76 @@ #1. Import the NUMPY package under the name np. - +import numpy as np #2. Print the NUMPY version and the configuration. - - +print(np.__version__) +print(np.__config__) #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? - - +import random as rd +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? - - +a.size == b.size + # Yes #8. Are you able to add a and b? Why or why not? - - +try: + a + b +except: + print("Error") +# Can't add a and b while they have different shapes #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)) +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? - +d = a + c +# Works now because a and c has equal shape #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. +print(a) +print("\n") +print(d) - - +# The difference for each position between both arrays is 1 #12. Multiply a and c. Assign the result to e. - +e = a * c #13. Does e equal to a? Why or why not? - +# Yes, because each element of a is multiplied by 1, so the result is the same #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" - +d_max = d.max() +d_min = d.min() +d_mean = 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((d.shape), dtype="int") @@ -75,7 +84,19 @@ 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]): + if d[i][x][z] == d_min: + f[i][x][z] = 0 + elif (d_min < d[i][x][z] < d_mean): + f[i][x][z] = 25 + elif d[i][x][z] == d_mean: + f[i][x][z] = 50 + elif (d_max > d[i][x][z] > d_mean): + f[i][x][z] = 75 + elif d[i][x][z] == d_max: + f[i][x][z] = 100 """ @@ -98,7 +119,9 @@ [ 75., 75., 75., 75., 75.], [ 25., 75., 0., 75., 75.]]]) """ - +print(d) +print("\n") +print(f) """ #18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values @@ -111,4 +134,21 @@ [ '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(d.shape, dtype="str") + +for i in range(d.shape[0]): + for x in range(d.shape[1]): + for z in range(d.shape[2]): + if d[i][x][z] == d_min: + g[i][x][z] = "A" + elif (d_min < d[i][x][z] < d_mean): + g[i][x][z] = "B" + elif d[i][x][z] == d_mean: + g[i][x][z] = "C" + elif (d_max > d[i][x][z] > d_mean): + g[i][x][z] = "D" + elif d[i][x][z] == d_max: + g[i][x][z] = "E" + +print(g) \ No newline at end of file