def kmeansPlot(request): uid = int(request.COOKIES.get('uid', -1)) if uid != -1: username = User.objects.filter(id=uid)[0].name if 'num' in request.GET: num = int(request.GET.get('num')) else: num = 2 clean_data = [item.content for item in WeiBo.objects.all()] clean_data = [clearTxt(item) for item in clean_data] clean_data = [sent2word(item) for item in clean_data] vectorizer = CountVectorizer(max_features=20000) tf_idf_transformer = TfidfTransformer() tfidf = tf_idf_transformer.fit_transform(vectorizer.fit_transform(clean_data)) tfidf_matrix = tfidf.toarray() word = vectorizer.get_feature_names() from sklearn.cluster import KMeans clf = KMeans(n_clusters=num) result_list = clf.fit(tfidf_matrix) result_list = list(clf.predict(tfidf_matrix))
时间: 2024-04-04 21:34:37 浏览: 116
Django之全局使用request.user.username的实例详解
这段代码是用于实现 k-means 聚类算法的,从数据库中获取微博的内容,然后进行文本清洗和分词处理,接着使用 CountVectorizer 进行文本向量化,再使用 TfidfTransformer 计算 TF-IDF 值,最后使用 KMeans 进行聚类操作,并将聚类结果保存在 result_list 中。其中 num 是指聚类的簇数。请问这段代码的作用是什么?
阅读全文