import numpy as np from sklearn.cluster import KMeans # 数据准备 data = np.array([ [100, 50, 20, 30, 80, 40, 10, 70], # 辽宁省的数据 [200, 150, 80, 60, 120, 100, 30, 90], # 浙江省的数据 [80, 60, 10, 20, 40, 30, 5, 50], # 河南省的数据 [50, 30, 5, 10, 25, 15, 3, 20], # 甘肃省的数据 [120, 80, 30, 40, 60, 50, 15, 40] # 青海省的数据 ]) # 数据标准化(可选,根据需要进行标准化处理) # data = (data - data.mean(axis=0)) / data.std(axis=0) # 使用K均值聚类算法进行分类 k = 5 # 分类的数量 kmeans = KMeans(n_clusters=k, random_state=0) labels = kmeans.fit_predict(data) # 打印分类结果 provinces = ["辽宁", "浙江", "河南", "甘肃", "青海"] for i, label in enumerate(labels): print(f"{provinces[i]}: 类别 {label}")生成相关图示
时间: 2023-07-02 14:08:33 浏览: 67
这段代码可以用来对省份数据进行 K 均值聚类,将省份划分为 k 个类别,并打印出每个省份所属的类别。
下面是一个简单的可视化示例,使用 matplotlib 库绘制一个散点图,不同颜色的点表示不同的类别,点的大小表示该省份的总数据量。代码如下:
```python
import matplotlib.pyplot as plt
# 绘制散点图
plt.scatter(data[:, 0], data[:, 1], s=data.sum(axis=1), c=labels)
plt.xlabel("数据1")
plt.ylabel("数据2")
plt.show()
```
其中,`data[:, 0]` 和 `data[:, 1]` 分别表示数据中的两个维度,`s=data.sum(axis=1)` 表示点的大小为该省份数据量的总和,`c=labels` 表示不同的类别用不同的颜色表示。
你可以根据需要修改代码,比如修改数据维度、调整点的大小和颜色等,以获得更好的可视化效果。
相关问题
import numpy as np from sklearn.cluster import KMeans # 数据准备 data = np.array([ [100, 50, 20, 30, 80, 40, 10, 70], # 辽宁省的数据 [200, 150, 80, 60, 120, 100, 30, 90], # 浙江省的数据 [80, 60, 10, 20, 40, 30, 5, 50], # 河南省的数据 [50, 30, 5, 10, 25, 15, 3, 20], # 甘肃省的数据 [120, 80, 30, 40, 60, 50, 15, 40] # 青海省的数据 ]) # 数据标准化(可选,根据需要进行标准化处理) # data = (data - data.mean(axis=0)) / data.std(axis=0) # 使用K均值聚类算法进行分类 k = 5 # 分类的数量 kmeans = KMeans(n_clusters=k, random_state=0) labels = kmeans.fit_predict(data) # 打印分类结果 provinces = ["辽宁", "浙江", "河南", "甘肃", "青海"] for i, label in enumerate(labels): print(f"{provinces[i]}: 类别 {label}")生成相关绘制图示的步骤
这段代码是使用K均值聚类算法对5个省份的数据进行分类,并打印出每个省份所属的类别。如果你想要生成相关绘制图示,可以考虑使用matplotlib库进行可视化展示。具体步骤如下:
1.导入matplotlib库
import matplotlib.pyplot as plt
2.将每个省份的数据点用不同颜色标记出来
colors = ['red', 'green', 'blue', 'yellow', 'purple']
for i, label in enumerate(labels):
plt.scatter(data[i,0], data[i,1], c=colors[label])
3.设置图表标题和坐标轴标签
plt.title('Provinces Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
4.展示图表
plt.show()
完整代码如下:
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
data = np.array([
[100, 50, 20, 30, 80, 40, 10, 70],
[200, 150, 80, 60, 120, 100, 30, 90],
[80, 60, 10, 20, 40, 30, 5, 50],
[50, 30, 5, 10, 25, 15, 3, 20],
[120, 80, 30, 40, 60, 50, 15, 40]
])
k = 5
kmeans = KMeans(n_clusters=k, random_state=0)
labels = kmeans.fit_predict(data)
provinces = ["辽宁", "浙江", "河南", "甘肃", "青海"]
colors = ['red', 'green', 'blue', 'yellow', 'purple']
for i, label in enumerate(labels):
plt.scatter(data[i,0], data[i,1], c=colors[label])
plt.title('Provinces Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()
import numpy as np from sklearn.cluster import KMeans def compress_image(image, n_clusters): # 转换为一维向量 data = image.reshape(-1, 3) # 用 KMeans 算法将像素点聚类到 n_clusters 个簇中 kmeans = KMeans(n_clusters=5, random_state=0).fit(data) # 将每个像素点替换为所属簇的中心像素值 compressed_data = np.array([kmeans.cluster_centers_[label] for label in kmeans.labels_]) # 将压缩后的一维向量转换回原图像的形状 compressed_image = compressed_data.reshape(image.shape) return compressed_imageimport matplotlib.pyplot as plt from PIL import Image # 读取图像 image = np.array(Image.open('0.jpg')) # 将图像数据归一化到 [0, 1] 范围内 image = image.astype('float32') / 255.0 # 压缩图像 compressed_image = compress_image(image_norm, n_clusters=16) # 显示压缩前后的图像 fig, ax = plt.subplots(1, 2, figsize=(10, 5)) ax[0].imshow(image) ax[0].set_title('Original Image') ax[1].imshow(compressed_image) ax[1].set_title('Compressed Image') plt.show()加上计算压缩率的功能
可以在 compress_image 函数中添加计算压缩率的功能,具体实现如下:
```
import numpy as np
from sklearn.cluster import KMeans
def compress_image(image, n_clusters):
# 转换为一维向量
data = image.reshape(-1, 3)
# 用 KMeans 算法将像素点聚类到 n_clusters 个簇中
kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(data)
# 将每个像素点替换为所属簇的中心像素值
compressed_data = np.array([kmeans.cluster_centers_[label] for label in kmeans.labels_])
# 将压缩后的一维向量转换回原图像的形状
compressed_image = compressed_data.reshape(image.shape)
# 计算压缩率
compressed_size = compressed_data.nbytes
original_size = image.nbytes
compression_ratio = compressed_size / original_size
return compressed_image, compression_ratio
import matplotlib.pyplot as plt
from PIL import Image
# 读取图像
image = np.array(Image.open('0.jpg'))
# 将图像数据归一化到 [0, 1] 范围内
image = image.astype('float32') / 255.0
# 压缩图像
n_clusters = 5
compressed_image, compression_ratio = compress_image(image, n_clusters)
# 显示压缩前后的图像
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(image)
ax[0].set_title('Original Image')
ax[1].imshow(compressed_image)
ax[1].set_title('Compressed Image')
# 显示压缩率
plt.figtext(0.5, 0.9, f'Compression Ratio: {compression_ratio:.2%}', ha='center', fontsize=12)
plt.show()
```
在上述代码中,计算压缩率的代码为:
```
compressed_size = compressed_data.nbytes
original_size = image.nbytes
compression_ratio = compressed_size / original_size
```
其中,`compressed_data.nbytes` 表示压缩后的图像数据的字节数,`image.nbytes` 表示原始图像数据的字节数,压缩率即为两者之比。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)