我使用Flask制作了一个API,该API使用预先训练的Scikit学习模型提供了预测。该API在本地主机和Heroku本地网络上都可以正常运行,但是在部署模型时无法加载该模型。 API以JSON形式返回预测。在本地运行时返回{"prediction":123}
。部署后将返回{"error":"Failed to load model"}
。
应用架构:
- houseprediction.py
- waitressServer.py
- requirements.txt
- Procfile
- supp-files:
- gbModel.pkl
- model_columns.pkl
- sectorLabels.pkl
houseprediction.py:
from flask import Flask, jsonify, request
from sklearn.externals import joblib
import pandas as pd
import os
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
dir_path = os.path.dirname(os.path.realpath(__file__))
try:
gbModel = joblib.load("{}\\supp-files\\gbModel.pkl".format(dir_path))
except:
return jsonify({'error': 'Failed to load model'})
try:
model_columns = joblib.load("{}\\supp-files\\model_columns.pkl".format(dir_path))
except:
return jsonify({'error': 'Failed to load model columns'})
try:
lbl = joblib.load("{}\\supp-files\\sectorLabels.pkl".format(dir_path))
except:
return jsonify({'error': 'Failed to load sector labels'})
json_ = request.get_json()
query_df = pd.DataFrame(json_, index=[0])
pd.options.display.max_columns = 50
query_df['sector'] = lbl.transform([query_df['sector']])[0]
print(query_df.dtypes)
query = pd.get_dummies(query_df)
for col in model_columns:
if col not in query.columns:
query[col] = 0
print(query_df.shape)
print(query_df.head(1))
prediction = gbModel.predict(query)
print(prediction)
return jsonify({'prediction': prediction[0]})
@app.route('/')
def home():
return "Welcome to House Prediction"
waitress.py:
from waitress import serve
import os
import housepredictionServer
serve(housepredictionServer.app, port=os.environ['PORT'])
Procfile:web: python waitressServer.py
让我知道是否还有其他需要。
答案 0 :(得分:1)
将所有路径更改为library(MASS)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following object is masked from 'package:MASS':
#>
#> select
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
data("iris")
iris$Species<-NULL
C <- cov(iris) # Note covariance, not cor
mu <- colMeans(iris)
df <- as_tibble(mvrnorm(10000,mu,C))
cor(df)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length 1.0000000 -0.1269997 0.8728482 0.8220813
#> Sepal.Width -0.1269997 1.0000000 -0.4334930 -0.3707066
#> Petal.Length 0.8728482 -0.4334930 1.0000000 0.9633552
#> Petal.Width 0.8220813 -0.3707066 0.9633552 1.0000000
cor(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length 1.0000000 -0.1175698 0.8717538 0.8179411
#> Sepal.Width -0.1175698 1.0000000 -0.4284401 -0.3661259
#> Petal.Length 0.8717538 -0.4284401 1.0000000 0.9628654
#> Petal.Width 0.8179411 -0.3661259 0.9628654 1.0000000
,因为heroku是基于Unix的系统