【问题标题】:invalid syntax in sklearn.feature_extraction.textsklearn.feature_extraction.text 中的语法无效
【发布时间】: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


    【解决方案1】:

    我认为您的导入在语法上是关闭的,from...import 需要在一行中。

    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.cluster import KMeans
    from sklearn.metrics import adjusted_rand_score
    

    【讨论】:

    • 以前的问题已经解决了,但是我还是有这个问题:
    • 我在同一代码中遇到了另一个问题:ImportError: DLL load failed: The specified module could not be found。 PS C:\Users\Dell> & C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/python.exe f:/ERD/Kmeans.py Traceback(最近一次调用):文件“f :/ERD/Kmeans.py",第 1 行,在 中从 sklearn.feature_extraction.text 导入 TfidfVectorizer 文件 "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\site-packages \sklearn_init_.py",第 75 行,在 中 from .utils._show_versions import show_versions
    【解决方案2】:

    我现在仍然有这个问题:

    ImportError: DLL load failed: The specified module could not be found.
    PS C:\Users\Dell> & C:/Users/Dell/AppData/Local/Programs/Python/Python37-32/python.exe f:/ERD/Kmeans.py
    Traceback (most recent call last):
      File "f:/ERD/Kmeans.py", line 1, in <module>
        from sklearn.feature_extraction.text import TfidfVectorizer
      File "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\__init__.py", line 75, in <module>
        from .utils._show_versions import show_versions
      File "C:\Users\Dell\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\_show_versions.py", line 12, in <module>
        from ._openmp_helpers import _openmp_parallelism_enabled
    ImportError: DLL load failed: The specified module could not be found.
    PS C:\Users\Dell>
    

    【讨论】:

    猜你喜欢
    • 2021-01-25
    • 2020-08-17
    • 1970-01-01
    • 2013-11-29
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多