python-2.7 - NLTK 和 SCIPY 没有执行我的 python 脚本

标签 python-2.7 ubuntu scipy nltk importerror

你好我已经安装了scipyNLTK使用 python 探索机器学习的软件包。

我已经安装了所有必要的依赖项并按照 NLTK 中给出的安装过程进行操作。和 scipy网站

我的脚本在 python shell 中执行良好,但是当我将它保存在 python 脚本中并在终端中执行时,我得到以下输出......

puneet@puneet-HP-Pavilion-g6-Notebook-PC:~/Desktop$ python token.py 
Traceback (most recent call last):
  File "token.py", line 4, in <module>
    import nltk 
  File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 103, in <module>
    from nltk.collocations import *
  File "/usr/local/lib/python2.7/dist-packages/nltk/collocations.py", line 38, in <module>
    from nltk.util import ngrams
  File "/usr/local/lib/python2.7/dist-packages/nltk/util.py", line 13, in <module>
    import pydoc
  File "/usr/lib/python2.7/pydoc.py", line 55, in <module>
    import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
  File "/usr/lib/python2.7/inspect.py", line 39, in <module>
    import tokenize
  File "/usr/lib/python2.7/tokenize.py", line 31, in <module>
    from token import *
  File "/home/puneet/Desktop/token.py", line 6, in <module>
    from sklearn.feature_extraction.text import CountVectorizer
ImportError: No module named sklearn.feature_extraction.text

这是我的脚本:
import nltk 
import scipy
from sklearn.feature_extraction.text import CountVectorizer
#vectorizer = CountVectorizer()

train_set = ("The sky is blue.", "The sun is bright.")
test_set = ("The sun in the sky is bright.",
    "We can see the shining sun, the bright sun.")

print vectorizer

cv = sklearn.feature_extraction.text.CountVectorizer(vocabulary=['sun', 'blue', 'bright'])
cv.fit_transform(['The sun in the sky is bright.', 'We can see the shining sun, the bright sun.', 'The sun is bright.', 'nine days old']).toarray()

有人可以建议一种解决方法来执行我的脚本。我认为这与 PYTHONPATH 变量有关......我认为! :D 但我无法修改它!请帮忙!!

最佳答案

使用 something.py 重命名您的文件因为在 python2.7 包(/usr/lib/python2.7/token.py)中还有一个名为 token.py 的文件,并且存在名称冲突。
如果您收到以下错误:

NameError: name 'N_TOKENS' is not defined

然后以 root 用户身份登录。更改line 30在文件名 tokenize.py
from token import * --> from token2 import *

它还需要重命名文件(/usr/lib/python2.7/token.py):
token.py --> token2.py

从痕迹看来你没有sklearn安装。
您可以使用:
sudo apt-get install python-sklearn

我刚刚修改了您的脚本,添加和删除了一些行。
import nltk 
import scipy
import sklearn         #added this line
from sklearn.feature_extraction.text import CountVectorizer

train_set = ("The sky is blue.", "The sun is bright.")
test_set = ("The sun in the sky is bright.",
    "We can see the shining sun, the bright sun.")
#removed the line which was present
cv = sklearn.feature_extraction.text.CountVectorizer(vocabulary=['sun', 'blue', 'bright'])
output = cv.fit_transform(['The sun in the sky is bright.', 'We can see the shining sun, the bright sun.', 'The sun is bright.', 'nine days old']).toarray()
print output

它编译得很好,没有任何错误。
输出:
manjunath@manjunath-virtual-michine:~/Desktop $ python my_test.py
[[1,0,1]
 [2,0,1]
 [1,0,1]
 [0,0,0]]
[[1,0,1]
 [2,0,1]
 [1,0,1]
 [0,0,0]]
manjunath@manjunath-virtual-michine:~/Desktop $

关于python-2.7 - NLTK 和 SCIPY 没有执行我的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29732862/

相关文章:

python - 像 dict.fromkeys 这样的内置类型的函数描述符与普通方法的行为不同

ruby-on-rails - Puppet 意外删除了 etckeeper-commit-pre 和 etckeeper-commit-post 有没有办法重新生成这些文件?

Android Studio 打开JDK 报错Ubuntu 16.04

python - 创建未知大小的稀疏矩阵

python - 将类方法访问到实例方法的最佳方法

python - 抓取列表的特定索引

node.js - 使用 Node js 在 ubuntu 上运行 React 基本应用程序

python - 如何在 Windows 上的 winpython 中更新 scipy?

python - 运行时错误 : Optimal parameters not found: Number of calls to function has reached maxfev = 800

python - 使用 urllib 下载 HTTPS 页面,错误 :14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error