【问题标题】:How to use native Cpython extensions in Jython如何在 Jython 中使用本机 Cpython 扩展
【发布时间】:2015-12-27 18:27:30
【问题描述】:

我有一个 python 脚本,我在其中使用了 python 包,如 numpy、scipy 等。当我尝试使用 Jython 运行脚本时,它给出了一个异常(“导入错误”)。

我的python代码是:

import numpy as np
#import pandas as pd
#import statsmodels.api as sm
#import matplotlib.pyplot as plt
#from patsy import dmatrices
#from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier 
import pandas
import pickle
#from numpy import genfromtxt, savetxt
import csv

trainingData="/home/gauge/Documents/TrainingData1/Training4.csv"
testFile = "/home/gauge/Documents/TrainingData1/TestCase1.csv"
PredictionOutput="/home/gauge/Documents/TrainingData1/result4.csv"

def make_x_and_y(filepath):

    x_y = []
    with open(filepath, 'rt') as f:
        reader = csv.reader(f)
        for idx,row in enumerate(reader):
            if idx<0: continue

            x_y.append([row[2],row[3],row[4],row[5],row[6],row[7],row[8]])
            #x_y.append([row[2],row[3],row[4],row[5],row[6],row[8]])
    #print x   
    X = [i[:-1] for i in x_y]

    y = [i[-1] for i in x_y]
    X = np.array(X,dtype='f8')

    #print file_path
    y = np.array(y,dtype='f8')
    #print X.shape, y.shape
    return X,y  




def build_model(filepath):
    X,y = make_x_and_y(filepath)
    target = np.array(y,dtype='f8')
    train  = np.array(X,dtype='f8')
    model = RandomForestClassifier(n_estimators=150,max_features=5,random_state=1,max_depth=10)
    model.fit(train, target)
    file_object=open("/home/gauge/Documents/pickle/model.pkl",'wb')
    pickle.dump(model,file_object,-1)
    return model



def predict():

    #for index in range(10,200):

        model = build_model(trainingData)
        X=[]
        data=[]
        with open(testFile,'rt') as f:
            reader = csv.reader(f)
            for idx,row in enumerate(reader):
                if idx<0: continue
                data.append([row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7]])
                X.append([row[2],row[3],row[4],row[5],row[6],row[7]])

            X=np.array(X,dtype='f8')   

            if (len(X) != 0 ):
                predicted = model.predict(X)
    #            prob=model.predict_proba(X)[0]
                #print prob        

        file_table=pandas.read_csv("/home/gauge/Documents/TrainingData1/testdata2.csv",sep=",",quoting=1)
        list=[]        
        list =file_table['Correct']
        #print list  
        count=0
        count1=0
        with open(PredictionOutput, 'w') as fp:
             a = csv.writer(fp)   
             for idx,p in enumerate(predicted):
                #print X[idx]
                """
                if list[idx]==0 and int(p)==1:
                    count+=1
                elif list[idx]==1 and int(p)==0:
                    count1+=1                
             print "FP -",count,"FN -",count1
                   """  


                prob =  model.predict_proba(X[idx])[0][0]
                prob1=model.predict_proba(X[idx])[0][1]            
                print prob,prob1
                #a.writerows([[str(data[idx][0]),str(data[idx][1]),int(p)]])


                if prob1>=0.90:
                    a.writerows([[str(data[idx][0]),str(data[idx][1]),int(p),prob,prob1]])
                    if list[idx]==0:
                        count+=1
                else:
                    a.writerows([[str(data[idx][0]),str(data[idx][1]),0,prob,prob1]])                
                    if list[idx]==1:
                        count1+=1
             print "FP -",count,"FN -",count1

predict()  

Jython 代码:

package com.gauge.ie.Jython;

import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;


public class PyJava 
{
    public static void main(String args[])
    {
        PythonInterpreter py=new PythonInterpreter();
        py.execfile("/home/gauge/Spyder/Classifier.py");
        PyObject obj=py.get("a");
        System.out.println("val: "+obj.toString());
    }   
}

【问题讨论】:

  • 要考虑的另一件事是,您可以从 Jython 中使用一些不错的 Java 数值库(如果您有充分的理由想要使用 Jython 而不是 CPython)。

标签: python numpy jython random-forest


【解决方案1】:

您不能直接使用来自 Jython 的 C 扩展,因为它们绑定到 CPython 实现。 Jython 内部非常不同,它在 C API 级别上与 CPython 不兼容。

如果要将 Jython 连接到 CPython C 扩展,则需要在它们之间使用某种 compatibility layer。但是 AFAIK 没有可靠的、可用于生产的库可以做到这一点。

另请参阅这个老问题以获取一些替代方案:Using NumPy and Cpython with Jython

【讨论】:

  • 我找到了同样的东西JyNI,但是不知道怎么用。
  • @NarendraRawat 我也没有。它看起来很有希望,但它肯定还没有准备好生产。如果我是你,我会完全转储 Jython,或者,如果不可能,则通过命名管道或套接字使用 IPC 在单独的进程中运行 Jython 和 CPython。在同一个进程中混合使用Java和CPython肯定会给你带来很多麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
相关资源
最近更新 更多