Professional Documents
Culture Documents
import heapq
class State:
self.water_amounts = water_amounts
self.path_cost = path_cost
def __lt__(self, other):
initial_state = State([0] * n, 0)
frontier = []
heapq.heappush(frontier, initial_state)
explored = set()
while frontier:
current_state = heapq.heappop(frontier)
if sum(current_state.water_amounts) == M:
return current_state.water_amounts
explored.add(tuple(current_state.water_amounts))
for i in range(n):
new_water_amounts = current_state.water_amounts.copy()
if action == 'pour_in':
new_water_amounts[i] = water_capacities[i]
if new_water_amounts[i] > 0:
new_water_amounts[i] = 0
new_path_cost = current_state.path_cost + 1
heapq.heappush(frontier, new_state)
return None
# Trả lời: Dán kết quả thực thi với dữ liệu Nhập: “3 13 7 8 9” vào bên dưới (1 điểm)
# Trả lời: Hãy giải thích hàm h’ (hàm khoảng cách trong thuật toán A* ở chương trình trên. (0.5 điểm)
Câu 2 (4 điểm): Cho tập dữ liệu input.csv với 90 mẫu dữ liệu, mỗi mẫu có 4 đặc trưng ( chiều dài đài hoa, chiều
rộng đài hoa, chiều dài cánh hoa, chiều rộng cánh hoa) và tên loài hoa tương ứng.
a) (3 điểm) Hãy viết chương trình phân loại hoa sử dụng Logistic Regression kết hợp với lớp softmax. Nêu
rõ mô hình thức phân loại trong chương trình như thế nào (Ví dụ: có bao nhiêu tế bào nơ-ron, mỗi nơ-
ron phụ trách công việc gì, làm sao để phân loại,…)?
# Trả lời: Dán code vào bên dưới
import numpy as np
import pandas as pd
z_exp = np.exp(z)
def one_hot(y):
n_classes = len(np.unique(y))
one_hot_y[np.arange(len(y)), y] = 1
return one_hot_y
w = np.zeros((X.shape[1], y.shape[1]))
for i inrange(n_iter):
y_pred = softmax(X.dot(w))
# Tính gradient
w -= lr * grad
return w
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
le = LabelEncoder()
y = le.fit_transform(y)
y = one_hot(y)
w = logistic_regression(X, y)
# Trả lời: Mô tả mô hình phân loại bằng hình ảnh hoặc bằng lời.
X1,x2,x3,x4: thông tin đầu vào của 1 mẫu gồm 4 đặc trưng :
Mô hình phân loại chỉ gồm input layer và output layer. Output layer chỉ có 1 tế bào neuron và hàm
activation function softmax của nó là hàm sigmoid.
Lặp:
Y^=sigmoid(b+w1*x1+w2*x2+w3*x3+……)
Phân loại
b) (1 điểm) Hãy thực thi chương trình và cho biết nhãn của 60 mẫu dữ liệu trong output.csv
# Trả lời: Dán code thực thi thành công
X_test = data.values
predicted_labels = le.inverse_transform(predictions)
print(f'{i+1}: {label}')
# Trả lời: Dán kết quả nhãn ứng với 60 mẫu dữ liệu
1: Iris-setosa
2: Iris-setosa
3: Iris-setosa
4: Iris-setosa
5: Iris-setosa
6: Iris-setosa
7: Iris-setosa
8: Iris-setosa
9: Iris-setosa
10: Iris-setosa
11: Iris-setosa
12: Iris-setosa
13: Iris-setosa
14: Iris-setosa
15: Iris-setosa
16: Iris-setosa
17: Iris-setosa
18: Iris-setosa
19: Iris-setosa
20: Iris-setosa
21: Iris-versicolor
22: Iris-versicolor
23: Iris-versicolor
24: Iris-versicolor
25: Iris-versicolor
26: Iris-versicolor
27: Iris-versicolor
28: Iris-versicolor
29: Iris-versicolor
30: Iris-versicolor
31: Iris-versicolor
32: Iris-versicolor
33: Iris-versicolor
34: Iris-versicolor
35: Iris-versicolor
36: Iris-versicolor
37: Iris-versicolor
38: Iris-versicolor
39: Iris-versicolor
40: Iris-versicolor
41: Iris-virginica
42: Iris-virginica
43: Iris-virginica
44: Iris-virginica
45: Iris-virginica
46: Iris-virginica
47: Iris-virginica
48: Iris-virginica
49: Iris-virginica
50: Iris-virginica
51: Iris-virginica
52: Iris-virginica
53: Iris-virginica
54: Iris-virginica
55: Iris-virginica
56: Iris-virginica
57: Iris-virginica
58: Iris-virginica
59: Iris-virginica
60: Iris-virginica
Câu 3 (3 điểm): Cho tập dữ liệu input.csv với 90 mẫu dữ liệu như câu 2, Hãy viết chương trình phân cụm bằng
thuật toán k-means
a) (1 điểm) Viết hàm thực thi thuật toán k-means
import pandas as pd
import numpy as np
b) (2 điểm) Nếu sử dụng thuật toán k-means với k = 3 thì kết quả phân nhóm sẽ như thế nào? (Trọng tâm
của các cụm, tỷ lệ phân cụm đúng, tiêu chí đánh giá việc phân cụm đúng là gì?).
(đã duyệt)