【问题标题】:Python Shape function for k-means clustering用于 k-means 聚类的 Python Shape 函数
【发布时间】:2018-08-31 15:27:56
【问题描述】:

我有一张 geotiff 灰度图像,它给了我 (4377, 6172) 二维数组。在第一部分中,我正在为我的压缩算法考虑 (:1024, :1024) 值(总值为 -> 1024 * 1024 = 1048576)。通过该算法,我通过该算法在 finalmatrix list var 中获得了总共 4 个值。在此之后,我对这些值应用 K-means 算法。下面是一个程序:

import numpy as np
from osgeo import gdal
from sklearn import cluster
import matplotlib.pyplot as plt

dataset =gdal.Open("1.tif")
band = dataset.GetRasterBand(1)
img = band.ReadAsArray()

finalmat = [255, 0, 2, 2]

#Converting list to array for dimensional change
ay = np.asarray(finalmat).reshape(-1,1)    

fig = plt.figure()

k_means = cluster.KMeans(n_clusters=2)
k_means.fit(ay)

cluster_means = k_means.cluster_centers_.squeeze()
a_clustered = k_means.labels_

print('# of observation :',ay.shape)

print('Cluster Means : ', cluster_means)

a_clustered.shape= img.shape

fig=plt.figure(figsize=(125,125))

ax = plt.subplot(2,4,8)
plt.axis('off')
xlabel = str(1) , ' clusters'
ax.set_title(xlabel)
plt.imshow(a_clustered)
    
plt.show()
fig.savefig('kmeans-1 clust ndvi08jan2010_guj 12 .png')    
    

在上述程序中,a_clustered.shape= img.shape 行出现错误。我得到的错误如下:

Error line:  

a_clustered.shape= img.shape

ValueError: cannot reshape array of size 4 into shape (4377,6172)

<matplotlib.figure.Figure at 0x7fb7c63975c0>

实际上,我想通过我得到的压缩值来可视化原始图像上的聚类。可以给点建议吗

【问题讨论】:

    标签: python-3.x cluster-analysis geotiff


    【解决方案1】:

    对一维数据使用 KMeans 没有多大意义。

    在 4 x 1 数组上使用它更没有意义!

    因此,您的网站源于您不能将 4 x 1 整数数组的大小调整为大图片。

    只需打印您要绘制的数组a_clustered。它可能包含[0, 1, 1, 1]

    【讨论】:

      猜你喜欢
      • 2020-10-27
      • 2016-02-01
      • 2018-06-21
      • 2016-08-14
      • 2017-03-16
      • 2015-04-11
      • 2021-08-06
      • 2018-02-26
      • 2011-08-13
      相关资源
      最近更新 更多