1/6
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
import numpy as np
np.random.seed(0)
arr_1 = np.random.randint(10, 21, size = (2, 3))
arr_2 = np.random.randint(10, 21, size = (1, 3))
display(arr_1, arr_1.shape)
display(arr_2, arr_2.shape)
np.random.seed is used to ensure the results are reproducible by ensuring the values in the arrays remain the same after each execution. Two 2-D arrays are filled with random integers from 10-20. The first array includes 2 rows and 3 columns while the second one includes 1 row and 3 columns. Shape counts the number of rows and columns in the arrays.
Example for reference
import numpy as np
np.random.seed(0)
arr_1 = np.random.randint(10, 21, size = (2, 3))
arr_2 = np.random.randint(10, 21, size = (1, 3))
display(arr_1, arr_1.shape)
display(arr_2, arr_2.shape)
Explain these lines of code
arr_sum = arr_1 + arr_2
display(arr_sum, arr_sum.shape)
# book version
arr_sum = np.add(arr_1, arr_2)
display(arr_sum, arr_sum.shape)
arr_sum = sum(arr_1, arr_2)
display(arr_sum, arr_sum.shape)
The first and second lines of code add both arrays together through element-wise addition. The first line of code adds both arrays with + while the second uses np.add to add both arrays. Both methods use broadcasting which changes the shape of the smaller array to match the shape of the larger array which makes the result is an array with 2 rows and 3 columns.
The use of sum on both array doesn’t perform element-wise addition on the arays. The function iterates over the first array and adds the elements in the second array by the first row in the first array and then adds the result by the second row of the first array, resulting in an array with 1 row and 3 columns.
Example for reference
import numpy as np
np.random.seed(0)
arr_1 = np.random.randint(10, 21, size = (2, 3))
arr_2 = np.random.randint(10, 21, size = (1, 3))
explain these lines of code
arr_2 = arr_2.T
np.dot(arr_1, arr_2)
# book version
np.dot(arr_1, arr_2.transpose())
Both arrays transpose the second array to calculate the dot product of the first and second array. The second line of code uses the transpose function within the np.dot function to calculate the dot product. Transposing the second array flips its rows and columns thus changing its shape from a 1 row and 3 columns to 3 rows and 1 column. This is done to successfully calculate the dot product of the first and second arrays. The result of the dot product results in an array with 2 rows and 1 column.
import numpy as np
import matplotlib.pyplot as plt
import statistics
np.random.seed(0)
arr3 = np.random.randint(0, 11, size = (100))
plt.hist(arr3, range(arr3.min(), arr3.max()), color = 'red')
plt.title('Historgram')
print(f'median: {np.median(arr3).astype(int)} mode: {statistics.mode(arr3)}')
np.random.seed ensures the array is reproducible by ensuring that the values in the array remains the same after each execution. A 1-D array is filled with 100 random integers from 0-10 with np.random.randint. Size is used to set the number of elements of the array to 100. Plt.hist is used to create a histogram from the array. The histogram’s bins are set to the array’s lowest and highest values. The color of the bars are set to red and plt.title is used to assign a title to the histogram. The median and mode of the array are calculated with the np.median and statistics.mode functions. The median and mean are converted to integers with astype.
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
arr4 = np.random.randn(2, 5, 10).astype(float)
x = arr4[:,0]
y = arr4[:,1]
z = arr4[:,2]
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(x, y, z, color = 'green')
ax.set_title('3-D scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
arr4 = np.random.randn(2, 5, 10).astype(float) fills a 3-D array with 2 dimensions, 5 rows, and 10 columns with random floats from a standard normal distribution. The array is sliced three times and assigned to variables representing the axis of the 3-D scatter plot. arr4[:,0] slices the first columns of the two 2-D slices, arr4[:,1] slices the second columns, and z = arr4[:,2] slices the third columns. fig = plt.figure() creates a plot and ax = fig.add_subplot(111, projection = '3d') creates a 3-D plot within a 1×1 grid. ax.scatter(x, y, z, color = 'green') creates a 3-D scatter plot by using the indices from the x, y, and x variables as it’s axis and setting the color of the dots to green. ax.set_title('3-D scatter plot') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') assigns titles to the plot and its three axis.
import numpy as np
lst =[[0, 1, 3, 0, 4], [8, 9, 0, 9, 6]]
arr5 = np.array(lst)
arr5 = arr5.flatten()
arr5 = [i for i in arr5 if i > 0]
print(arr5)
a list is converted into a 2-D array with np.array. This array is then converted into a 1-D array with the flatten method. A list comprehension is used to loop over the values in the array greater than 0. The list comprehension is assigned to the variable containing the array ensuring that the new array only includes non-zero values.
# book version
lst =[[0, 1, 3, 0, 4], [8, 9, 0, 9, 6]]
arr5 = np.array(lst)
arr5 = np.flatnonzero(arr5)
print(arr5)
a list is converted into a 2-D array with np.array. Np.flattenzero operates on a 1-D version of the array and prints all the indices of non-zero elements in the array.