From 154145ce905ee5d5d7545719b34d46d2ee476ea3 Mon Sep 17 00:00:00 2001 From: Lucia Aguzzoni Date: Tue, 17 Oct 2023 16:10:45 +0200 Subject: [PATCH 1/3] solutions --- your-code/main.py | 104 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..277f708 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,66 +1,119 @@ #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. +print(np.__version__) +print(np.show_config()) + #3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable "a" +a=np.array([[[rd.randint(0,101) for k in range(5)] for j in range(3)] for i in range(2)]) #from Python List to Numpy Array + # Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find? +a1=np.random.randint(101, size =(2, 3, 5)) #np.random.randint +#a2 + #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("Do a and b have the same size?") +print(a.size==b.size) +print(f"a has size: {a.size}") +print(f"b has size: {b.size}") #8. Are you able to add a and b? Why or why not? +#No, because they have same size but not same shape +print(f"a has shape: {a.shape}") +print(f"b has shape: {b.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=b.transpose(1,2,0) +print(f"c has shape: {c.shape}") +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 +print(d) +#now it works because a and c has 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 numpy:") +print(a) +print("d numpy:") +print(d) +#d is the sum of a and c. since every element of c is a 1, the diifferece between an element of d and the element of a with the same coordinates is always 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? +print("Does e equal to a?") +print(e==a) +# e equals to a because it was obtaining multypling the elements of a for the elements of c, which are all 1s #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) +print(f"max value in d is {d_max}") + +d_min = np.min(d) +print(f"min value in d is {d_min}") + +d_mean = np.mean(d) +print(f"mean value of d is {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)) @@ -74,8 +127,28 @@ 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(2): + for j in range(3): + for k in range(5): + d_elem=d[i,j,k] + if d_elem>d_min and d_elemd_mean and d_elemd_min and d_elemd_mean and d_elem Date: Tue, 17 Oct 2023 16:19:09 +0200 Subject: [PATCH 2/3] solutions --- your-code/main.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 277f708..3e38118 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -17,7 +17,7 @@ # Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find? a1=np.random.randint(101, size =(2, 3, 5)) #np.random.randint -#a2 +a2=np.random.random((2,3,5)) #np.random.random @@ -93,7 +93,8 @@ #13. Does e equal to a? Why or why not? print("Does e equal to a?") -print(e==a) +#print(e==a) +print(np.array_equal(e,a)) # e equals to a because it was obtaining multypling the elements of a for the elements of c, which are all 1s @@ -142,15 +143,9 @@ elif d_elem == d_max: f[i,j,k]=100 -print(d) -print(f"max value in d is {d_max}") -print(f"min value in d is {d_min}") -print(f"mean value of d is {d_mean}") -print(f) - """ #17. Print d and f. Do you have your expected f? For instance, if your d is: From b5e490e1740986146961866344c6e2b62bc25cc3 Mon Sep 17 00:00:00 2001 From: Lucia Aguzzoni Date: Tue, 17 Oct 2023 17:09:35 +0200 Subject: [PATCH 3/3] solutions --- your-code/main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 3e38118..9737cbc 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -128,9 +128,9 @@ 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(2): - for j in range(3): - for k in range(5): +for i in range(len(d)): + for j in range(len(d[i])): + for k in range(len(d[i,j])): d_elem=d[i,j,k] if d_elem>d_min and d_elemd_min and d_elem