Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions your-code/main.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,72 @@
#1. Import the NUMPY package under the name np.


import numpy as np
import random

#2. Print the NUMPY version and the configuration.


print (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?


a = np.random.randint(100, size = (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 (a.size == b.size)


#8. Are you able to add a and b? Why or why not?


# 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 = np.transpose(b, (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 = a + c

#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.

print (a)
print (d)


# c dds 1 to every value in a, so the values of d are larger by magnitude of 1

#12. Multiply a and c. Assign the result to e.


e = a * c

#13. Does e equal to a? Why or why not?


print (e == a) # Yes, since c multiplies 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_max = max(d.flatten())
d_min = min(d.flatten())
d_mean = np.mean(d.flatten())


#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))


"""
Expand All @@ -76,6 +80,21 @@
"""


for i in range(2):
for j in range(3):
for k in range(5):
element = d[i, j, k]
if d_min < element < d_mean:
f[i, j, k] = 25
elif d_mean < element < d_max:
f[i, j, k] = 75
elif element == d_mean:
f[i, j, k] = 50
elif element == d_min:
f[i, j, k] = 0
elif element == d_max:
f[i, j, k] = 100



"""
Expand All @@ -99,6 +118,9 @@
[ 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
Expand All @@ -111,4 +133,25 @@
[ 'D', 'D', 'D', 'D', 'D'],
[ 'B', 'D', 'A', 'D', 'D']]])
Again, you don't need Numpy in this question.
"""
"""

labels = {0: "A", 25: "B", 50: "C", 75: "D", 100: "E"}

f_l = np.empty((2, 3, 5), dtype=str)

for i in range(2):
for j in range(3):
for k in range(5):
element_l = d[i,j,k]
if d_min < element_l < d_mean:
f_l[i,j,k] = labels[25]
elif d_mean < element_l < d_max:
f_l[i,j,k] = labels[75]
elif element_l == d_mean:
f_l[i,j,k] = labels[50]
elif element_l == d_min:
f_l[i,j,k] = labels[0]
elif element_l == d_max:
f_l[i,j,k] = labels[100]

print (f_l)