链接到Lua的Python程序

时间:2017-03-23 16:09:49

标签: python lua

我正在尝试将python程序链接到lua。为此,我安装了 Lunatic-python 。我已经链接了一个简单的python代码。但是当python中包含import语句时,它无法工作。我是否需要安装任何其他lua库才能使它工作?

我的python代码:

import random
import numpy as np
import sys
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics

def knn(k):

    thelist=['alt.atheism','talk.religion.misc','comp.graphics','sci.space','comp.os.ms-windows.misc','comp.sys.ibm.pc.hardware','sci.med','comp.sys.mac.hardware','rec.autos','misc.forsale','comp.windows.x','rec.motorcycles','rec.sport.baseball','rec.sport.hockey','sci.crypt','sci.electronics','talk.politics.guns','talk.politics.mideast','talk.politics.misc','soc.religion.christian']
    categories=random.sample(thelist,k)
    print ("The categories are:")
    print (categories)

    twenty_train = fetch_20newsgroups(subset = 'train',categories = categories,shuffle=True, random_state=42)  
    twenty_test = fetch_20newsgroups(subset='test',categories=categories, shuffle=True, random_state=42)   
    vectorizer = CountVectorizer(min_df=1)

    count_vect = CountVectorizer()
    X_train_counts = count_vect.fit_transform(twenty_train.data)
    tfidf_transformer = TfidfTransformer()
    X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
    clf = MultinomialNB().fit(X_train_tfidf, twenty_train.target)


    print("Enter the text to classify: ")
    ans = sys.stdin.readline()
    ans = ans.strip('\n')
    if ans.endswith(' '):
        ans = ans.rstrip(' ')
    docs_new = [ans]


    X_new_counts = count_vect.transform(docs_new)
    X_new_tfidf = tfidf_transformer.transform(X_new_counts)  

    predicted = clf.predict(X_new_tfidf)   
    for doc, category in zip(docs_new, predicted):   
        print('%r => %s' % (doc, twenty_train.target_names[category])) 


    text_clf = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()),('clf', KNeighborsClassifier())])  
    text_clf.fit(twenty_train.data, twenty_train.target)  

    docs_test = twenty_test.data   
    knn_predicted = text_clf.predict(docs_test)  

    accuracy=np.mean(knn_predicted == twenty_test.target)  

    print ("The accuracy of twenty_test is %s" %accuracy)  
    print(metrics.classification_report(twenty_test.target, knn_predicted,target_names=twenty_test.target_names)) 

我的Lua代码:

py = require 'python'
knn = py.import "kk".knn
k=io.read()
knn(k)

错误是:

Traceback (most recent call last):
  File "kk.py", line 5, in knn
    import numpy as np
  File "/usr/local/lib/python2.7/dist-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py", line 24, in <module>
    raise ImportError(msg)
ImportError: 
Importing the multiarray numpy extension module failed.  Most likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all files not under version control).  Otherwise reinstall numpy.

lua: l.lua:4: error calling python function
stack traceback:
    [C]: in function 'knn'
    l.lua:4: in main chunk
    [C]: ?

1 个答案:

答案 0 :(得分:0)

  

导入多阵列numpy扩展模块失败。您很可能正在尝试导入失败的numpy版本。

您可能没有正确安装numpy。尝试使用pip uninstall numpy删除Numpy,然后使用pip install numpy重新安装。