本文整理汇总了Python中sklearn.cluster.MiniBatchKMeans.set_params方法的典型用法代码示例。如果您正苦于以下问题:Python MiniBatchKMeans.set_params方法的具体用法?Python MiniBatchKMeans.set_params怎么用?Python MiniBatchKMeans.set_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.MiniBatchKMeans
的用法示例。
在下文中一共展示了MiniBatchKMeans.set_params方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import set_params [as 别名]
class Kmeans:
kmeans_batch_size = 45
kmeans = None
k = None
def __init__(self, k=10, centers=None):
self.k = k
if(centers!=None):
init_centers = centers
else:
init_centers = 'k-means++'
self.kmeans = MiniBatchKMeans(init=init_centers, n_clusters=self.k, batch_size=self.kmeans_batch_size,
n_init=10, max_no_improvement=10, verbose=0)
def fit(self, X):
self.kmeans.fit(X)
def partial_fit(self, X):
self.kmeans.partial_fit(X)
def predict(self, X):
return self.kmeans.predict(X)
def get_centers(self):
return self.kmeans.cluster_centers_
def set_centers(self, centers):
self.kmeans.cluster_centers_ = centers
def predict_hist(self, X):
labels = self.predict(X)
bins = range(self.k)
histogram = np.histogram(labels, bins=bins, density=True)[0]
#histogram = histogram/X.shape[0]
return histogram
def get_params(self):
return self.kmeans.get_params(deep=True)
def set_params(self, kmeans_params):
self.kmeans.set_params(**kmeans_params)
示例2: classifier
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import set_params [as 别名]
#.........这里部分代码省略.........
self.PCA_common()
# Performs Incremental PCA when testing images.
def PCA_test(self):
self.PCA_common()
# Helper function for Incremental PCA calling the procedures common between both training and testing.
def PCA_common(self):
self.descriptors = self.pca.transform(self.descriptors)
if self.pca_before_kmeans:
tmp = []
for i in self.des_list:
tmp.append(self.pca.transform(i))
self.des_list = tmp
# Performs Mini Batch K-Means when training images. Sets up the K-Means environment if it hasn't already been set up.
def KMeans_train(self):
clusterfun = None
special = False
newclusters = self.kclusters
if self.kmeans is None:
if self.descriptors.shape[0] < self.kclusters:
special = True
newclusters = self.descriptors.shape[0]
self.kmeans = MiniBatchKMeans(n_clusters = newclusters)
clusterfun = self.kmeans.fit
else:
clusterfun = self.kmeans.partial_fit
clusterfun(self.descriptors)
if special:
self.kmeans.set_params(n_clusters = self.kclusters)
self.KMeans_common()
# Performs Mini Batch K-Means when testing images.
def KMeans_test(self):
self.KMeans_common()
# Helper function for Mini Batch K-Means calling the procedures common between both training and testing. Also generates the histogram of image features.
def KMeans_common(self):
n = len(self.des_list)
im_features = np.zeros((n, self.kclusters), "float32")
for i in xrange(n):
words = self.kmeans.predict(self.des_list[i])
for w in words:
im_features[i][w] += 1
if self.tfidf is True:
im_features = self.tf_idf(im_features)
stdSlr = StandardScaler().fit(im_features)
im_features = stdSlr.transform(im_features)
self.descriptors = im_features
# Helper function that extracts the descriptors and correct image classes from a list of images and stores them in temporary variables in the clasifier
# for later processing.
def getDescriptors(self, imglist):
image_classes = []
des_list = []
for i in imglist:
img = i.data
if self.convert_grey:
img = self.convertGrey(img)