From ef1411b653e4e19c1ec06a52c656ae3db4887c5e Mon Sep 17 00:00:00 2001 From: Estherkii <123992666+Estherkii@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:55:06 +0200 Subject: [PATCH] lab-numpy --- your-code/main.py | 86 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..676d05f 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,69 +1,91 @@ #1. Import the NUMPY package under the name np. - - +import numpy as np #2. Print the NUMPY version and the configuration. - +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)) +a = np.random.randint(0, 10, size=(2, 3, 5)) +a = np.random.random_sample ((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) #8. Are you able to add a and b? Why or why not? - +# Yes, you can do it by using transpose. the function doesn't modify the original array but returns a new array with the dimensions rearranged as specified. #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) +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) +# Values in d are one unit greater than the values in a +# The values in d are incremented by 1 for each element in the array +# "a" contains values in the range [0, 1) while "d" contains values in the range [1, 2). #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" is not equal to "a", because we hace obtained "e" by multiplying the elements of "a" and "c" #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_min = np.min(d) +print(d_min) +d_max = np.max(d) +print(d_max) - -#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`. +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) """ #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. @@ -75,7 +97,20 @@ Note: you don't have to use Numpy in this question. """ - +for i in range(2): #because there are two blocks + for j in range(3): + for k in range(5): + + if d[i][j][k] > d_min and d[i][j][k] < d_mean: + f[i][j][k] = 25 + elif d[i][j][k] > d_mean and d[i][j][k] < d_max: + f[i][j][k] = 75 + elif d[i][j][k] == d_mean: + f[i][j][k] = 50 + elif d[i][j][k] == d_min: + f[i][j][k] = 0 + elif d[i][j][k] == d_max: + f[i][j][k] = 100 """ @@ -89,6 +124,7 @@ [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881], [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]]) + Your f should be: array([[[ 75., 75., 75., 25., 75.], [ 75., 75., 25., 25., 25.], @@ -98,7 +134,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 +148,23 @@ [ '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(shape=(2, 3, 5), dtype='object') +print(g) + +for i in range(2): #because there are two blocks + for j in range(3): + for k in range(5): + + if d[i][j][k] > d_min and d[i][j][k] < d_mean: + g[i][j][k] = "B" + elif d[i][j][k] > d_mean and d[i][j][k] < d_max: + g[i][j][k] = "D" + elif d[i][j][k] == d_mean: + g[i][j][k] = "C" + elif d[i][j][k] == d_min: + g[i][j][k] = "A" + elif d[i][j][k] == d_max: + g[i][j][k] = "E" + +print(g)