From 33b845590531892e14a7d3d26e48ca5da460ffac Mon Sep 17 00:00:00 2001 From: Alex Montiel Date: Sat, 12 Oct 2019 16:34:56 -0500 Subject: [PATCH 1/2] Lab Numpy Alex / Nohely --- .idea/.gitignore | 3 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/lab-numpy.iml | 12 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + your-code/main.py | 226 +++++++++++++++++- 7 files changed, 255 insertions(+), 10 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/lab-numpy.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..0e40fe8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/lab-numpy.iml b/.idea/lab-numpy.iml new file mode 100644 index 0000000..7c9d48f --- /dev/null +++ b/.idea/lab-numpy.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a2e120d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..120609b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/your-code/main.py b/your-code/main.py index 78c792b..7a68a58 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,72 +1,249 @@ +# 1.- Import the NUMPY package under the name np. +import numpy + + +# 2. Print the NUMPY version and the configuration. +numpy_version = numpy.version +print("Version:", numpy_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? +a = numpy.random.random((2, 3, 5)) + + +# 4. Print a. +print("a: ", a) + + +# 5. Create a 5x2x3 3-dimensional array with all values equaling 1. +# Assign the array to variable "b" +b = numpy.ones((5, 3, 2)) + + +# 6. Print b. +print("b:", b) + + +# 7. Do a and b have the same size? How do you prove that in Python code? +print("Same Size A and B?", len(a) == len(b)) + + +# 8. Are you able to add a and b? Why or why not? +try: + numpy.add(a, b) + +except: + print("No son del mismo tamaño") + + +# 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 = numpy.transpose(b) +print("c:", c) + + +# 10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +try: + d = numpy.add(a, c) +except: + print("operands could not be broadcast together with shapes/ No son del mismo tamaño") + +print("a-shape: ", a.shape, "c-shape", c.shape) + + +# 11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. +print("a: ", a) +print("d: ", d) +# Numpy realizó una suma entre arreglos, sumando 1 a todos los datos del array + + +# 12. Multiply a and c. Assign the result to e. +e = numpy.multiply(a, c) +print(a, c) +print("e:", e) + + +# 13. Does e equal to a? Why or why not? +print("e equeal to a?", a == e) +# Porque solo se multiplico x1 + + +# 14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_max = d.max() +d_min = d.min() +d_mean = d.mean() +print("Max:", d_max, "Min:", d_min, "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 = numpy.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 i in range(len(d)): + for j in range(len(d[0])): + for k in range(len(d[0, 0])): + 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 +print("F:", f) + + +""" +#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. +""" #1. Import the NUMPY package under the name np. +import numpy #2. Print the NUMPY version and the configuration. +numpy_version = numpy.version +#print(numpy_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? +a = numpy.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 = numpy.ones((5,3,2)) #6. Print b. +#print(b) + #7. Do a and b have the same size? How do you prove that in Python code? +#print(len(a) == len(b)) #8. Are you able to add a and b? Why or why not? +try: + print(numpy.add(a,b)) +except: + print("operands could not be broadcast together with shapes/ No son del mismo tamaño") + #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 = numpy.transpose(b) +#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? +#10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +try: + d = numpy.add(a, c) +except: + print("operands could not be broadcast together with shapes/ No son del mismo tamaño") +#print(a.shape, c.shape) #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. - +#print("a: ", a) +#print("d: ", d) +# Numpy realizó una suma entre arreglos, sumando 1 a todos los datos del array #12. Multiply a and c. Assign the result to e. +e = numpy.multiply(a,c) +#print(a,c) +#print("e:", e) #13. Does e equal to a? Why or why not? +#print(a == e) +#Porque solo se multiplico x1 + #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_max = d.max() +d_min = d.min() +d_mean = d.mean() +#print("Max:", d_max,"Min:", d_min,"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 = numpy.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. +#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. @@ -74,8 +251,20 @@ 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(len(d)): + for j in range(len(d[0])): + for k in range(len(d[0,0])): + 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 +print(f) """ @@ -98,11 +287,12 @@ [ 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: +("A=0", "B=25", "C=50", "D=75", and "E=100") 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']], @@ -111,4 +301,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 = numpy.chararray((2,3,5)) +for i in range(len(d)): + for j in range(len(d[0])): + for k in range(len(d[0,0])): + if d[i,j,k] > d_min and d[i,j,k,] < d_mean: + g[i,j,k] = 'B' + elif d[i,j,k] > d_mean and d[i,j,k,] < d_max: + g[i,j,k] = 'D' + elif d[i,j,k] == d_mean: + g[i,j,k] = 'C' + elif d[i,j,k] == d_min: + g[i,j,k] = 'A' + elif d[i,j,k] == d_max: + g[i,j,k] = 'E' +print(g) From c612ece9136b62f232300cba5f32e99aa3658c59 Mon Sep 17 00:00:00 2001 From: Alex Montiel Date: Sat, 12 Oct 2019 16:51:35 -0500 Subject: [PATCH 2/2] 2do commit para entrega, fix el 17 con el dtype --- your-code/main.py | 191 ++++++---------------------------------------- 1 file changed, 24 insertions(+), 167 deletions(-) diff --git a/your-code/main.py b/your-code/main.py index 7a68a58..a04aa35 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -78,7 +78,7 @@ # 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 = numpy.empty((2, 3, 5)) +f = numpy.empty((2, 3, 5),dtype=object) """ @@ -95,15 +95,15 @@ for j in range(len(d[0])): for k in range(len(d[0, 0])): if d[i, j, k] > d_min and d[i, j, k,] < d_mean: - f[i, j, k] = 25 + f[i, j, k] = "B" elif d[i, j, k] > d_mean and d[i,j,k,] < d_max: - f[i, j, k] = 75 + f[i, j, k] = "D" elif d[i, j, k] == d_mean: - f[i, j, k] = 50 + f[i, j, k] = "C" elif d[i, j, k] == d_min: - f[i, j, k] = 0 + f[i, j, k] = "A" elif d[i, j, k] == d_max: - f[i, j, k] = 100 + f[i, j, k] = "E" print("F:", f) @@ -127,169 +127,10 @@ [ 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. -""" -#1. Import the NUMPY package under the name np. -import numpy - - - -#2. Print the NUMPY version and the configuration. -numpy_version = numpy.version -#print(numpy_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? -a = numpy.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 = numpy.ones((5,3,2)) - - -#6. Print b. -#print(b) - - - - -#7. Do a and b have the same size? How do you prove that in Python code? -#print(len(a) == len(b)) - - - - -#8. Are you able to add a and b? Why or why not? -try: - print(numpy.add(a,b)) -except: - print("operands could not be broadcast together with shapes/ No son del mismo tamaño") - - - - -#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 = numpy.transpose(b) -#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? -try: - d = numpy.add(a, c) -except: - print("operands could not be broadcast together with shapes/ No son del mismo tamaño") - -#print(a.shape, c.shape) - -#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. -#print("a: ", a) -#print("d: ", d) -# Numpy realizó una suma entre arreglos, sumando 1 a todos los datos del array - - - -#12. Multiply a and c. Assign the result to e. -e = numpy.multiply(a,c) -#print(a,c) -#print("e:", e) - - - -#13. Does e equal to a? Why or why not? -#print(a == e) -#Porque solo se multiplico x1 - - - - - -#14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" -d_max = d.max() -d_min = d.min() -d_mean = d.mean() -#print("Max:", d_max,"Min:", d_min,"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 = numpy.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 i in range(len(d)): - for j in range(len(d[0])): - for k in range(len(d[0,0])): - 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 +print(d) print(f) -""" -#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=0", "B=25", "C=50", "D=75", and "E=100") to label the array elements? You are expecting the result to be: @@ -302,7 +143,7 @@ [ 'B', 'D', 'A', 'D', 'D']]]) Again, you don't need Numpy in this question. """ - +''''''''' g = numpy.chararray((2,3,5)) for i in range(len(d)): for j in range(len(d[0])): @@ -318,3 +159,19 @@ elif d[i,j,k] == d_max: g[i,j,k] = 'E' print(g) +''''''''''' + +for i in range(len(d)): + for j in range(len(d[0])): + for k in range(len(d[0, 0])): + 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 +print("F:", f)