【问题标题】:I tried downloading nltk 'stopwords' using nltk.download("stopwords"), for NLP model. Which shows an error我尝试使用 nltk.download("stopwords") 为 NLP 模型下载 nltk 'stopwords'。显示错误
【发布时间】:2019-05-03 19:57:24
【问题描述】:

我正在学习机器学习,NLP-自然语言处理,我尝试下载 nltk 停用词。我收到如下错误,代码和错误就像... sklearn 未定义...我也没有在代码中使用它..

我尝试使用命令安装 pip 和 conda, pip install --upgrade nltk(已下载但未安装) conda install -c anaconda nltk(已下载并安装但没有用)

    # importing libraries
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt

    # importing dataset
    dataset = pd.read_csv('Restaurant_Reviews.tsv', delimiter='\t', quoting= 3)

    # Cleaning the texts
    import re
    import nltk
    nltk.download("stopwords")
    review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][0])
    review = review.lower()

#Error! is:
NameError: name 'sklearn' is not defined

Expected is to get nltk downloaded, now the error is,

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-16-12f820b60d6b> in <module>()
     14 
     15 import re
---> 16 import nltk
     17 nltk.download("stopwords")
     18 review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][0])

~\Anaconda4\lib\site-packages\nltk\__init__.py in <module>()
    126 ###########################################################
    127 
--> 128 from nltk.chunk import *
    129 from nltk.classify import *
    130 from nltk.inference import *

~\Anaconda4\lib\site-packages\nltk\chunk\__init__.py in <module>()
    155 from nltk.data import load
    156 
--> 157 from nltk.chunk.api import ChunkParserI
    158 from nltk.chunk.util import (ChunkScore, accuracy, tagstr2tree, conllstr2tree,
    159                              conlltags2tree, tree2conlltags, tree2conllstr, tree2conlltags,

~\Anaconda4\lib\site-packages\nltk\chunk\api.py in <module>()
     11 ##//////////////////////////////////////////////////////
     12 
---> 13 from nltk.parse import ParserI
     14 
     15 from nltk.chunk.util import ChunkScore

~\Anaconda4\lib\site-packages\nltk\parse\__init__.py in <module>()
     77 from nltk.parse.malt import MaltParser
     78 from nltk.parse.evaluate import DependencyEvaluator
---> 79 from nltk.parse.transitionparser import TransitionParser
     80 from nltk.parse.bllip import BllipParser
     81 from nltk.parse.corenlp import CoreNLPParser, CoreNLPDependencyParser

~\Anaconda4\lib\site-packages\nltk\parse\transitionparser.py in <module>()
     19     from numpy import array
     20     from scipy import sparse
---> 21     from sklearn.datasets import load_svmlight_file
     22     from sklearn import svm
     23 except ImportError:

D:\My Data\Data Science\CurrentFocus\ML Python Workspace\Part 0 - Python All Codes & Prints\Codes_Self\sklearn.py in <module>()
      9 # =============================================================================
     10 #Preprocessing Imputer
---> 11 sklearn.preprocessing.Imputer(missing_values='NaN', strategy='mean', axis=0, verbose=0, copy=True)
     12 #Imputation transformer for completing missing values.
     13 

NameError: name 'sklearn' is not defined

【问题讨论】:

    标签: python jupyter-notebook nltk


    【解决方案1】:

    我无法重现该错误,但如果您已经安装了 scikit-learn,请卸载或更新它并重试。您也可以尝试升级 numpy。 请参考这个问题,import nltk does not work

    【讨论】:

    • 错误是:我已经通过 Anaconda 单独并再次安装了 Python。卸载所有内容并仅通过 Anaconda 重新安装,并且成功了!
    猜你喜欢
    • 2015-02-23
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2018-01-01
    • 2020-02-17
    • 2022-10-14
    相关资源
    最近更新 更多