From 7838e0a11e0950eca3fc0ab1aac7e203ecab2c58 Mon Sep 17 00:00:00 2001 From: linharesjunior Date: Tue, 17 Oct 2023 17:42:40 +0200 Subject: [PATCH] lab numpy done --- your-code/main.py | 63 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 78c792b..4f158c1 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,68 +1,92 @@ #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.show_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? -#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 +a = np.random.random((2, 3, 5)) +# a2 = np.random.randint(2, 3, 5) +# a3 = np.random.choice(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? - +if a.size == b.size: + print("a and b have the same size") +else: + print("a and b do not have the same size") #8. Are you able to add a and b? Why or why not? - + # Answer: No, because 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 = b.transpose(1, 2, 0) +print(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? +a = (2, 3, 5) +c = np.array(a) - -#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. +d = np.array([[2, 3, 5], [2, 3, 5]]) +print(d) # a and c 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 +print(e) #13. Does e equal to a? Why or why not? - + # Answer: No, beacuse e it's a result of a multiplication (a * c) + # and c is a transposed version of b #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("Max value:", d_max) +print("Min value:", d_min) +print("Mean value:", 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)) +print(f) """ @@ -75,7 +99,12 @@ Note: you don't have to use Numpy in this question. """ - +f = np.where((d > d_min) & (d < d_mean), 25, + np.where((d > d_mean) & (d < d_max), 75, + np.where(d == d_mean, 50, + np.where(d <= d_min, 0, + np.where(d >= d_max, 100, d))))) +print(f) """