错误:在flask中部署管道模型时,列表索引必须是整数或片,而不是列表

2024-10-06 12:12:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试部署使用管道生成的模型。它在jupyter或spyder笔记本上运行良好。但是,在部署im时出现以下错误: 列表索引必须是整数或切片,而不是列表

以下是导入库后app.py(flask中的部署)的代码:

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 ] 
pass


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['Monthly Premium Auto'] = np.log(X['Monthly Premium Auto'])
    return X.values
pass

传递为管道创建的函数

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 = [int(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__":  

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

    print("loaded OK")

    app.run(debug=True)

很长一段时间以来,我一直在努力寻找解决办法。任何帮助都会很好。 多谢各位


Tags: toselfnonelogappmodelreturnnames
1条回答
网友
1楼 · 发布于 2024-10-06 12:12:29

我已通过将输出转换为数据帧解决了此错误。这有助于解决我的模型在格式和数据类型方面遇到的所有错误。 Simple将输入数据更改为数据帧

相关问题 更多 >