本文整理汇总了Python中sklearn.cluster.KMeans.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python KMeans.reshape方法的具体用法?Python KMeans.reshape怎么用?Python KMeans.reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.KMeans
的用法示例。
在下文中一共展示了KMeans.reshape方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: kmeansRemovingOutlierForClassifier
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import reshape [as 别名]
def kmeansRemovingOutlierForClassifier():
"""
use k-means to do outlier removal
:return: NA
"""
# load data
X_train = np.load('inputClf_small/X_train.npy')
y_train = np.load('inputClf_small/y_train.npy')
y_train_price = np.load('inputClf_small/y_train_price.npy')
# cluster initializing
X_train1 = X_train[np.where(y_train==0)[0], :]
X_train2 = X_train[np.where(y_train==1)[0], :]
cluster1 = KMeans(init='random', n_clusters=1, random_state=0).fit(X_train1)
cluster1 = cluster1.cluster_centers_
cluster2 = KMeans(init='random', n_clusters=1, random_state=0).fit(X_train2)
cluster2 = cluster2.cluster_centers_
clusters = np.concatenate((cluster1, cluster2), axis=0)
y_pred = KMeans(init='random', n_clusters=2, random_state=2).fit_predict(X_train)
y_pred = y_pred.reshape((y_pred.shape[0], 1))
y_pred = y_pred
tmp = np.concatenate((y_train, y_pred), axis=1)
sam = y_train == y_pred
print "# total: {}".format(y_train.shape[0])
print "# datas left: {}".format(np.sum(sam))
# Keep 63.62% data.
print "Keep {}% data.".format(round(np.sum(sam)*100.0/y_train.shape[0], 2))
print tmp[0:22, :]
print np.where(y_train==y_pred)[0]
# keep the data which are not outliers
X_train = X_train[np.where(y_train==y_pred)[0], :]
y_train_price = y_train_price[np.where(y_train==y_pred)[0], :]
y_train = y_train[np.where(y_train==y_pred)[0], :]
np.save('inputClf_KMeansOutlierRemoval/X_train', X_train)
np.save('inputClf_KMeansOutlierRemoval/y_train', y_train)
np.save('inputClf_KMeansOutlierRemoval/y_train_price', y_train_price)
示例2: KMeans
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import reshape [as 别名]
colors = [[0, 0, 0],
[0, 0, 255],
[0, 255, 0],
[255, 0, 0],
[255, 255, 0],
[255, 0, 255],
[0, 255, 255]]
filename = sys.argv[1]
original = io.imread(filename)
if original.shape[0] % 2 != 0 or original.shape[1] % 2 != 0:
original = tfm.resize(original, [original.shape[0]//2*2, original.shape[1]//2*2, original.shape[2]])
io.imsave(filename, original)
#downscale = 2
img = original#downscale(original)
arr = np.reshape(img, [-1, 3])
labels = KMeans(n_clusters = 8).fit_predict(arr)
labels = labels.reshape((img.shape[0], -1))
labels *= 32
#labels = upscale(labels)
io.imsave("{}_sem.png".format(filename[:-4]), labels)
示例3: loadData
# 需要导入模块: from sklearn.cluster import KMeans [as 别名]
# 或者: from sklearn.cluster.KMeans import reshape [as 别名]
import numpy as np
import PIL.Image as image
from sklearn.cluster import KMeans
def loadData(filePath):
f = open(filePath,'rb')
data = []
img = image.open(f)
m,n = img.size
for i in range(m):
for j in range(n):
x,y,z = img.getpixel((i,j))
data.append([x/256.0,y/256.0,z/256.0])
f.close()
return np.mat(data),m,n
imgData,row,col = loadData('starbucks.jpg')
label = KMeans(n_clusters=4).fit_predict(imgData)
label = label.reshape([row,col])
pic_new = image.new("L", (row, col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i,j), int(256/(label[i][j]+1)))
pic_new.save("result-bull-4.jpg", "JPEG")