我有一个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());
}
}
答案 0 :(得分:4)
您不能直接使用Jython的C扩展,因为它们绑定到CPython实现。 Jython内部非常不同,它与C API级别的CPython不兼容。
如果要将Jython连接到CPython C扩展,则需要在它们之间使用某种compatibility layer。但是AFAIK没有可靠的,可以生产的库来实现这一点。
有关其他选择,请参阅此旧问题:Using NumPy and Cpython with Jython