Practice2
中文版:练习 2
Import NumPy using np as an alias (refer to Week 9: lab4 02.0202 and 02.07)
import numpy as np- Create a 6×6 matrix X (with all elements being unique, e.g., 1 to 36), and extract the 3rd and 5th columns from X to form a new matrix Y.
# Step 1: Create a 6x6 matrix with unique elements (e.g., 1 to 36)
# to complete
X =
print("Matrix X (6x6):")
print(X)
# Step 2: Extract the 3rd and 5th columns (indices 2 and 4 in 0-based indexing)
# to complete
Y =
print("\nMatrix Y (6x2, 3rd & 4th columns of X):")
print(Y)- Now we have an array X_1 of shape 6x6, predict the result of X+X_1
# to complete
X_1=# run for test
Z=X+X_1
print(Z)- Predict the result of X + Y, and use code to perform the corresponding calculation
Z=X+Y # is this correct?The rest of this practice may include next week’s lecture material, you can have a look or leave these questions for next week and continue the Practice3
- Calculate the maximum value in each row of X, and the minimum value in each column of Y.
# Max in each row of X
# to complete
row_max_X =
print("Max per row of X:", row_max_X)
# Min in each column of Y
# to complete
col_min_Y =
print("Min per column of Y:", col_min_Y)- Create an 8×8 array filled with zeros. Randomly select 25 positions and fill them with random numbers greater than 0 using ‘np.random.uniform’. Print the resulting matrix W.
# Step 1: Create an 8x8 array of zeros
W =
# Step 2: Randomly select 25 unique positions (indices)
# Step 3: Fill selected positions with random numbers > 0
# Print the resulting matrix W
print("Matrix W (8x8 with 25 random non-zero entries):")
print(W)- Calculate the average value of the elements in the matrix and store it in a variable named mean_W. Replace all the zeros in W with the value of mean_W.
# Calculate the mean of all elements in W (excluding zeros if needed)
mean_W =
# Replace zeros with mean_W
W_no_zeros =
# Alternatively, modify W in-place:
# W[W == 0] = mean_W
print("Mean value (mean_W):", mean_W)
print("\nMatrix W with zeros replaced by mean_W:")
print(W_no_zeros)- Replace all values in the matrix that are smaller than mean_W with -1, and print how many -1 values are in the matrix.
# Calculate mean_W (as previously defined)
mean_W =
# Replace values < mean_W with -1
W_replaced =
# Count the number of -1 values
count_minus_1 =
# Print results
print(f"Mean value (mean_W): {mean_W:.4f}")
print(f"Number of -1 values: {count_minus_1}")
print("\nMatrix after replacement:")
print(W_replaced)- Compute the transpose of matrix W and store it in Z. Combine W and Z to form a 3-dimensional matrix V, and then flatten Z to get a new 1-dimensional array U.
# Step 1: Compute transpose of W and store in Z
Z =
# Step 2: Combine W and Z to form a 3D matrix V (shape: 2 x rows x cols)
V =
# Step 3: Flatten Z to get a 1D array U
U =
# Print results
print("Transpose Z:")
print(Z)
print("\n3D Matrix V (shape: {}):".format(V.shape))
print(V)
print("\nFlattened array U:")
print(U)- Find the third largest value in matrix W, denoted as Max3. Then, obtain a Boolean matrix B from W where all elements greater than or equal to Max3 are marked as True, and the rest as False.
# Step 1: Find the third largest value (Max3)
# Step 2: Create Boolean matrix B
B =
# Print results
print(f"Third largest value (Max3): {Max3}")
print("\nBoolean matrix B:")
print(B)- Print the elements in Z that correspond to the True positions in B and calculate their average.
# Step 1: Get elements in Z where B is True
selected_elements =
# Step 2: Calculate the average of these elements
average_selected =
# Print results
print("Elements in Z corresponding to True positions in B:")
print(selected_elements)
print(f"\nAverage of these elements: {average_selected}")- Combine A and B into a 2x4 matrix C. Then, combine A and B into an 4x2 matrix D. Finally, print out the elements of C row by row., print out the elements of D column by column.
# Combine into C (2x4) by horizontal stacking
C =
# Combine into D (4x2) by vertical stacking
D =
print(C)
print(D)# Print C row by row
print("Matrix C (row by row):")
for row in C:
print(row)
# Print D column by column
print("\nMatrix D (column by column):")
for col_idx in range(D.shape[1]): # Iterate over the list colomn index
column = D[:, col_idx] # get the current column
print(column)