From 900889bc64e0c738114160cb973bd58e73e807a3 Mon Sep 17 00:00:00 2001 From: Alejandro Arroyo Yamin Date: Wed, 10 Jul 2019 13:07:53 -0500 Subject: [PATCH] Terminanos PP Numpy, Enrique y Alejandro --- .DS_Store | Bin 0 -> 6148 bytes your-code/main.py | 177 ++++++++++++++++++++++++++-------------------- 2 files changed, 99 insertions(+), 78 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3dcd9092d222cf11a39611e0626a9bc24356afde GIT binary patch literal 6148 zcmeHKJ5EC}5S)b+L1|J_`T{AqffXeu-~xy|x=2q7Nlu zk6wbLoQI;@%xtDCJl6pPz=ev5QiPt+&{q`;*D z_qkkq{om97=>IQCT1f#Za8nA{V)wk;@<~-&XOHt*+vqQJ&-tReaUK*7QI3gGj=Au1 e{1{1@*L=?XUN|KNo$;U(^)ukQ$fUr3EAS16%oQL2 literal 0 HcmV?d00001 diff --git a/your-code/main.py b/your-code/main.py index 78c792b..cbccb47 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,114 +1,135 @@ #1. Import the NUMPY package under the name np. - - +import numpy as np #2. Print the NUMPY version and the configuration. - - +print('Numpy version: ', np.version.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? +print('First way: generating random floating numbers between 0 and 1: a = np.random.random((2,3,5))') +a = np.random.random((2,3,5)) +print('Second way: generating random integers between a specific range: a_1 = np.random.randint(0,9,size=(2,3,5))') +a_1 = np.random.randint(0,9,size=(2,3,5)) -#4. Print a. +print('Third way: generates random floating numbers between 0 and 1, without the use of a tupple in the argument: a_2 = np.random.rand(2,3,5)') +a_2 = np.random.rand(2,3,5) +print('Fourth way: generates random floating numbers between 0 and 1 according to a statistical distribution: a_3 = np.random.random_sample((2,3,5))') +a_3 = 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('They have the same size because they have the same number of elements: 30') +print(a.size == b.size) #8. Are you able to add a and b? Why or why not? +print("No, we are not able to add them because they don't 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". - - +#9. Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to variable "c". +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 = np.add(a,c) +print("We can add 'a' and 'c' now because they are 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) +print("We can see that each element in 'd' is larger by 1, which is the value of 'c'.") #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(e) +print("Yes, because we're multiplying each element in 'a' by 1.") #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_mean = np.mean(d) +d_max = np.max(d) +d_min = np.min(d) - - +print("Mean of 'd': ", d_mean) +print("Max of 'd': ", d_max) +print("Min of 'd': ", d_min) #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`. - - - -""" -#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. -If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f. -If a value equals to d_mean, assign 50 to the corresponding value in f. -Assign 0 to the corresponding value(s) in f for d_min in d. -Assign 100 to the corresponding value(s) in f for d_max in d. -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. -""" - - - - -""" -#17. Print d and f. Do you have your expected f? -For instance, if your d is: -array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931], - [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981], - [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]], - - [[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]]]) - -Your f should be: -array([[[ 75., 75., 75., 25., 75.], - [ 75., 75., 25., 25., 25.], - [ 75., 25., 75., 75., 75.]], - - [[ 25., 25., 25., 25., 100.], - [ 75., 75., 75., 75., 75.], - [ 25., 75., 0., 75., 75.]]]) -""" - - -""" -#18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values -("A", "B", "C", "D", and "E") to label the array elements? You are expecting the result to be: -array([[[ 'D', 'D', 'D', 'B', 'D'], - [ 'D', 'D', 'B', 'B', 'B'], - [ 'D', 'B', 'D', 'D', 'D']], - - [[ 'B', 'B', 'B', 'B', 'E'], - [ '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 = 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. If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.If a value equals to d_mean, assign 50 to the corresponding value in f. Assign 0 to the corresponding value(s) in f for d_min in d. Assign 100 to the corresponding value(s) in f for d_max in d. 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 li in range(f.shape[0]): + for el in range(f.shape[1]): + for x in range(f.shape[2]): + if d_min == d[li,el,x]: + f[li,el,x] = 0 + if d_min < d[li,el,x] and d[li,el,x] < d_mean: + f[li,el,x] = 25 + if d_mean == d[li,el,x]: + f[li,el,x] = 50 + if d_mean < d[li,el,x] and d[li,el,x] < d_max: + f[li,el,x] = 75 + if d_max == d[li,el,x]: + f[li,el,x] = 100 + +#17. Print d and f. Do you have your expected f For instance, if your d is: +#array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931], +# [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981], +# [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]], +# +# [[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]]]) + +#Your f should be: +#array([[[ 75., 75., 75., 25., 75.], +# [ 75., 75., 25., 25., 25.], +# [ 75., 25., 75., 75., 75.]], +# +# [[ 25., 25., 25., 25., 100.], +# [ 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 ("A", "B", "C", "D", and "E") to label the array elements? You are expecting the result to be: +#array([[[ 'D', 'D', 'D', 'B', 'D'], +# [ 'D', 'D', 'B', 'B', 'B'], +# [ 'D', 'B', 'D', 'D', 'D']], +# +# [[ 'B', 'B', 'B', 'B', 'E'], +# [ 'D', 'D', 'D', 'D', 'D'], +# [ 'B', 'D', 'A', 'D', 'D']]]) +#Again, you don't need Numpy in this question. + +g = np.empty((2,3,5),dtype=str) + +for li in range(g.shape[0]): + for el in range(g.shape[1]): + for x in range(g.shape[2]): + if d_min == d[li,el,x]: + g[li,el,x] = 'E' + if d_min < d[li,el,x] and d[li,el,x] < d_mean: + g[li,el,x] = 'D' + if d_mean == d[li,el,x]: + g[li,el,x] = 'C' + if d_mean < d[li,el,x] and d[li,el,x] < d_max: + g[li,el,x] = 'B' + if d_max == d[li,el,x]: + g[li,el,x] = 'A' + +print(g) \ No newline at end of file