1

I have a python script in which, i used python packages, like numpy,scipy etc. When i am trying to run the script using Jython, it gives an exception("Import error").

My python code is:

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

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());
    }   
}
Narendra Rawat
  • 353
  • 2
  • 5
  • 17
  • The other thing to consider is that there are some decent Java numerical libraries out there that you can use from Jython (if you have good reason for wanting to use Jython instead of CPython). – DavidW Oct 01 '15 at 09:38

1 Answers1

4

You can't use C extensions from Jython directly, because they are bound to CPython implementation. Jython is very different inside and it's not compatible with CPython on C API level.

If you want to connect Jython to CPython C extensions, you need some kind of compatibility layer between them. But AFAIK there is no reliable, production-ready library which does that.

See also this old question for some alternatives: Using NumPy and Cpython with Jython

Community
  • 1
  • 1
alexanderlukanin13
  • 4,577
  • 26
  • 29
  • I had found the same thing [JyNI](http://jyni.org/) ,but don't know how to use it. – Narendra Rawat Sep 30 '15 at 05:24
  • @NarendraRawat Neither do I. It looks promising, but it's definitely not production ready. If I were you, I would either dump Jython completely, or, if it's not possible, run Jython and CPython in separate processes with [IPC](https://en.wikipedia.org/wiki/Inter-process_communication) via named pipes or sockets. Mixing Java and CPython in the same process will definitely bring you a lot of headache. – alexanderlukanin13 Sep 30 '15 at 05:32