python - How to use native Cpython extensions in Jython -


i have python script in which, used python packages, numpy,scipy etc. when trying run script using jython, gives exception("import error").

my python code is:

import numpy np #import pandas pd #import statsmodels.api sm #import matplotlib.pyplot plt #from patsy import dmatrices #from sklearn.linear_model import logisticregression 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 = []     open(filepath, 'rt') f:         reader = csv.reader(f)         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] in x_y]      y = [i[-1] 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=[]         open(testfile,'rt') f:             reader = csv.reader(f)             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         open(predictionoutput, 'w') fp:              = csv.writer(fp)                 idx,p in enumerate(predicted):                 #print x[idx]                 """                 if list[idx]==0 , int(p)==1:                     count+=1                 elif list[idx]==1 , 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()   

the jython code:

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());     }    } 

you can't use c extensions jython directly, because bound cpython implementation. jython different inside , it's not compatible cpython on c api level.

if want connect jython cpython c extensions, need kind of compatibility layer between them. afaik there no reliable, production-ready library that.

see old question alternatives: using numpy , cpython jython


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -