diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..815596f Binary files /dev/null and b/.DS_Store differ diff --git a/your-code/main.py b/your-code/main.py index 78c792b..71af2dc 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,69 +1,76 @@ +# coding=utf-8 #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? - - +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.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? - - - +print(a.shape) +print(b.shape) #8. Are you able to add a and b? Why or why not? - - +''' +Answer: No, because they do not have the same 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.reshape(b, (2,3,5)) +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) #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. - - - +print("Array a:\n", a) +print("Array d:\n", d) +''' +Explain: Array "a" values have been added with correspondent index in array "d". +''' #12. Multiply a and c. Assign the result to e. - - +e = np.multiply(a,c) #13. Does e equal to a? Why or why not? - - - +print("Array e:\n", e) +''' +Explain: Yes, they are equal because array c only has the number 1 as elements; Any number multiplied by 1 == the same number +''' #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("\nd_max:", d_max) +print("\nd_min:", d_min) +print("\nd_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((2,3,5)) """ #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,8 +82,19 @@ Note: you don't have to use Numpy in this question. """ - - +for k, x in enumerate(d): + for m, y in enumerate(x): + for n, z in enumerate(y): + if z > d_min and z < d_mean: + f[k,m,n] = 25 + if z > d_mean and z < d_max: + f[k, m, n] = 75 + if z == d_min: + f[k, m, n] = 0 + if z == d_max: + f[k, m, n] = 100 + if z == d_mean: + f[k, m, n] = 50 """ #17. Print d and f. Do you have your expected f? @@ -99,6 +117,8 @@ [ 25., 75., 0., 75., 75.]]]) """ +print("Array D:\n", d) +print("Array F:\n", f) """ #18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values @@ -111,4 +131,20 @@ [ '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=object) +for k, x in enumerate(d): + for m, y in enumerate(x): + for n, z in enumerate(y): + if z > d_min and z < d_mean: + g[k,m,n] = "B" + if z > d_mean and z < d_max: + g[k, m, n] = "D" + if z == d_min: + g[k, m, n] = "A" + if z == d_max: + g[k, m, n] = "E" + if z == d_mean: + g[k, m, n] = "C" + +print("Array G:\n", g) \ No newline at end of file