本文整理汇总了Python中sklearn.cluster.MiniBatchKMeans.get_params方法的典型用法代码示例。如果您正苦于以下问题:Python MiniBatchKMeans.get_params方法的具体用法?Python MiniBatchKMeans.get_params怎么用?Python MiniBatchKMeans.get_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.MiniBatchKMeans
的用法示例。
在下文中一共展示了MiniBatchKMeans.get_params方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: applyKmeansMiniBatch
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import get_params [as 别名]
def applyKmeansMiniBatch(self, number_of_clusters, country_specific_tweets):
train, feature_names = self.extractFeatures(country_specific_tweets,False)
name = "kmeans"
if self.results:
print("Performing dimensionality reduction using LSA")
t0 = time()
# Vectorizer results are normalized, which makes KMeans behave as
# spherical k-means for better results. Since LSA/SVD results are
# not normalized, we have to redo the normalization.
#svd = TruncatedSVD(len(feature_names)-1)
#normalizer = Normalizer(copy=False)
#lsa = make_pipeline(svd, normalizer)
#print(train.toarray())
#train = lsa.fit_transform(train)
if self.results:
print("done in %fs" % (time() - t0))
# explained_variance = svd.explained_variance_ratio_.sum()
#print("Explained variance of the SVD step: {}%".format(int(explained_variance * 100)))
if self.results:
print("Clustering sparse data", end=" - ")
t0 = time()
km = MiniBatchKMeans(n_clusters=number_of_clusters, init='k-means++',init_size=1000,batch_size=1000, verbose=False)
km.fit(train)
if self.results:
print("done in %0.3fs" % (time() - t0))
parameters = km.get_params()
labels = km.labels_
# without SVD:
order_centroids = km.cluster_centers_.argsort()[:, ::-1]
# with SVD:
#original_space_centroids = svd.inverse_transform(km.cluster_centers_)
#order_centroids = original_space_centroids.argsort()[:, ::-1]
top10 = self.printKMeansCluster(number_of_clusters, labels, order_centroids, feature_names)
if self.results:
print("Silhouette Coefficient {0}: {1}".format(name, metrics.silhouette_score(train, labels)))
return name, parameters, top10, labels
示例2: __init__
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import get_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)
示例3: train_codebook
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import get_params [as 别名]
def train_codebook(nclusters,normalized_descriptors):
km = MiniBatchKMeans(nclusters)
km.fit(normalized_descriptors)
i = km.get_params() #carreguem els params del vector k
nc = i['n_clusters'] #obtenim el numero de clusters des de la clase minibatchkmeans
return km, nc
示例4: __init__
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import get_params [as 别名]
class FeatureGen:
max_cat_records = 12499
max_dog_records = 12499
kmeans = None
n_clusters = 0
sift = None
def __init__(self):
#self.kmeans = KMeans(precompute_distances=False)
self.kmeans = MiniBatchKMeans()
self.sift = cv2.SIFT()
self.n_clusters = self.kmeans.get_params()["n_clusters"]
print self.n_clusters
def prepareTrainTestTuples(self):
file = open("train_tuples","w")
for idx in range(0,self.max_cat_records):
print "processing cat."+str(idx)+".jpg\n"
img = cv2.imread('train/train/cat.'+str(idx)+'.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kp = self.sift.detect(gray,None)
tkp, td = self.sift.compute(gray, kp)
temp_points = []
for k in tkp:
tuples = (int(math.ceil(k.pt[0])),int(math.ceil(k.pt[1])))
temp_points.append(tuples)
file.write(json.dumps(temp_points)+"\n")
for idx in range(0,self.max_dog_records):
print "processing dog."+str(idx)+".jpg\n"
img = cv2.imread('train/train/dog.'+str(idx)+'.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kp = self.sift.detect(gray,None)
tkp, td = self.sift.compute(gray, kp)
temp_points = []
for k in tkp:
tuples = (int(math.ceil(k.pt[0])),int(math.ceil(k.pt[1])))
temp_points.append(tuples)
file.write(json.dumps(temp_points)+"\n")
file.close()
'''
file = open("test_tuples","w")
for idx in range(1,12501):
print "processing "+str(idx)+".jpg\n"
img = cv2.imread('test1/test1/'+str(idx)+'.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kp = self.sift.detect(gray,None)
tkp, td = self.sift.compute(gray, kp)
temp_points = []
for k in tkp:
tuples = (int(math.ceil(k.pt[0])),int(math.ceil(k.pt[1])))
temp_points.append(tuples)
file.write(json.dumps(temp_points)+
'''
def getSIFTTrainFeatures(self):
print "refined version"
points = []
loaded_feats = []
file = open("train_tuples","r")
lines = file.readlines()
count=1
for line in lines:
if count >=10000 and count <=12500:
print "continue"+str(count)
count = count+1
continue
elif count == 24000:
break
print count
feat_vals = json.loads(line)
loaded_feats.append(feat_vals)
for feat in feat_vals:
points.append(feat)
count = count+1
print count
self.kmeans = self.kmeans.fit(points)
overall_feats = []
count = 1
for feat in loaded_feats:
print "Record-->"+str(count)
clusters = self.kmeans.predict(feat)
print clusters
feats = []
for i in range(0,self.n_clusters):
feats.append(0)
if count <10000:
#if count<self.max_cat_records:
feats.append(0)
else:
feats.append(1)
for num in clusters:
feats[num] = feats[num]+1
overall_feats.append(feats)
count = count+1
return overall_feats
'''
def getSIFTTrainFeatures(self):
print "Enter train"
points = []
loaded_feats = []
file = open("train_tuples","r")
lines = file.readlines()
#.........这里部分代码省略.........
示例5: len
# 需要导入模块: from sklearn.cluster import MiniBatchKMeans [as 别名]
# 或者: from sklearn.cluster.MiniBatchKMeans import get_params [as 别名]
num_kps[img_idx] = len(kp)
#stack descriptors for all training images
if (img_idx == 0):
des_tot = des
else:
des_tot = np.vstack((des_tot, des))
#end for
#cluster images into a dictionary
dictionary_size = 100
kmeans = MiniBatchKMeans(n_clusters = dictionary_size, init = 'k-means++', batch_size = 5000, random_state = 0, verbose=0)
tic = time()
kmeans.fit(des_tot)
toc = time()
kmeans.get_params()
print "K-means objective: %.2f" %kmeans.inertia_
print "elapsed time: %.4f sec" %(toc - tic)
kmeans.cluster_centers_
labels = kmeans.labels_
#PCA plot of kmeans_cluster centers
pca = PCA(n_components=2)
visual_words = pca.fit_transform(kmeans.cluster_centers_)
plt.figure()
plt.scatter(visual_words[:,0], visual_words[:,1], color='b', marker='o', lw = 2.0, label='Olivetti visual words')
plt.title("Visual Words (PCA of cluster centers)")
plt.xlabel("PC1")
plt.ylabel("PC2")