import numpy as np
a = np.array([-3, -2, 9, 1, -1])
b = np.array([11, 2, 3, 9, 5, 10, 4])
maskA = a > 0
maskB = (b > 3) & (b < 9)
a[maskA] = 0
print(a)
print(b[maskB])
[-3 -2 0 0 -1] [5 4]
import numpy as np
import numpy.typing as npt
def clean(a: npt.NDArray) -> npt.NDArray:
"""
Replace all negative values in a with 0,
then reverse the array in place and return it.
"""
# Step 1: replace negatives with 0
a[a < 0] = 0
# Step 2: reverse the array *in place*
# a[::-1] makes a reversed copy, so assign it back using slice on left
a[:] = a[::-1]
return a
clean(np.array([-1, 1,2,3,4]))
array([4, 3, 2, 1, 0])
import matplotlib.pyplot as plt
import numpy as np
temps = np.array([-3, -2, 0, -5, -3, 2, 5, 4, 8, 11, 15, 13, 9, 12, 7, 4, 5, 6, 3, -1, 0, -2, -5, -11])
days = np.array(range(1,25)) # or use np.linspace(1,24,24)
freezing = temps < 0
cold = (temps < 10) & (temps >=0)
warm = temps >= 10
plt.plot(days, temps, label = "temperature fluctuations")
plt.plot(days[freezing], temps[freezing], 'bo', label = "freezing")
plt.plot(days[cold], temps[cold], 'go', label = "cold")
plt.plot(days[warm], temps[warm], 'ro', label = "warm")
plt.xlabel('Day')
plt.ylabel('Temperature')
plt.title('Temperatures over 24 Days')
plt.legend()
plt.show()
# plotting with pretty colors
import matplotlib.pyplot as plt
import numpy as np
temps = np.array([-3, -2, 0, -5, -3, 2, 5, 4, 8, 11, 15, 13, 9, 12, 7, 4, 5, 6, 3, -1, 0, -2, -5, -11])
days = np.array(range(1,25))
freezing = temps < 0
cold = (temps < 10) & (temps >=0)
warm = temps >= 10
plt.plot(days, temps, color = "lightskyblue", label = "temperature fluctuations")
plt.plot(days[freezing], temps[freezing], color = 'dodgerblue', marker = "o", linestyle = "none", label = "freezing")
plt.plot(days[cold], temps[cold], color = 'mediumslateblue', marker = "o", linestyle = "none", label = "cold")
plt.plot(days[warm], temps[warm], color = 'hotpink', marker = "o", linestyle = "none", label = "warm")
plt.xlabel('Day')
plt.ylabel('Temperature')
plt.title('Temperatures over 24 Days')
plt.legend()
plt.show()