当前位置: 首页>>代码示例>>Python>>正文


Python MiniBatchKMeans.set_params方法代码示例

本文整理汇总了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)
开发者ID:guidefreitas,项目名称:bag_of_visual_words,代码行数:43,代码来源:kmeans.py

示例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)
开发者ID:xjdeng,项目名称:bag-of-words-image-classifier,代码行数:70,代码来源:classifier.py


注:本文中的sklearn.cluster.MiniBatchKMeans.set_params方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。