diff --git a/your-code/main.py b/your-code/main.py index 78c792b..3be3ea0 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 +import random as rd #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.randint(0, 9, size = (2,3,5)) +a_1 = np.random.random((2,3,5)) +a_2 = np.array([rd.uniform(10, 60) for i in range(20)]) #4. Print a. +print(a) +print(a_1) +print(a_2) +#5. Create a 5x2x3 3-dimensional array with all values equaling 1.y -#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 #8. Are you able to add a and b? Why or why not? +"Yes, I am able to do it if I transpose one of the array" #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 +c = b.transpose(1, 2, 0) #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 +"I can do the sum now because both arrays 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) #12. Multiply a and c. Assign the result to e. - +e = a * c #13. Does e equal to a? Why or why not? - +e.shape == a.shape #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) #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) """ @@ -75,6 +88,19 @@ Note: you don't have to use Numpy in this question. """ +for i in range(2): + 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 @@ -88,7 +114,10 @@ [[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]]]) +""" +print(d) +""" Your f should be: array([[[ 75., 75., 75., 25., 75.], [ 75., 75., 25., 25., 25.], @@ -98,7 +127,7 @@ [ 75., 75., 75., 75., 75.], [ 25., 75., 0., 75., 75.]]]) """ - +print(f) """ #18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values @@ -111,4 +140,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 +""" + +f_str = np.empty((2, 3, 5), dtype=str) + + +for i in range(2): + 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_str[i][j][k] = 'B' + elif d[i][j][k] > d_mean and d[i][j][k] < d_max: + f_str[i][j][k] = 'D' + elif d[i][j][k] == d_mean: + f_str[i][j][k] = 'C' + elif d[i][j][k] == d_min: + f_str[i][j][k] = 'A' + elif d[i][j][k] == d_max: + f_str[i][j][k] = 'E' + + +print(f_str) \ No newline at end of file