Practice
中文版:Practice
import numpy as np
np.random.seed(0)- Create an 8×8 integer matrix A and a 2×8 floating-point matrix B, where all elements are randomly generated numbers.
# Create an 8×8 integer matrix A with random integers
A =
# Create a 2×8 floating-point matrix B with random floats
B =
# Output the matrices
print("Matrix A (8x8, integers):")
print(A)
print("\nMatrix B (2x8, floating-point):")
print(B)- Computes element-wise addition of matrix ( A ) with itself:
C=
print(C)- This subtracts the scalar 2 from every element in matrix ( A ):
Please calculate and print matrix D
D=
print(D)- This computes the element-wise square of matrix ( B ):
Please calculate and print matrix E
Note: This is not matrix multiplication, but element-wise squaring.
E =
print(E)- This computes the element-wise modulo 4 of matrix ( A ):
Calculate and print the new array F
F=
print(F)- Calculate the sum of all elements in the matrix E + B, the output should be a single value rather than an array (use the function np.sum)
s=
print(s)- Compute the Maximum, Minimum, Standard Deviation, and Mean values of matrix F
Max=
Min=
Std=
Mean=
print("max", Max)
print("min", Min)
print("std", Std)
print("mean", Mean)- Calculate the maximum value in each row of A, and the minimum value in each column of B.
MaxA=
MinB=
print("Max of each row in A: ", MaxA)
print("Min of each column in B: ", MinB)An 8×8 array G has been created and filled with zeros. 20 positions were selected randomly and filled with random numbers greater than 0 using ‘np.random.randint’.
# Create an 8x8 array G filled with zeros
G = np.zeros((8, 8))
# Randomly select 20 positions to fill with random numbers greater than 0
rows = np.random.randint(0, 8, 20) # Random row indices
cols = np.random.randint(0, 8, 20) # Random column indices
# Fill these positions with random numbers (greater than 0) from uniform distribution
for i in range(20):
G[rows[i], cols[i]] = np.random.randint(-10, 20) # Random float in the range (0, 100)
# Output the resulting matrix G
print("Matrix G (8x8 with random values at 20 positions):")
print(G)- Calculate the average value of the positive elements in the matrix and store it in a variable named mean_G.
# Calculate the average value of the elements in G (ignoring zeros)
mean_G =
print("\nMean value (mean_G) of the non-zero elements in G:")
print(mean_G)- Replace all the zeros in G with the value of mean_G.
# Replace all the zeros in G with the value of mean_G
# Output the modified matrix G and the mean value
print("Modified Matrix G (zeros replaced with mean_G):")
print(G)- Compute the sum of A and the first column of G
# Calculate the sum of A and the first column of G
first_column_G =
sum_A_first_column_G =
# Output the results
print("Sum of A and the first column of G:")
print(sum_A_first_column_G)- Create a new matrix new_G, replace all values in the matrix G that are smaller than mean_G with -1, and print how many -1 values are in the new_G.
# Create a new matrix new_G and replace values smaller than mean_G with -1
new_G =
# Count how many -1 values are in new_G
count_minus_1 =
# Output the resulting matrix and count of -1 values
print("Matrix new_G (with values smaller than mean_G replaced by -1):")
print(new_G)
print("\nNumber of -1 values in new_G:")
print(count_minus_1)- Compute the transpose of matrix G and store it in H. Then flatten H to get a new 1-dimensional array K.
# Compute the transpose of G and store it in H
H =
# Flatten H to get a new 1-dimensional array K
K =
print("\nTranspose of G (Matrix H):")
print(H.shape)
print("\nFlattened version of H (1D array K):")
print(K.shape)- Sort each row in G.
print(G)# Sort each row in G
G_sorted_rows =
# Output the result
print("Matrix G after sorting each row:")
print(G_sorted_rows)- Create a mask matrix M, where each element in M depends on whether the corresponding element in G is greater than 0.
# Create a mask matrix M, where each element is True if the corresponding element in G is greater than 0
M =
print("\nMask Matrix M (True if G > 0, otherwise False):")
print(M)- Print the elements in G that correspond to the True positions in M and calculate their average.
# Get the elements in G that correspond to the True positions in M
elements_in_G =
# Calculate the average of the selected elements
average =
print("\nElements in G corresponding to True positions in M:")
print(elements_in_G)
print("\nAverage of these elements:")
print(average)Introduction: The Iris Dataset
The Iris dataset is one of the most famous and classic datasets in machine learning and statistics. Introduced by British statistician Ronald A. Fisher in 1936, it is widely used for teaching and experimenting with classification models.
The dataset contains 150 samples (150 rows), each representing an iris flower from one of three species:
- Setosa
- Versicolor
- Virginica
Each sample includes four numerical features (4 colomns):
- Sepal length (cm)
- Sepal width (cm)
- Petal length (cm)
- Petal width (cm)
The goal is to classify the species of an iris flower based on these features.
The following exercise uses this well-known Iris dataset. Before getting started, please load the data using the following code:
from sklearn.datasets import load_iris
import numpy as np
iris = load_iris()
data = iris.data # shape: (150, 4)
target = iris.target # shape: (150,)
feature_names = iris.feature_names- Check the shape and data type of the array ‘data’?
print(data.shape)
print(data.dtype)
print(data[-5:])- Extract the features of the first and the fourth iris sample from data
# Extract the first and fourth samples
first_sample =
fourth_sample =
# Output the extracted features
print("Features of the first iris sample:", first_sample)
print("Features of the fourth iris sample:", fourth_sample)- Use NumPy functions to compute the max, min, mean, and standard deviation for each feature.
# Compute the max, min, mean, and standard deviation for each feature (column)
max_values =
min_values =
mean_values =
std_dev_values =
# Output the results
print("Max values for each feature:", max_values)
print("Min values for each feature:", min_values)
print("Mean values for each feature:", mean_values)
print("Standard deviation values for each feature:", std_dev_values)- Extract all samples(rows) from array ‘data’ where the ‘target’ is 0 (setosa), print the number of samples and save these data to a new array setosa. Do the same for the other two types Versicolor (target1) and Virginica (target2).
# Extract Setosa (target == 0)
setosa =
print(f"Number of Setosa samples: {setosa.shape[0]}")
# Extract Versicolor (target == 1)
versicolor =
print(f"Number of Versicolor samples: {versicolor.shape[0]}")
# Extract Virginica (target == 2)
virginica =
print(f"Number of Virginica samples: {virginica.shape[0]}")- Create a 1D boolean mask that is True where the ‘petal length’ (column index 2 in array ‘data’) is greater than 1.5.
# Create a boolean mask where petal length (column index 2) is greater than 1.5
mask =
# Output the mask
print("Boolean mask where petal length is greater than 1.5:")
print(mask)- Use the mask above to extract samples with petal length > 1.5
# Extract the samples where petal length > 1.5 using the mask
petal_length_gt_1_5 =
# Output the extracted samples
# print("Samples with petal length greater than 1.5:")
# print(petal_length_gt_1_5)
print(f"Number of samples with petal length > 1.5: {petal_length_gt_1_5.shape[0]}")- Find the largest petal is of which species (define what is largest petal by yourself) for exmple we can use the petal area, which is the product of petal length (column index 2) and petal width (column index 3). The largest petal can then be defined as the sample with the largest petal area.
# Compute the petal area (petal length * petal width) for each sample
petal_area =
# Find the index of the sample with the largest petal area
max_petal_area_idx =
# Extract the sample with the largest petal area
largest_petal_sample =
# Find the species of this sample
species =
# Output the results
print(f"The species with the largest petal area is: {species}")
print(f"Features of this sample:")
print(f"Sepal Length: {largest_petal_sample[0]}")
print(f"Sepal Width: {largest_petal_sample[1]}")
print(f"Petal Length: {largest_petal_sample[2]}")
print(f"Petal Width: {largest_petal_sample[3]}")