Flask(python)中的管道部署

时间:2020-06-03 16:02:54

标签: machine-learning flask data-science pipeline data-modeling

我正在尝试通过Flask部署使用Pipeline构建的模型,但是我面临以下属性错误 '无法从'app.py'获取main上的'FeatureSelector'属性”

这是我的model.py代码: (在加载必要的库并读取数据之后,我为管道定义了类)

class FeatureSelector( BaseEstimator, TransformerMixin ):
#Class Constructor 
def __init__( self, feature_names ):
    self._feature_names = feature_names 

#Return self nothing else to do here    
def fit( self, X, y =None):
    return self 

#Method that describes what we need this transformer to do
def transform( self, X, y = None):
    return X[ self._feature_names ] 

LE = LabelEncoder()
class CategoricalTransformer( BaseEstimator, TransformerMixin ):
#Class constructor method that takes in a list of values as its argument
def __init__(self, cat_cols = ['Response', 'EmploymentStatus', 'Number of Open Complaints',
   'Number of Policies', 'Policy Type', 'Renew Offer Type',
   'Vehicle Class']):
    self._cat_cols = cat_cols

#Return self nothing else to do here
def fit( self, X, y = None  ):
    return self

#Transformer method we wrote for this transformer 
def transform(self, X , y = None ):

   if self._cat_cols:
    for i in X[cat_cols]:
        X[i]= LE.fit_transform(X[i])

   return X.values 

class NumericalTransformer(BaseEstimator, TransformerMixin):
#Class Constructor
def __init__( self, MPA_log = True):
    self._MPA_log = MPA_log

#Return self, nothing else to do here
def fit( self, X, y = None):
    return self 

#Custom transform method we wrote that creates aformentioned features and drops redundant ones 
def transform(self, X, y = None):
    if self._MPA_log:
        X.loc[:,'MPA_log'] = np.log(X['Monthly Premium Auto'])
        X.drop(['Monthly Premium Auto'], axis =1)

    return X.values

我为数字和分类节日创建了不同的管道。它们已使用“功能联盟”在“完整管道”中进行了组合。

   full_pipeline = FeatureUnion( transformer_list = [ ( 'categorical_pipeline', categorical_pipeline ), ( 'numerical_pipeline', numerical_pipeline ) ] )

   X = df.drop('Customer Lifetime Value', axis = 1)
   y = df['Customer Lifetime Value']

   y = np.log(y) #Transforming the y variable

  full_pipeline_RF = Pipeline( steps = [('full_pipeline', full_pipeline),('model', 
  RandomForestRegressor(max_depth=21, min_samples_leaf= 8, random_state=0))])
  full_pipeline_RF.fit(X, y)


  # Saving model to disk
  pickle.dump(full_pipeline_RF, open('model.pkl','wb'))

  # Loading model to compare the results
  model = pickle.load(open('model.pkl','rb'))

已在app.py文件中使用以下代码调用模型:

import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle

app = Flask(__name__)
model = pickle.load(open('model.pkl', 'rb'))

@app.route('/')
def home():
return render_template('index.html')

@app.route('/predict',methods=['POST'])
def predict():
'''
For rendering results on HTML GUI
'''
int_features = [float(x) for x in request.form.values()]
final_features = [np.array(int_features)]
prediction = model.predict(final_features)

output = round(np.exp(prediction[0]),2)


return render_template('index.html', prediction_text='Customer Lifetime Value $ {}'.format(output))


if __name__ == "__main__":
app.run(debug=True)

该代码在Jupyter中正常工作。即使在Spyder中运行,它也不会引发任何错误。请使用此代码帮助我,我只停留在执行位上。

1 个答案:

答案 0 :(得分:0)

这实际上很简单。我要做的就是在app.py中传递在管道中创建的类。 这些是为案例定制的新类,因此需要通过这些类。 在定义每个类之后,只需写“ pass”即可。

相关问题