You are on page 1of 12

1.

TÌM KIẾM CHIỀU RỘNG

2. TÌM KIẾM CHIỀU SÂU


3. THUẬT TOÁN KMEANS

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial.distance import cdist

means = [[2, 2], [9, 2], [4, 9]]


cov = [[2, 0], [0, 2]]
n_samples = 500
n_cluster = 3
X0 = np.random.multivariate_normal(means[0], cov, n_samples)
X1 = np.random.multivariate_normal(means[1], cov, n_samples)
X2 = np.random.multivariate_normal(means[2], cov, n_samples)
X = np.concatenate((X0, X1, X2), axis = 0)

plt.xlabel('x')
plt.ylabel('y')
plt.plot(X[:, 0], X[:, 1], 'bo', markersize=5)
plt.plot()
plt.show()

def kmeans_init_centers(X, n_cluster):

return X[np.random.choice(X.shape[0], n_cluster, replace=False)]


def kmeans_predict_labels(X, centers):
D = cdist(X, centers)

return np.argmin(D, axis = 1)


def kmeans_update_centers(X, labels, n_cluster):
centers = np.zeros((n_cluster, X.shape[1]))
for k in range(n_cluster):

Xk = X[labels == k, :]

centers[k,:] = np.mean(Xk, axis = 0)


return centers
def kmeans_has_converged(centers, new_centers):

return (set([tuple(a) for a in centers]) ==


set([tuple(a) for a in new_centers]))
def kmeans_visualize(X, centers, labels, n_cluster, title):
plt.xlabel('x')
plt.ylabel('y')
plt.title(title) # title của đồ thị
plt_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']

for i in range(n_cluster):
data = X[labels == i]
plt.plot(data[:, 0], data[:, 1], plt_colors[i] + '^', markersize = 4, label = 'cluster_' + str(i))
plt.plot(centers[i][0], centers[i][1], plt_colors[i+4] + 'o', markersize = 10, label = 'center_' +
str(i))
plt.legend()
plt.show()
def kmeans(init_centes, init_labels, X, n_cluster):
centers = init_centes
labels = init_labels
times = 0
while True:
labels = kmeans_predict_labels(X, centers)
kmeans_visualize(X, centers, labels, n_cluster, 'Assigned label for data at time = ' +
str(times + 1))
new_centers = kmeans_update_centers(X, labels, n_cluster)
if kmeans_has_converged(centers, new_centers):
break
centers = new_centers
kmeans_visualize(X, centers, labels, n_cluster, 'Update center possition at time = ' +
str(times + 1))
times += 1
return (centers, labels, times)

init_centers = kmeans_init_centers(X, n_cluster)


print(init_centers)
init_labels = np.zeros(X.shape[0])
kmeans_visualize(X, init_centers, init_labels, n_cluster, 'Init centers in the first run. Assigned
all data as cluster 0')
centers, labels, times = kmeans(init_centers, init_labels, X, n_cluster)

print('Done! Kmeans has converged after', times, 'times')


22/4 /2 02 2

1. Principal component analysis

PCA trong Python

Sử dụng thư viện prince


pip install prince

Load dữ liệu iris:


import pandas as pd
import prince
from sklearn import datasets

X, y = datasets.load_iris(return_X_y=True)
X = pd.DataFrame(data=X, columns=['Sepal length', 'Sepal width', 'Petal lengt
h', 'Petal width'])
y = pd.Series(y).map({0: 'Setosa', 1: 'Versicolor', 2: 'Virginica'})
X.head()
53

1. Principal component analysis

Dữ liệu trước và sau khi sử dụng PCA

pca = prince.PCA(
n_components=2,
n_iter=3,
rescale_with_mean=True,
rescale_with_std=True,
copy=True,
check_input=True,
engine='auto',
random_state=42
)
pca = pca.fit(X)
pca.transform(X).head()

27
54
22/4 /2 02 2

1. Principal component analysis


Plot 2 trục chính

ax = pca.plot_row_coordinates(
X,
ax=None,
figsize=(6, 6),
x_component=0,
y_component=1,
labels=None,
color_labels=y,
ellipse_outline=False,
ellipse_fill=True,
show_points=True
)
ax.get_figure()

Sau khi áp dụng PCA, dữ liệu “có vẻ”


được phân loại tốt hơn!!
55

1. Principal component analysis


Mức độ đóng góp của các trục chính

28
56
18/3 /2 02 2

BÀI TOÁN: TRÒ CHƠI 8 SỐ

Trạng thái đầu {1, 4, 3, 7, null, 6, 5, 8, 2}


Trạng thái đích {1, 2, 3, 4, 5, 6, 7, 8, null}
Toán tử:
Up
Down
Left
Right

Ghi chú:
Để code cài đặt, có thể đặt nút null có giá trị là 0

BÀI TOÁN: TRÒ CHƠI 8 SỐ

21
18/3 /2 02 2

BÀI TOÁN: TRÒ CHƠI 8 SỐ

BÀI TOÁN: TRÒ CHƠI 8 SỐ

22
18/3 /2 02 2

BÀI TOÁN: TRÒ CHƠI 8 SỐ

BÀI TOÁN: TRÒ CHƠI 8 SỐ

23
18/3 /2 02 2

BÀI TOÁN: TRÒ CHƠI 8 SỐ

BÀI TOÁN: TRÒ CHƠI 8 SỐ

24
18/3 /2 02 2

BÀI TOÁN: TRÒ CHƠI 8 SỐ

RUN

CHƯƠNG 3: TÌM KIẾM THEO


HEURISTIC
BÀI TẬP

1. Cây trò chơi và tìm kiếm trên cây trò chơi


2. Giải thuật minimax
3. Cắt tỉa alpha-beta

25
50

You might also like