从java运行python sklearn分类器
我使用Sklearn和其他库在python中训练了一个SVC分类器。我通过构建管道(sklearn)实现了这一点 我能够在pickle文件中转储经过训练的模型,并生成另一个python脚本,该脚本将加载pickle文件并从命令行获取输入以进行预测。我能够从java调用这个python脚本,它工作得很好。 唯一的问题是它需要很多时间,因为我在python脚本中调用了nltk、numpy和panda库,这是预处理输入参数所必需的。我多次调用这个python脚本,这增加了时间。 我如何解决这个问题 这就是我的皮勒线的样子从java运行python sklearn分类器,java,python,scikit-learn,Java,Python,Scikit Learn,我使用Sklearn和其他库在python中训练了一个SVC分类器。我通过构建管道(sklearn)实现了这一点 我能够在pickle文件中转储经过训练的模型,并生成另一个python脚本,该脚本将加载pickle文件并从命令行获取输入以进行预测。我能够从java调用这个python脚本,它工作得很好。 唯一的问题是它需要很多时间,因为我在python脚本中调用了nltk、numpy和panda库,这是预处理输入参数所必需的。我多次调用这个python脚本,这增加了时间。 我如何解决这个问题 这
pipeline = Pipeline([
# Use FeatureUnion to combine the features from dataset
('union', FeatureUnion(
transformer_list=[
# Pipeline for getting POS
('ngrams', Pipeline([
('selector', ItemSelector(key='Sentence')),
('vect', CountVectorizer(analyzer='word')),
('tfidf', TfidfTransformer()),
])),
],
# weight components in FeatureUnion
transformer_weights={
'ngrams': 0.7,
},
)),
# Use a SVC classifier on the combined features
('clf', LinearSVC()),
])
下面是一个为scikit模型设置一个简单的RESTAPI烧瓶的示例
import sys
import os
import time
import traceback
from flask import Flask, request, jsonify
from sklearn.externals import joblib
app = Flask(__name__)
model_directory = 'model'
model_file_name = '%s/model.pkl' % model_directory
# These will be populated at training time
clf = None
@app.route('/predict', methods=['POST'])
def predict():
if clf:
try:
json_ = request.json
# query = get the payload from the json and feed it to your model
prediction = list(clf.predict(query))
return jsonify({'prediction': prediction})
except Exception, e:
return jsonify({'error': str(e), 'trace': traceback.format_exc()})
else:
return 'no model here'
if __name__ == '__main__':
try:
port = int(sys.argv[1])
except Exception, e:
port = 80
try:
clf = joblib.load(model_file_name)
print 'model loaded'
app.run(host='0.0.0.0', port=port, debug=True)
下面是一个为scikit模型设置一个简单的RESTAPI烧瓶的示例
import sys
import os
import time
import traceback
from flask import Flask, request, jsonify
from sklearn.externals import joblib
app = Flask(__name__)
model_directory = 'model'
model_file_name = '%s/model.pkl' % model_directory
# These will be populated at training time
clf = None
@app.route('/predict', methods=['POST'])
def predict():
if clf:
try:
json_ = request.json
# query = get the payload from the json and feed it to your model
prediction = list(clf.predict(query))
return jsonify({'prediction': prediction})
except Exception, e:
return jsonify({'error': str(e), 'trace': traceback.format_exc()})
else:
return 'no model here'
if __name__ == '__main__':
try:
port = int(sys.argv[1])
except Exception, e:
port = 80
try:
clf = joblib.load(model_file_name)
print 'model loaded'
app.run(host='0.0.0.0', port=port, debug=True)
优化特征提取过程并不是一件小事,但我的猜测是,每次调用脚本时,您都要花费大量时间从内存加载模型。考虑使用一个长时间运行的进程,并使用HTTP或另一个IPC方法与它通信。你有什么例子,我如何使用HTTP?你也可以使用,将您的模型转换为PMML,然后直接在java中加载。优化特征提取过程并不简单,但我猜您在每次调用脚本时都会花费大量时间从内存加载模型。考虑使用一个长时间运行的进程,并使用HTTP或另一个IPC方法与之通信。你有什么例子我将如何使用HTTP?你也可以使用,将你的模型转换成PMML,然后直接用java加载它们。