【发布时间】:2019-12-26 06:20:13
【问题描述】:
我想在文本上应用 k-Means 聚类。我已经安装了 sklearn 包,但仍然在其中发现错误...我现在在下面的代码中遇到问题...它在第 1 行显示语法无效(sklearn.feature_extraction.text)
from sklearn.feature_extraction.text
import TfidfVectorizer
from sklearn.cluster
import KMeans
from sklearn.metrics
import adjusted_rand_score
documents = ["This little kitty came to play when I was eating at a restaurant.",
"Merley has the best squooshy kitten belly.",
"Google Translate app is incredible.",
"If you open 100 tab in google you get a smiley face.",
"Best cat photo I've ever taken.",
"Climbing ninja cat.",
"Impressed with google map feedback.",
"Key promoter extension for Google Chrome."]
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(documents)
true_k = 2
model = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1)
model.fit(X)
print("Top terms per cluster:")
order_centroids = model.cluster_centers_.argsort()[:, ::-1]
terms = vectorizer.get_feature_names()
for i in range(true_k):
print("Cluster %d:" % i),
for ind in order_centroids[i, :10]:
print(' %s' % terms[ind]),
print
print("\n")
print("Prediction")
Y = vectorizer.transform(["chrome browser to open."])
prediction = model.predict(Y)
print(prediction)
Y = vectorizer.transform(["My cat is hungry."])
prediction = model.predict(Y)
print(prediction)
问题是 { "message": "无效语法(第 1 行)",
}
【问题讨论】:
标签: python machine-learning scikit-learn