python kmeans source code

### Mã nguồn Python K-Mean

Phân cụm K-MEAN là một thuật toán học tập không giám sát đơn giản nhưng mạnh mẽ có thể được sử dụng để tìm các mẫu trong dữ liệu.Nó thường được sử dụng để trực quan hóa và nén dữ liệu.Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách thực hiện phân cụm K-MEAN trong Python.

#### 1. Nhập các thư viện cần thiết

Để thực hiện phân cụm K-MEAN trong Python, chúng tôi sẽ cần nhập các thư viện sau:

*** Numpy: ** Thư viện này cung cấp hỗ trợ cho các mảng và ma trận đa chiều.
*** Scipy: ** Thư viện này cung cấp một số công cụ điện toán khoa học, bao gồm lớp `cluster.kmeans`.
*** matplotlib: ** Thư viện này cung cấp hỗ trợ cho việc vẽ dữ liệu.

#### 2. Tải dữ liệu

Bước đầu tiên là tải dữ liệu mà chúng tôi muốn phân cụm.Trong ví dụ này, chúng tôi sẽ sử dụng [Bộ dữ liệu Iris] (https://scikit-learn.org/stable/modules/datasets.html#iris-dataset).Bộ dữ liệu này chứa các phép đo chiều dài và chiều rộng cánh hoa và chiều rộng của ba loài hoa mống mắt.

Chúng tôi có thể tải dữ liệu bằng mã sau:

`` `Python
từ sklearn.datasets nhập load_iris

iris = load_iris ()
`` `

Điều này sẽ tạo một đối tượng `dataFrame 'chứa dữ liệu.Đối tượng `DataFrame` có các cột sau:

* `sepal_length`: chiều dài của sepal tính bằng centimet.
* `sepal_width`: chiều rộng của sepal tính bằng centimet.
* `Petal_length`: Chiều dài của cánh hoa tính bằng centimet.
* `Petal_width`: chiều rộng của cánh hoa tính bằng centimet.
* `loài`: loài hoa iris.

#### 3. Chọn số lượng cụm

Bước tiếp theo là chọn số lượng cụm mà chúng tôi muốn tạo.Điều này có thể được thực hiện bằng cách dùng thử và lỗi, hoặc bằng cách sử dụng một kỹ thuật như [phương pháp khuỷu tay] (https://scikit-learn.org/stable/modules/clustering.html#k-means).

Trong ví dụ này, chúng tôi sẽ sử dụng phương pháp khuỷu tay để chọn số lượng cụm.Để làm điều này, chúng tôi sẽ vẽ tổng số các lỗi bình phương (SSE) cho các giá trị khác nhau của `k`.`SSE` là thước đo về mức độ dữ liệu được phân cụm.

Chúng ta có thể vẽ SSE bằng mã sau:

`` `Python
từ sklearn.cluster nhập kmeans

SSE = []
Đối với k trong phạm vi (1, 11):
kmeans = kmeans (n_cluster = k)
kmeans.fit (iris.data)
SSE.Append (kmeans.inertia_)

plt.plot (phạm vi (1, 11), SSE)
plt.xlabel ('số cụm')
plt.ylabel ('sse')
plt.show ()
`` `

Cốt truyện cho thấy SSE giảm khi số lượng cụm tăng.Tuy nhiên, việc giảm mức SSE tắt sau khoảng 3 cụm.Điều này cho thấy 3 là một số lượng tốt các cụm cho dữ liệu này.

#### 4. Huấn luyện mô hình K-Means

Bây giờ chúng tôi đã chọn số lượng cụm, chúng tôi có thể đào tạo mô hình K-Means.Để làm điều này, chúng ta có thể sử dụng mã sau:

`` `Python
kmeans = kmeans (n_cluster = 3)
kmeans.fit (iris.data)
`` `

Điều này sẽ đào tạo mô hình K-MEAN trên dữ liệu.Mô hình sẽ tìm hiểu các trung tâm của các cụm.

#### 5. Dự đoán nhãn cụm

Khi mô hình được đào tạo, chúng ta có thể dự đoán các nhãn cụm cho dữ liệu.Để làm điều này, chúng ta có thể sử dụng mã sau:

`` `Python
Labels = kmeans.predict (iris.data)
`` `

Điều này sẽ trả về một mảng `numpy` chứa các nhãn cụm cho mỗi điểm dữ liệu.

#### 6. Hình dung các cụm

Chúng ta có thể trực quan hóa các cụm bằng mã sau:

`` `Python
plt.scatter (iris.data [:, 0], iris.data [:, 1], c = nhãn
=======================================
### Python K-Means Source Code

K-means clustering is a simple yet powerful unsupervised learning algorithm that can be used to find patterns in data. It is often used for data visualization and compression. In this article, we will show you how to implement K-means clustering in Python.

#### 1. Import the necessary libraries

To implement K-means clustering in Python, we will need to import the following libraries:

* **numpy:** This library provides support for multidimensional arrays and matrices.
* **scipy:** This library provides a number of scientific computing tools, including the `cluster.KMeans` class.
* **matplotlib:** This library provides support for plotting data.

#### 2. Load the data

The first step is to load the data that we want to cluster. In this example, we will use the [Iris dataset](https://scikit-learn.org/stable/modules/datasets.html#iris-dataset). This dataset contains measurements of the sepal and petal length and width of three species of iris flowers.

We can load the data using the following code:

```python
from sklearn.datasets import load_iris

iris = load_iris()
```

This will create a `DataFrame` object containing the data. The `DataFrame` object has the following columns:

* `sepal_length`: The length of the sepal in centimeters.
* `sepal_width`: The width of the sepal in centimeters.
* `petal_length`: The length of the petal in centimeters.
* `petal_width`: The width of the petal in centimeters.
* `species`: The species of iris flower.

#### 3. Choose the number of clusters

The next step is to choose the number of clusters that we want to create. This can be done by trial and error, or by using a technique such as the [elbow method](https://scikit-learn.org/stable/modules/clustering.html#k-means).

In this example, we will use the elbow method to choose the number of clusters. To do this, we will plot the sum of squared errors (SSE) for different values of `k`. The `SSE` is a measure of how well the data is clustered.

We can plot the SSE using the following code:

```python
from sklearn.cluster import KMeans

sse = []
for k in range(1, 11):
kmeans = KMeans(n_clusters=k)
kmeans.fit(iris.data)
sse.append(kmeans.inertia_)

plt.plot(range(1, 11), sse)
plt.xlabel('Number of clusters')
plt.ylabel('SSE')
plt.show()
```

The plot shows that the SSE decreases as the number of clusters increases. However, the decrease in SSE levels off after around 3 clusters. This suggests that 3 is a good number of clusters for this data.

#### 4. Train the K-means model

Now that we have chosen the number of clusters, we can train the K-means model. To do this, we can use the following code:

```python
kmeans = KMeans(n_clusters=3)
kmeans.fit(iris.data)
```

This will train the K-means model on the data. The model will learn the centroids of the clusters.

#### 5. Predict the cluster labels

Once the model is trained, we can predict the cluster labels for the data. To do this, we can use the following code:

```python
labels = kmeans.predict(iris.data)
```

This will return a `numpy` array containing the cluster labels for each data point.

#### 6. Visualize the clusters

We can visualize the clusters using the following code:

```python
plt.scatter(iris.data[:, 0], iris.data[:, 1], c=labels
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock